|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright (C) 2009 Google Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | + |
| 18 | +"""AtomPubClient provides CRUD ops. in line with the Atom Publishing Protocol. |
| 19 | +
|
| 20 | +""" |
| 21 | + |
| 22 | +__author__ = 'j.s@google.com (Jeff Scudder)' |
| 23 | + |
| 24 | + |
| 25 | +import atom.http_core |
| 26 | + |
| 27 | + |
| 28 | +class AtomPubClient(object): |
| 29 | + host = None |
| 30 | + auth_token = None |
| 31 | + |
| 32 | + def __init__(self, http_client=None, host=None, auth_token=None, *args, |
| 33 | + **kwargs): |
| 34 | + self.http_client = http_client or atom.http_core.HttpClient() |
| 35 | + if host is not None: |
| 36 | + self.host = host |
| 37 | + if auth_token is not None: |
| 38 | + self.auth_token = auth_token |
| 39 | + |
| 40 | + def request(self, method=None, uri=None, auth_token=None, |
| 41 | + http_request=None, *args, **kwargs): |
| 42 | + """Performs an HTTP request to the server indicated. |
| 43 | +
|
| 44 | + Uses the http_client instance to make the request. |
| 45 | +
|
| 46 | + Args: |
| 47 | + method: The HTTP method as a string, usually one of 'GET', 'POST', |
| 48 | + 'PUT', or 'DELETE' |
| 49 | + uri: The URI desired as a string or atom.http_core.Uri. |
| 50 | + http_request: |
| 51 | + auth_token: An authorization token object whose modify_request method |
| 52 | + sets the HTTP Authorization header. |
| 53 | + """ |
| 54 | + if http_request is None: |
| 55 | + http_request = atom.http_core.HttpRequest() |
| 56 | + # If the http_request didn't specify the target host, use the client's |
| 57 | + # default host (if set). |
| 58 | + if self.host is not None and http_request.host is None: |
| 59 | + http_request.host = self.host |
| 60 | + # Modify the request based on the AtomPubClient settings and parameters |
| 61 | + # passed in to the request. |
| 62 | + if isinstance(uri, (str, unicode)): |
| 63 | + uri = atom.http_core.parse_uri(uri) |
| 64 | + if uri is not None: |
| 65 | + uri.modify_request(http_request) |
| 66 | + if isinstance(method, (str, unicode)): |
| 67 | + http_request.method = method |
| 68 | + # Any unrecognized arguments are assumed to be capable of modifying the |
| 69 | + # HTTP request. |
| 70 | + for arg in args: |
| 71 | + arg.modify_request(http_request) |
| 72 | + for name, value in kwargs.iteritems(): |
| 73 | + value.modify_request(http_request) |
| 74 | + # Default to an http request if the protocol scheme is not set. |
| 75 | + if http_request.scheme is None: |
| 76 | + http_request.scheme = 'http' |
| 77 | + # Add the Authorization header at the very end. The Authorization header |
| 78 | + # value may need to be calculated using information in the request. |
| 79 | + if auth_token: |
| 80 | + auth_token.modify_request(http_request) |
| 81 | + elif self.auth_token: |
| 82 | + self.auth_token.modify_request(http_request) |
| 83 | + # Perform the fully specified request using the http_client instance. |
| 84 | + # Sends the request to the server and returns the server's response. |
| 85 | + return self.http_client.request(http_request) |
0 commit comments