typo fix
[pykit.git] / PythonHTTP.cc
1 // $Id$
2 //
3 // Copyright (C) 2011
4
5 /** \file
6     \brief PythonHTTP non-inline non-template implementation */
7
8 //#include "PythonHTTP.hh"
9 //#include "PythonHTTP.ih"
10
11 // Custom includes
12 #include <boost/python.hpp>
13 #include <QNetworkAccessManager>
14 #include <QNetworkRequest>
15 #include <QNetworkReply>
16 #include <QTimer>
17
18 //#include "PythonHTTP.mpp"
19 #define prefix_
20 ///////////////////////////////cc.p////////////////////////////////////////
21
22 namespace py = boost::python;
23
24 namespace {
25
26     class Manager
27     {
28     public:
29         void post(QString const & url, QByteArray const & data, boost::python::object callback);
30         void get(QString const & url, boost::python::object callback);
31
32     private:
33         QNetworkAccessManager mgr_;
34     };
35
36     class ReplyHandler : public QObject
37     {
38         Q_OBJECT;
39     public:
40         explicit ReplyHandler(boost::python::object callback, QNetworkReply * reply);
41         virtual ~ReplyHandler();
42
43     public slots:
44         void finished();
45
46     private:
47         boost::python::object callback_;
48         QNetworkReply * reply_;
49     };
50
51     class TimeoutHandler : public QObject
52     {
53         Q_OBJECT;
54     public:
55         explicit TimeoutHandler(boost::python::object callback, unsigned msecs);
56
57     public slots:
58         void timeout();
59
60     private:
61         boost::python::object callback_;
62         QTimer timer_;
63     };
64
65     void timeout(boost::python::object callback, unsigned msecs)
66     {
67         new TimeoutHandler(callback, msecs);
68     }
69 }
70
71 prefix_ void Manager::post(QString const & url, QByteArray const & data,
72                            boost::python::object callback)
73 {
74     QNetworkRequest req;
75     req.setUrl(QUrl(url));
76     QNetworkReply * reply (mgr_.post(req, data));
77     new ReplyHandler(callback, reply);
78 }
79
80 prefix_ void Manager::get(QString const & url, boost::python::object callback)
81 {
82     QNetworkRequest req;
83     req.setUrl(QUrl(url));
84     QNetworkReply * reply (mgr_.get(req));
85     new ReplyHandler(callback, reply);
86 }
87
88 prefix_ ReplyHandler::ReplyHandler(boost::python::object callback, QNetworkReply * reply)
89     : callback_ (callback), reply_ (reply)
90 {
91     connect(reply_,SIGNAL(finished()),this,SLOT(finished()));
92 }
93
94 prefix_ ReplyHandler::~ReplyHandler()
95 {}
96
97 prefix_ void ReplyHandler::finished()
98 {
99     callback_(reply_->error() == QNetworkReply::NoError, reply_->readAll());
100     reply_->deleteLater();
101     deleteLater();
102 }
103
104 prefix_ TimeoutHandler::TimeoutHandler(boost::python::object callback, unsigned msecs)
105     : callback_ (callback)
106 {
107     timer_.setSingleShot(true);
108     timer_.start(msecs);
109     connect(&timer_, SIGNAL(timeout()), this, SLOT(timeout()));
110 }
111
112 prefix_ void TimeoutHandler::timeout()
113 {
114     callback_();
115     deleteLater();
116 }
117
118 BOOST_PYTHON_MODULE(_httpapi)
119 {
120     py::class_<Manager, boost::noncopyable>("Manager")
121         .def("post", &Manager::post)
122         .def("get", &Manager::get)
123         ;
124
125     py::def("timeout", &timeout);
126 }
127
128 #include "PythonHTTP.moc"
129
130 ///////////////////////////////cc.e////////////////////////////////////////
131 #undef prefix_
132 //#include "PythonHTTP.mpp"
133
134 \f
135 // Local Variables:
136 // mode: c++
137 // indent-tabs-mode: nil
138 // End: