PageRenderTime 25ms CodeModel.GetById 18ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/library/HTTPRequest.py

http://pyjamas.googlecode.com/
Python | 88 lines | 82 code | 4 blank | 2 comment | 2 complexity | 3d0270d098140a45b88605ce57c20e83 MD5 | raw file
 1from __pyjamas__ import JS
 2
 3class HTTPRequest:
 4    # also callable as: asyncPost(self, url, postData, handler)
 5    def asyncPost(self, user, pwd, url, postData=None, handler=None):
 6        if postData == None:
 7            return self.asyncPostImpl(None, None, user, pwd, url)
 8        return self.asyncPostImpl(user, pwd, url, postData, handler)
 9
10    # also callable as: asyncGet(self, url, handler)
11    def asyncGet(self, user, pwd, url, handler):
12        if url == None:
13            return self.asyncGetImpl(None, None, user, pwd)
14        return self.asyncGetImpl(user, pwd, url, handler)
15
16    def createXmlHTTPRequest(self):
17        return self.doCreateXmlHTTPRequest()
18
19    def doCreateXmlHTTPRequest(self):
20        JS("""
21        return new XMLHttpRequest();
22        """)
23
24    def asyncPostImpl(self, user, pwd, url, postData, handler):
25        JS("""
26        var xmlHttp = this.doCreateXmlHTTPRequest();
27        try {
28            xmlHttp.open("POST", url, true);
29            xmlHttp.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
30            xmlHttp.onreadystatechange = function() {
31                if (xmlHttp.readyState == 4) {
32                    delete xmlHttp.onreadystatechange;
33                    var localHandler = handler;
34                    var responseText = xmlHttp.responseText;
35                    var status = xmlHttp.status;
36                    handler = null;
37                    xmlHttp = null;
38                    if(status == 200) {
39                        localHandler.onCompletion(responseText);
40                    } else {
41                        localHandler.onError(responseText, status);
42                    }
43                }
44            };
45            xmlHttp.send(postData);
46            return true;
47        }
48        catch (e) {
49            delete xmlHttp.onreadystatechange;
50            handler = null;
51            xmlHttp = null;
52            localHandler.onError(String(e));
53            return false;
54        }
55        """)
56
57    def asyncGetImpl(self, user, pwd, url, handler):
58        JS("""
59        var xmlHttp = this.doCreateXmlHTTPRequest();
60        try {
61            xmlHttp.open("GET", url, true);
62            xmlHttp.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
63            xmlHttp.onreadystatechange = function() {
64                if (xmlHttp.readyState == 4) {
65                    delete xmlHttp.onreadystatechange;
66                    var localHandler = handler;
67                    var responseText = xmlHttp.responseText;
68                    var status = xmlHttp.status;
69                    handler = null;
70                    xmlHttp = null;
71                    if(status == 200) {
72                        localHandler.onCompletion(responseText);
73                    } else {
74                        localHandler.onError(responseText, status);
75                    }
76                }
77            };
78            xmlHttp.send('');
79            return true;
80        }
81        catch (e) {
82            delete xmlHttp.onreadystatechange;
83            handler = null;
84            xmlHttp = null;
85            localHandler.onError(String(e));
86            return false;
87        }
88        """)