|
25 | 25 | import atom.http_core |
26 | 26 |
|
27 | 27 |
|
| 28 | +class Error(Exception): |
| 29 | + pass |
| 30 | + |
| 31 | + |
| 32 | +class MissingHost(Error): |
| 33 | + pass |
| 34 | + |
| 35 | + |
28 | 36 | class AtomPubClient(object): |
29 | 37 | host = None |
30 | 38 | auth_token = None |
@@ -67,6 +75,10 @@ def request(self, method=None, uri=None, auth_token=None, |
67 | 75 | http_request: |
68 | 76 | auth_token: An authorization token object whose modify_request method |
69 | 77 | sets the HTTP Authorization header. |
| 78 | +
|
| 79 | + Returns: |
| 80 | + The results of calling self.http_client.request. With the default |
| 81 | + http_client, this is an HTTP response object. |
70 | 82 | """ |
71 | 83 | # Modify the request based on the AtomPubClient settings and parameters |
72 | 84 | # passed in to the request. |
@@ -96,48 +108,75 @@ def request(self, method=None, uri=None, auth_token=None, |
96 | 108 | auth_token.modify_request(http_request) |
97 | 109 | elif self.auth_token: |
98 | 110 | self.auth_token.modify_request(http_request) |
| 111 | + # Check to make sure there is a host in the http_request. |
| 112 | + if http_request.uri.host is None: |
| 113 | + raise MissingHost('No host provided in request %s %s' % ( |
| 114 | + http_request.method, str(http_request.uri))) |
99 | 115 | # Perform the fully specified request using the http_client instance. |
100 | 116 | # Sends the request to the server and returns the server's response. |
101 | 117 | return self.http_client.request(http_request) |
102 | 118 |
|
103 | 119 | Request = request |
104 | 120 |
|
105 | 121 | def get(self, uri=None, auth_token=None, http_request=None, **kwargs): |
| 122 | + """Performs a request using the GET method, returns an HTTP response.""" |
106 | 123 | return self.request(method='GET', uri=uri, auth_token=auth_token, |
107 | 124 | http_request=http_request, **kwargs) |
108 | 125 |
|
109 | 126 | Get = get |
110 | 127 |
|
111 | 128 | def post(self, uri=None, data=None, auth_token=None, http_request=None, |
112 | 129 | **kwargs): |
| 130 | + """Sends data using the POST method, returns an HTTP response.""" |
113 | 131 | return self.request(method='POST', uri=uri, auth_token=auth_token, |
114 | 132 | http_request=http_request, data=data, **kwargs) |
115 | 133 |
|
116 | 134 | Post = post |
117 | 135 |
|
118 | 136 | def put(self, uri=None, data=None, auth_token=None, http_request=None, |
119 | 137 | **kwargs): |
| 138 | + """Sends data using the PUT method, returns an HTTP response.""" |
120 | 139 | return self.request(method='PUT', uri=uri, auth_token=auth_token, |
121 | 140 | http_request=http_request, data=data, **kwargs) |
122 | 141 |
|
123 | 142 | Put = put |
124 | 143 |
|
125 | 144 | def delete(self, uri=None, auth_token=None, http_request=None, **kwargs): |
| 145 | + """Performs a request using the DELETE method, returns an HTTP response.""" |
126 | 146 | return self.request(method='DELETE', uri=uri, auth_token=auth_token, |
127 | 147 | http_request=http_request, **kwargs) |
128 | 148 |
|
129 | 149 | Delete = delete |
130 | 150 |
|
131 | 151 | def modify_request(self, http_request): |
| 152 | + """Changes the HTTP request before sending it to the server. |
| 153 | + |
| 154 | + Sets the User-Agent HTTP header and fills in the HTTP host portion |
| 155 | + of the URL if one was not included in the request (for this it uses |
| 156 | + the self.host member if one is set). This method is called in |
| 157 | + self.request. |
| 158 | +
|
| 159 | + Args: |
| 160 | + http_request: An atom.http_core.HttpRequest() (optional) If one is |
| 161 | + not provided, a new HttpRequest is instantiated. |
| 162 | +
|
| 163 | + Returns: |
| 164 | + An atom.http_core.HttpRequest() with the User-Agent header set and |
| 165 | + if this client has a value in its host member, the host in the request |
| 166 | + URL is set. |
| 167 | + """ |
132 | 168 | if http_request is None: |
133 | 169 | http_request = atom.http_core.HttpRequest() |
| 170 | + |
134 | 171 | if self.host is not None and http_request.uri.host is None: |
135 | 172 | http_request.uri.host = self.host |
| 173 | + |
136 | 174 | # Set the user agent header for logging purposes. |
137 | 175 | if self.source: |
138 | | - http_request.headers['User-Agent'] = '%s gdata-py/2.0.5' % self.source |
| 176 | + http_request.headers['User-Agent'] = '%s gdata-py/2.0.6' % self.source |
139 | 177 | else: |
140 | | - http_request.headers['User-Agent'] = 'gdata-py/2.0.5' |
| 178 | + http_request.headers['User-Agent'] = 'gdata-py/2.0.6' |
| 179 | + |
141 | 180 | return http_request |
142 | 181 |
|
143 | 182 | ModifyRequest = modify_request |
0 commit comments