1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2011 Citrix Systems
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
22
class MiniClient(object):
24
"""A base client class - derived from Glance.BaseClient"""
26
action_prefix = '/v0.1/tenants/{tenant_id}'
28
def __init__(self, host, port, use_ssl):
30
Creates a new client to some service.
32
:param host: The host where service resides
33
:param port: The port where service resides
34
:param use_ssl: Should we use HTTPS?
38
self.use_ssl = use_ssl
39
self.connection = None
41
def get_connection_type(self):
43
Returns the proper connection type
46
return httplib.HTTPSConnection
48
return httplib.HTTPConnection
50
def do_request(self, tenant, method, action, body=None,
51
headers=None, params=None):
53
Connects to the server and issues a request.
54
Returns the result data, or raises an appropriate exception if
55
HTTP status code is not 2xx
57
:param method: HTTP method ("GET", "POST", "PUT", etc...)
58
:param body: string of data to send, or None (default)
59
:param headers: mapping of key/value pairs to add as headers
60
:param params: dictionary of key/value pairs to add to append
64
action = MiniClient.action_prefix + action
65
action = action.replace('{tenant_id}',tenant)
66
if type(params) is dict:
67
action += '?' + urllib.urlencode(params)
70
connection_type = self.get_connection_type()
71
headers = headers or {}
73
# Open connection and send request
74
c = connection_type(self.host, self.port)
75
c.request(method, action, body, headers)
77
status_code = self.get_status_code(res)
78
if status_code in (httplib.OK,
84
raise Exception("Server returned error: %s" % res.read())
86
except (socket.error, IOError), e:
87
raise Exception("Unable to connect to "
88
"server. Got error: %s" % e)
90
def get_status_code(self, response):
92
Returns the integer status code from the response, which
93
can be either a Webob.Response (used in testing) or httplib.Response
95
if hasattr(response, 'status_int'):
96
return response.status_int
98
return response.status
b'\\ No newline at end of file'