Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 1 | # Copyright 2010 Google Inc. All Rights Reserved. |
| 2 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 3 | """Classes to encapsulate a single HTTP request. |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 4 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 5 | The classes implement a command pattern, with every |
| 6 | object supporting an execute() method that does the |
| 7 | actuall HTTP request. |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 8 | """ |
| 9 | |
| 10 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 11 | __all__ = [ |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 12 | 'HttpRequest', 'RequestMockBuilder', 'HttpMock' |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 13 | ] |
| 14 | |
Joe Gregorio | c672246 | 2010-12-20 14:29:28 -0500 | [diff] [blame] | 15 | import httplib2 |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 16 | import os |
| 17 | |
Joe Gregorio | 89174d2 | 2010-12-20 14:37:36 -0500 | [diff] [blame] | 18 | from model import JsonModel |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 19 | from errors import HttpError |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 20 | |
| 21 | |
| 22 | class HttpRequest(object): |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 23 | """Encapsulates a single HTTP request. |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 24 | """ |
| 25 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 26 | def __init__(self, http, postproc, uri, |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 27 | method='GET', |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 28 | body=None, |
| 29 | headers=None, |
Joe Gregorio | abda96f | 2011-02-11 20:19:33 -0500 | [diff] [blame] | 30 | methodId=None): |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 31 | """Constructor for an HttpRequest. |
| 32 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 33 | Args: |
| 34 | http: httplib2.Http, the transport object to use to make a request |
Joe Gregorio | abda96f | 2011-02-11 20:19:33 -0500 | [diff] [blame] | 35 | postproc: callable, called on the HTTP response and content to transform |
| 36 | it into a data object before returning, or raising an exception |
| 37 | on an error. |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 38 | uri: string, the absolute URI to send the request to |
| 39 | method: string, the HTTP method to use |
| 40 | body: string, the request body of the HTTP request |
| 41 | headers: dict, the HTTP request headers |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 42 | methodId: string, a unique identifier for the API method being called. |
| 43 | """ |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 44 | self.uri = uri |
| 45 | self.method = method |
| 46 | self.body = body |
| 47 | self.headers = headers or {} |
| 48 | self.http = http |
| 49 | self.postproc = postproc |
| 50 | |
| 51 | def execute(self, http=None): |
| 52 | """Execute the request. |
| 53 | |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 54 | Args: |
| 55 | http: httplib2.Http, an http object to be used in place of the |
| 56 | one the HttpRequest request object was constructed with. |
| 57 | |
| 58 | Returns: |
| 59 | A deserialized object model of the response body as determined |
| 60 | by the postproc. |
| 61 | |
| 62 | Raises: |
| 63 | apiclient.errors.HttpError if the response was not a 2xx. |
| 64 | httplib2.Error if a transport error has occured. |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 65 | """ |
| 66 | if http is None: |
| 67 | http = self.http |
| 68 | resp, content = http.request(self.uri, self.method, |
| 69 | body=self.body, |
| 70 | headers=self.headers) |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 71 | |
| 72 | if resp.status >= 300: |
| 73 | raise HttpError(resp, content, self.uri) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 74 | return self.postproc(resp, content) |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 75 | |
| 76 | |
| 77 | class HttpRequestMock(object): |
| 78 | """Mock of HttpRequest. |
| 79 | |
| 80 | Do not construct directly, instead use RequestMockBuilder. |
| 81 | """ |
| 82 | |
| 83 | def __init__(self, resp, content, postproc): |
| 84 | """Constructor for HttpRequestMock |
| 85 | |
| 86 | Args: |
| 87 | resp: httplib2.Response, the response to emulate coming from the request |
| 88 | content: string, the response body |
| 89 | postproc: callable, the post processing function usually supplied by |
| 90 | the model class. See model.JsonModel.response() as an example. |
| 91 | """ |
| 92 | self.resp = resp |
| 93 | self.content = content |
| 94 | self.postproc = postproc |
| 95 | if resp is None: |
Joe Gregorio | c672246 | 2010-12-20 14:29:28 -0500 | [diff] [blame] | 96 | self.resp = httplib2.Response({'status': 200, 'reason': 'OK'}) |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 97 | if 'reason' in self.resp: |
| 98 | self.resp.reason = self.resp['reason'] |
| 99 | |
| 100 | def execute(self, http=None): |
| 101 | """Execute the request. |
| 102 | |
| 103 | Same behavior as HttpRequest.execute(), but the response is |
| 104 | mocked and not really from an HTTP request/response. |
| 105 | """ |
| 106 | return self.postproc(self.resp, self.content) |
| 107 | |
| 108 | |
| 109 | class RequestMockBuilder(object): |
| 110 | """A simple mock of HttpRequest |
| 111 | |
| 112 | Pass in a dictionary to the constructor that maps request methodIds to |
| 113 | tuples of (httplib2.Response, content) that should be returned when that |
| 114 | method is called. None may also be passed in for the httplib2.Response, in |
| 115 | which case a 200 OK response will be generated. |
| 116 | |
| 117 | Example: |
| 118 | response = '{"data": {"id": "tag:google.c...' |
| 119 | requestBuilder = RequestMockBuilder( |
| 120 | { |
| 121 | 'chili.activities.get': (None, response), |
| 122 | } |
| 123 | ) |
| 124 | apiclient.discovery.build("buzz", "v1", requestBuilder=requestBuilder) |
| 125 | |
| 126 | Methods that you do not supply a response for will return a |
| 127 | 200 OK with an empty string as the response content. The methodId |
| 128 | is taken from the rpcName in the discovery document. |
| 129 | |
| 130 | For more details see the project wiki. |
| 131 | """ |
| 132 | |
| 133 | def __init__(self, responses): |
| 134 | """Constructor for RequestMockBuilder |
| 135 | |
| 136 | The constructed object should be a callable object |
| 137 | that can replace the class HttpResponse. |
| 138 | |
| 139 | responses - A dictionary that maps methodIds into tuples |
| 140 | of (httplib2.Response, content). The methodId |
| 141 | comes from the 'rpcName' field in the discovery |
| 142 | document. |
| 143 | """ |
| 144 | self.responses = responses |
| 145 | |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 146 | def __call__(self, http, postproc, uri, method='GET', body=None, |
Joe Gregorio | abda96f | 2011-02-11 20:19:33 -0500 | [diff] [blame] | 147 | headers=None, methodId=None): |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 148 | """Implements the callable interface that discovery.build() expects |
| 149 | of requestBuilder, which is to build an object compatible with |
| 150 | HttpRequest.execute(). See that method for the description of the |
| 151 | parameters and the expected response. |
| 152 | """ |
| 153 | if methodId in self.responses: |
| 154 | resp, content = self.responses[methodId] |
| 155 | return HttpRequestMock(resp, content, postproc) |
| 156 | else: |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 157 | model = JsonModel(False) |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 158 | return HttpRequestMock(None, '{}', model.response) |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 159 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 160 | |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 161 | class HttpMock(object): |
| 162 | """Mock of httplib2.Http""" |
| 163 | |
Joe Gregorio | ec34365 | 2011-02-16 16:52:51 -0500 | [diff] [blame] | 164 | def __init__(self, filename, headers=None): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 165 | """ |
| 166 | Args: |
| 167 | filename: string, absolute filename to read response from |
| 168 | headers: dict, header to return with response |
| 169 | """ |
Joe Gregorio | ec34365 | 2011-02-16 16:52:51 -0500 | [diff] [blame] | 170 | if headers is None: |
| 171 | headers = {'status': '200 OK'} |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 172 | f = file(filename, 'r') |
| 173 | self.data = f.read() |
| 174 | f.close() |
| 175 | self.headers = headers |
| 176 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 177 | def request(self, uri, |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 178 | method='GET', |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 179 | body=None, |
| 180 | headers=None, |
| 181 | redirections=1, |
| 182 | connection_type=None): |
Joe Gregorio | cb8103d | 2011-02-11 23:20:52 -0500 | [diff] [blame] | 183 | return httplib2.Response(self.headers), self.data |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 184 | |
| 185 | |
| 186 | class HttpMockSequence(object): |
| 187 | """Mock of httplib2.Http |
| 188 | |
| 189 | Mocks a sequence of calls to request returning different responses for each |
| 190 | call. Create an instance initialized with the desired response headers |
| 191 | and content and then use as if an httplib2.Http instance. |
| 192 | |
| 193 | http = HttpMockSequence([ |
| 194 | ({'status': '401'}, ''), |
| 195 | ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'), |
| 196 | ({'status': '200'}, 'echo_request_headers'), |
| 197 | ]) |
| 198 | resp, content = http.request("http://examples.com") |
| 199 | |
| 200 | There are special values you can pass in for content to trigger |
| 201 | behavours that are helpful in testing. |
| 202 | |
| 203 | 'echo_request_headers' means return the request headers in the response body |
| 204 | 'echo_request_body' means return the request body in the response body |
| 205 | """ |
| 206 | |
| 207 | def __init__(self, iterable): |
| 208 | """ |
| 209 | Args: |
| 210 | iterable: iterable, a sequence of pairs of (headers, body) |
| 211 | """ |
| 212 | self._iterable = iterable |
| 213 | |
| 214 | def request(self, uri, |
| 215 | method='GET', |
| 216 | body=None, |
| 217 | headers=None, |
| 218 | redirections=1, |
| 219 | connection_type=None): |
| 220 | resp, content = self._iterable.pop(0) |
| 221 | if content == 'echo_request_headers': |
| 222 | content = headers |
| 223 | elif content == 'echo_request_body': |
| 224 | content = body |
| 225 | return httplib2.Response(resp), content |