Revision 0c75cbf7 commissioning/clients/http.py
b/commissioning/clients/http.py | ||
---|---|---|
1 | 1 |
#!/usr/bin/env python |
2 | 2 |
|
3 |
from httplib import HTTPConnection, HTTPException
|
|
3 |
from synnefo.lib.pool.http import get_http_connection
|
|
4 | 4 |
from urlparse import urlparse |
5 | 5 |
from commissioning import Callpoint |
6 | 6 |
from commissioning.utils.clijson import clijson |
... | ... | |
40 | 40 |
def debug(fmt, *args): |
41 | 41 |
global _logger |
42 | 42 |
if _logger is None: |
43 |
init_logger_stderr('logger')
|
|
43 |
init_logger_stderr('logger')
|
|
44 | 44 |
_logger.debug(fmt % args) |
45 | 45 |
|
46 | 46 |
|
... | ... | |
61 | 61 |
def do_make_call(self, api_call, data): |
62 | 62 |
url = urlparse(self.url) |
63 | 63 |
scheme = url.scheme |
64 |
if scheme == 'http': |
|
65 |
port = 80 |
|
66 |
elif scheme == 'https': |
|
67 |
port = 443 |
|
68 |
else: |
|
69 |
raise ValueError("Unsupported scheme %s" % (scheme,)) |
|
70 |
|
|
71 | 64 |
path = url.path.strip('/') |
72 | 65 |
path = ('/' + path + '/' + api_call) if path else ('/' + api_call) |
66 |
netloc = url.netloc |
|
73 | 67 |
|
74 |
netloc = url.netloc.rsplit(':', 1) |
|
75 |
netloclen = len(netloc) |
|
76 |
if netloclen == 1: |
|
77 |
host = netloc[0] |
|
78 |
elif netloclen == 2: |
|
79 |
host, port = netloc |
|
80 |
else: |
|
81 |
msg = "Unsupported network location type '%s'" % (netloc,) |
|
82 |
raise ValueError(msg) |
|
83 |
|
|
84 |
debug("Connecting to %s:%s\n>>>", host, port) |
|
85 |
conn = HTTPConnection(host, port) |
|
86 |
|
|
87 |
if (api_call.startswith('list') or |
|
88 |
api_call.startswith('get') or |
|
89 |
api_call.startswith('read')): |
|
90 |
|
|
91 |
method = 'GET' |
|
92 |
else: |
|
93 |
method = 'POST' |
|
94 |
|
|
95 |
json_data = self.json_dumps(data) |
|
96 |
debug("%s %s\n%s\n<<<\n", method, path, json_data) |
|
97 |
|
|
98 |
req = conn.request(method, path, body=json_data) |
|
99 |
resp = conn.getresponse() |
|
100 |
debug(">>>\nStatus: %s", resp.status) |
|
101 |
|
|
102 |
for name, value in resp.getheaders(): |
|
103 |
debug("%s: %s", name, value) |
|
104 |
|
|
105 |
body = '' |
|
106 |
while 1: |
|
107 |
s = resp.read() |
|
108 |
if not s: |
|
109 |
break |
|
110 |
body += s |
|
111 |
|
|
112 |
debug("\n%s\n<<<\n", body) |
|
113 |
|
|
114 |
status = int(resp.status) |
|
115 |
if status == 200: |
|
116 |
if body: |
|
117 |
body = json_loads(body) |
|
118 |
return body |
|
119 |
else: |
|
120 |
return body |
|
121 |
|
|
122 |
raise IOError("Call Failed", str(resp.status)) |
|
68 |
conn = None |
|
69 |
try: |
|
70 |
debug("Connecting to %s\n>>>", netloc) |
|
71 |
conn = get_http_connection(netloc=netloc, scheme=scheme) |
|
72 |
if (api_call.startswith('list') or |
|
73 |
api_call.startswith('get') or |
|
74 |
api_call.startswith('read')): |
|
75 |
|
|
76 |
method = 'GET' |
|
77 |
else: |
|
78 |
method = 'POST' |
|
79 |
|
|
80 |
json_data = self.json_dumps(data) |
|
81 |
debug("%s %s\n%s\n<<<\n", method, path, json_data) |
|
82 |
|
|
83 |
req = conn.request(method, path, body=json_data) |
|
84 |
resp = conn.getresponse() |
|
85 |
debug(">>>\nStatus: %s", resp.status) |
|
86 |
|
|
87 |
for name, value in resp.getheaders(): |
|
88 |
debug("%s: %s", name, value) |
|
89 |
|
|
90 |
body = '' |
|
91 |
while 1: |
|
92 |
s = resp.read() |
|
93 |
if not s: |
|
94 |
break |
|
95 |
body += s |
|
96 |
|
|
97 |
debug("\n%r\n<<<\n", body) |
|
98 |
|
|
99 |
status = int(resp.status) |
|
100 |
if status == 200: |
|
101 |
if body: |
|
102 |
body = json_loads(body) |
|
103 |
return body |
|
104 |
else: |
|
105 |
return body |
|
106 |
|
|
107 |
raise IOError("Call Failed", str(resp.status)) |
|
108 |
finally: |
|
109 |
if conn is not None: |
|
110 |
conn.close() |
|
123 | 111 |
|
124 | 112 |
API_Callpoint = HTTP_API_Client |
125 | 113 |
|
... | ... | |
133 | 121 |
progname = basename(argv[0]) |
134 | 122 |
h, s, t = progname.rpartition('.') |
135 | 123 |
if t == 'py': |
136 |
progname = h
|
|
124 |
progname = h
|
|
137 | 125 |
|
138 | 126 |
if progname == 'http': |
139 | 127 |
if len(argv) < 2: |
Also available in: Unified diff