forked from membase/membase-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrestclient.py
206 lines (168 loc) · 5.76 KB
/
restclient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
membase_cli_rest_client
Class with methods for contacting to a HTTP server, sending REST commands
and processing the JSON response
"""
import sys
import socket
import httplib
import urllib
import base64
import simplejson as json
import string
from StringIO import StringIO
class RestClient:
def __init__(self, server, port, opts= {}):
"""
__init__()
This method is the instatiation method
"""
# do something here?
self.server = server
self.port = port
self.debug = opts.get('debug', False)
self.uri = '/pools'
self.method = 'GET' # default
self.params = {}
self.user = ''
self.password = ''
self.clientConnect(server, int(port))
def clientConnect(self, server, port):
error_connect = "Unable to connect to %s" % self.server
try:
self.conn = httplib.HTTPConnection(server, port)
except httplib.HTTPException:
print error_connect
sys.exit(2)
except httplib.NotConnected:
print error_connect
sys.exit(2)
except socket.error:
print error_connect
sys.exit(2)
except socket.gaierror:
print error_connect
sys.exit(2)
def setParam(self, param, value):
"""
setParam()
This method allows the caller to set parameters which are in
turn later encoded for REST POST commands
"""
self.params[param] = value
def handleResponse(self,
method,
response,
opts={ 'success_msg':'',
'error_msg':''}):
"""
handleResponse()
This method handles the response, success or failure, and
returns the necessary data to the caller
"""
error = False
if response.status == 200 or response.status == 204:
if method == 'GET':
data = response.read()
else:
data = "SUCCESS: %s" % opts['success_msg']
elif response.status == 401:
error = True
data = 'ERROR: Unable to access the REST API - check username and password!'
else:
error = True
data = 'ERROR: %s %s' % (opts['error_msg'], response.reason)
if error:
print data
sys.exit(2)
return data
def bootStrap(self, headers):
"""
bootStrap()
This method produces the initial access required for subsequent
REST commands to function properly
"""
opts = {'error_msg':'Unable to bootstrap'}
self.conn.request('GET', '/pools', '', headers)
response = self.conn.getresponse()
return self.handleResponse('GET', response, opts)
def sendCmd(self,
method,
uri,
user='',
password='',
opts = {}):
data = ''
headers = {}
encoded_params = ''
"""
sendCmd()
This method handles accessing the REST API and returning
either data, if a GET, or a success or error message if a POST
"""
if user and password:
self.user = user
self.password = password
auth = 'Basic ' + string.strip(
base64.encodestring(user + ':' + password))
headers['Authorization'] = auth
# this step must always be performed
self.bootStrap(headers)
# send the request to the server
if method == 'POST':
encoded_params = urllib.urlencode(self.params)
headers['Content-type'] = 'application/x-www-form-urlencoded'
else:
if self.params:
uri = uri, '?', urllib.urlencode(self.params)
if self.debug:
print "METHOD: %s" % method
print "PARAMS: ", self.params
print "ENCODED_PARAMS: %s" % encoded_params
print "REST CMD: %s %s" % (method,uri)
self.makeRequest(method, uri, encoded_params, headers)
# obtain the response
response = self.conn.getresponse()
if self.debug:
print "response.status: %s" % response.status
return response
def makeRequest(self, method, uri, encoded_params, headers):
"""
makeRequest()
This method handles the attempt to connect to the REST API
on the given server
"""
error_connect = "Unable to connect to %s" % self.server
try:
self.conn.request(method, uri, encoded_params, headers)
except httplib.HTTPException:
print error_connect
sys.exit(2)
except httplib.NotConnected:
print error_connect
sys.exit(2)
except socket.error:
print error_connect
sys.exit(2)
except socket.gaierror:
print error_connect
sys.exit(2)
def getJson(self, data):
return json.loads(data)
def jsonMessage(self, data):
return json.JSONEncoder().encode(data)
def restCmd(self, method, uri, user='', password='', opts={}):
"""
handlePostResponse - this handles the response
from a POST according to the operation/cmd run
"""
if method == None:
method = 'GET'
response = self.sendCmd(method,
uri,
user,
password,
opts)
return self.handleResponse(method, response, opts)