annotate lemuriformes/client.py @ 7:92ae11e33f85

abstract API client
author Jeff Hammel <k0scist@gmail.com>
date Sun, 10 Dec 2017 11:54:10 -0800
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 """
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 abstract ReSTful HTTP client
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 # imports
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 import requests
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 import sys
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import time
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 from urlparse import urlparse
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 from .cli import ConfigurationParser
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 def isurl(string):
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 """is `string` a URL?"""
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 return bool(urlparse(string).scheme)
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 def serialize_headers(headers):
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 return '\n'.join(["{key}: {value}".format(key=key, value=value)
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 for key, value in sorted(headers.items(),
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 key=lambda x: x[0])
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 ])
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 def serialize_request(request):
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 """serialize a request object to a string"""
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 template = u"""{method} {url}
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 {headers}
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 {body}
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 """
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 headers = '\n'.join(['{key}: {value}'.format(key=key, value=value)
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 for key, value in request.headers.items()])
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 retval = template.format(method=request.method,
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 url=request.url,
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 headers=headers,
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 body=request.body or '')
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 return retval
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 def serialize_response(response):
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 """serialize a response object to a string"""
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 template = u"""{url}
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 {status_code}
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48 {headers}
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 {text}
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51 """
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 retval = template.format(url=response.url,
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 status_code=response.status_code,
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55 headers=serialize_headers(response.headers),
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 text=response.text.strip())
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57 return retval
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
59
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60 class Endpoint(object):
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
61 """abstract base class for a RESTful API client"""
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
63 path = ''
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
64 endpoints = []
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
65
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
66 def __init__(self, base_url, session=None, timeout=60.):
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
67 base_url = base_url.rstrip('/')
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
68 self.url = '{}/{}'.format(base_url, self.path)
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
69 self.timeout = timeout
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
70 if session is None:
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
71 session = requests.Session()
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
72 self.session = session
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
73 for endpoint in self.endpoints:
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
74 path = endpoint.path
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
75 setattr(self,
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
76 path,
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
77 endpoint(self.url, session=self.session, timeout=timeout))
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
78
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
79 def __call__(self, method, url, **kwargs):
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
80 """make an HTTP request"""
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
81
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
82 kwargs.setdefault('timeout', self.timeout)
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
83 start = time.time()
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
84 response = self.session.request(method, url, **kwargs)
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
85 end = time.time()
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
86 response.duration = end - start
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
87 try:
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
88 response.raise_for_status()
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
89 except requests.HTTPError as e:
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
90 sys.stderr.write(serialize_response(response) + '\n')
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
91 sys.stderr.write("=>\n")
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
92 sys.stderr.write(serialize_request(response.request) + '\n')
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
93 raise
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
94 return response
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
95
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
96 def POST(self, data, **kwargs):
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
97 return self('POST', self.url, data=data, **kwargs)
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
98
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
99
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
100 class ClientParser(ConfigurationParser):
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
101 """abstract argument parser for HTTP client"""
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
102
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
103 client_class = Endpoint
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
104
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
105 def add_arguments(self):
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
106 self.add_argument('base_url', help="base URL")
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
107 self.add_argument('--timeout', dest='timeout',
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
108 type=float, default=60.,
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
109 help='per request timeout in seconds [DEFAULT: %(default)s]')
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
110
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
111 def base_url(self):
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
112 return self.options.base_url
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
113
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
114 def client(self):
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
115 """return argument specified requests HTTP client"""
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
116
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
117 if self.options is None:
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
118 raise Exception("options not yet parsed!")
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
119
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
120 return self.client_class(self.base_url(),
92ae11e33f85 abstract API client
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
121 timeout=self.options.timeout)