|
28 | 28 | import StringIO |
29 | 29 |
|
30 | 30 |
|
| 31 | +USER_AGENT = '%s GData-Python/1.2.0' |
| 32 | + |
| 33 | + |
31 | 34 | class Error(Exception): |
32 | 35 | pass |
33 | 36 |
|
@@ -84,3 +87,54 @@ def read(self, amt=None): |
84 | 87 | return self._body.read() |
85 | 88 | else: |
86 | 89 | return self._body.read(amt) |
| 90 | + |
| 91 | + |
| 92 | +class GenericHttpClient(object): |
| 93 | + def __init__(self, http_client, headers=None): |
| 94 | + """ |
| 95 | + |
| 96 | + Args: |
| 97 | + http_client: An object which provides a request method to make an HTTP |
| 98 | + request. The request method in GenericHttpClient performs a |
| 99 | + call-through to the contained HTTP client object. |
| 100 | + headers: A dictionary containing HTTP headers which should be included |
| 101 | + in every HTTP request. Common persistent headers include |
| 102 | + 'User-Agent'. |
| 103 | + """ |
| 104 | + self.http_client = http_client |
| 105 | + self.headers = headers or {} |
| 106 | + |
| 107 | + def request(self, operation, url, data=None, headers=None): |
| 108 | + all_headers = self.headers.copy() |
| 109 | + if headers: |
| 110 | + all_headers.update(headers) |
| 111 | + return self.http_client.request(operation, url, data=data, |
| 112 | + headers=all_headers) |
| 113 | + |
| 114 | + def get(self, url, headers=None): |
| 115 | + return self.request('GET', url, headers=headers) |
| 116 | + |
| 117 | + def post(self, url, data, headers=None): |
| 118 | + return self.request('POST', url, data=data, headers=headers) |
| 119 | + |
| 120 | + def put(self, url, data, headers=None): |
| 121 | + return self.request('PUT', url, data=data, headers=headers) |
| 122 | + |
| 123 | + def delete(self, url, headers=None): |
| 124 | + return self.request('DELETE', url, headers=headers) |
| 125 | + |
| 126 | + |
| 127 | +class GenericToken(object): |
| 128 | + """Represents an Authorization token to be added to HTTP requests. |
| 129 | + |
| 130 | + Some Authorization headers included calculated fields (digital |
| 131 | + signatures for example) which are based on the parameters of the HTTP |
| 132 | + request. Therefore the token is responsible for signing the request |
| 133 | + and adding the Authorization header. |
| 134 | + """ |
| 135 | + def perform_request(self, http_client, operation, url, data=None, |
| 136 | + headers=None): |
| 137 | + """For the GenericToken, no Authorization token is set.""" |
| 138 | + return http_client.request(operation, url, data=data, headers=headers) |
| 139 | + |
| 140 | + |
0 commit comments