1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 __all__ = (
23 "RestClient",
24 )
25
26 import urlparse
27 import urllib
28 import base64
29 import warnings
30 import socket
31 import sys
32
33 with warnings.catch_warnings():
34 warnings.filterwarnings("ignore",category=DeprecationWarning)
35 import httplib2
36
39 self.base_url = base_url
40 self.url = urlparse.urlparse(base_url)
41
42 - def request_get(self, resource, args = None, body = None, headers = {}):
43 return self.request(resource, "get", args, body, headers)
44
45 - def request_post(self, resource, args = None, body = None, headers = {}):
46 return self.request(resource, "post", args, body, headers)
47
48 - def request_put(self, resource, args = None, body = None, headers = {}):
49 return self.request(resource, "put", args, body, headers)
50
51 - def request(self, resource, method = "get", args = None, body = None, headers = {}):
52 path = resource
53 headers['Accept'] = 'application/json'
54 http = httplib2.Http(".cache")
55
56 if args:
57 if method == "put" or method == "post":
58 headers['Content-Type'] = 'application/json'
59 body = args
60 try:
61 response, content = http.request("%s%s" % (self.base_url, resource), method.upper(), body=body, headers=headers)
62 return (response, content.decode("UTF-8"))
63 except httplib2.ServerNotFoundError as e:
64 print "Error:%s, Maybe the flies sever is down?"%e
65 sys.exit(2)
66 except httplib2.HttpLib2Error as e:
67 print "Error:%s"%e
68 sys.exit(2)
69