This repository was archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 216
Expand file tree
/
Copy pathstd.py
More file actions
55 lines (41 loc) · 1.67 KB
/
std.py
File metadata and controls
55 lines (41 loc) · 1.67 KB
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
from six.moves import urllib
import requests
from consul import base
__all__ = ['Consul']
class HTTPClient(object):
def __init__(self, host='127.0.0.1', port=8500, scheme='http',
verify=True):
self.host = host
self.port = port
self.scheme = scheme
self.verify = verify
self.base_uri = '%s://%s:%s' % (self.scheme, self.host, self.port)
self.session = requests.session()
def response(self, response):
response.encoding = 'utf-8'
return base.Response(
response.status_code, response.headers, response.text)
def uri(self, path, params=None):
uri = self.base_uri + path
if not params:
return uri
return '%s?%s' % (uri, urllib.parse.urlencode(params))
def get(self, callback, path, params=None):
uri = self.uri(path, params)
return callback(self.response(
self.session.get(uri, verify=self.verify)))
def put(self, callback, path, params=None, data=''):
uri = self.uri(path, params)
return callback(self.response(
self.session.put(uri, data=data, verify=self.verify)))
def delete(self, callback, path, params=None):
uri = self.uri(path, params)
return callback(self.response(
self.session.delete(uri, verify=self.verify)))
def post(self, callback, path, params=None, data=''):
uri = self.uri(path, params)
return callback(self.response(
self.session.post(uri, data=data, verify=self.verify)))
class Consul(base.Consul):
def connect(self, host, port, scheme, verify=True):
return HTTPClient(host, port, scheme, verify)