@@ -70,7 +70,8 @@ class Client(object):
70
70
71
71
USER_AGENT = 'vsc-rest-client'
72
72
73
- def __init__ (self , url , username = None , password = None , token = None , token_type = 'Token' , user_agent = None , append_slash = False ):
73
+ def __init__ (self , url , username = None , password = None , token = None , token_type = 'Token' , user_agent = None ,
74
+ append_slash = False ):
74
75
"""
75
76
Create a Client object,
76
77
this client can consume a REST api hosted at host/endpoint
@@ -104,78 +105,73 @@ def __init__(self, url, username=None, password=None, token=None, token_type='To
104
105
elif token is not None :
105
106
self .auth_header = '%s %s' % (token_type , token )
106
107
108
+ def _append_slash_to (self , url ):
109
+ """Append slash to specified URL, if desired and needed."""
110
+ if self .append_slash and not url .endswith ('/' ):
111
+ url += '/'
112
+ return url
113
+
107
114
def get (self , url , headers = None , ** params ):
108
115
"""
109
116
Do a http get request on the given url with given headers and parameters
110
117
Parameters is a dictionary that will will be urlencoded
111
118
"""
112
- if self .append_slash :
113
- url += '/'
114
- url += self .urlencode (params )
119
+ url = self ._append_slash_to (url ) + self .urlencode (params )
115
120
return self .request (self .GET , url , None , headers )
116
121
117
122
def head (self , url , headers = None , ** params ):
118
123
"""
119
124
Do a http head request on the given url with given headers and parameters
120
125
Parameters is a dictionary that will will be urlencoded
121
126
"""
122
- if self .append_slash :
123
- url += '/'
124
- url += self .urlencode (params )
127
+ url = self ._append_slash_to (url ) + self .urlencode (params )
125
128
return self .request (self .HEAD , url , None , headers )
126
129
127
- def delete (self , url , headers = None , ** params ):
130
+ def delete (self , url , headers = None , body = None , ** params ):
128
131
"""
129
- Do a http delete request on the given url with given headers and parameters
132
+ Do a http delete request on the given url with given headers, body and parameters
130
133
Parameters is a dictionary that will will be urlencoded
131
134
"""
132
- if self .append_slash :
133
- url += '/'
134
- url += self .urlencode (params )
135
- return self .request (self .DELETE , url , None , headers )
135
+ url = self ._append_slash_to (url ) + self .urlencode (params )
136
+ return self .request (self .DELETE , url , json .dumps (body ), headers , content_type = 'application/json' )
136
137
137
138
def post (self , url , body = None , headers = None , ** params ):
138
139
"""
139
140
Do a http post request on the given url with given body, headers and parameters
140
141
Parameters is a dictionary that will will be urlencoded
141
142
"""
142
- if self .append_slash :
143
- url += '/'
144
- url += self .urlencode (params )
145
- headers ['Content-Type' ] = 'application/json'
146
- return self .request (self .POST , url , json .dumps (body ), headers )
143
+ url = self ._append_slash_to (url ) + self .urlencode (params )
144
+ return self .request (self .POST , url , json .dumps (body ), headers , content_type = 'application/json' )
147
145
148
146
def put (self , url , body = None , headers = None , ** params ):
149
147
"""
150
148
Do a http put request on the given url with given body, headers and parameters
151
149
Parameters is a dictionary that will will be urlencoded
152
150
"""
153
- if self .append_slash :
154
- url += '/'
155
- url += self .urlencode (params )
156
- headers ['Content-Type' ] = 'application/json'
157
- return self .request (self .PUT , url , json .dumps (body ), headers )
151
+ url = self ._append_slash_to (url ) + self .urlencode (params )
152
+ return self .request (self .PUT , url , json .dumps (body ), headers , content_type = 'application/json' )
158
153
159
154
def patch (self , url , body = None , headers = None , ** params ):
160
155
"""
161
156
Do a http patch request on the given url with given body, headers and parameters
162
157
Parameters is a dictionary that will will be urlencoded
163
158
"""
164
- if self .append_slash :
165
- url += '/'
166
- url += self .urlencode (params )
167
- headers ['Content-Type' ] = 'application/json'
168
- return self .request (self .PATCH , url , json .dumps (body ), headers )
159
+ url = self ._append_slash_to (url ) + self .urlencode (params )
160
+ return self .request (self .PATCH , url , json .dumps (body ), headers , content_type = 'application/json' )
169
161
170
- def request (self , method , url , body , headers ):
162
+ def request (self , method , url , body , headers , content_type = None ):
171
163
"""Low-level networking. All HTTP-method methods call this"""
172
164
if headers is None :
173
165
headers = {}
166
+
167
+ if content_type is not None :
168
+ headers ['Content-Type' ] = content_type
169
+
174
170
if self .auth_header is not None :
175
171
headers ['Authorization' ] = self .auth_header
176
172
headers ['User-Agent' ] = self .user_agent
177
173
fancylogger .getLogger ().debug ('cli request: %s, %s, %s, %s' , method , url , body , headers )
178
- #TODO: in recent python: Context manager
174
+ # TODO: in recent python: Context manager
179
175
conn = self .get_connection (method , url , body , headers )
180
176
status = conn .code
181
177
body = conn .read ()
0 commit comments