Skip to content

Commit 4f7ab2f

Browse files
author
api.jscudder
committed
Adding initial foundation for version two HTTP requests.
1 parent 79fd744 commit 4f7ab2f

13 files changed

Lines changed: 1797 additions & 3 deletions

src/atom/auth.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
# This module is used for version 2 of the Google Data APIs.
19+
20+
21+
__author__ = 'j.s@google.com (Jeff Scudder)'
22+
23+
24+
import base64
25+
26+
27+
class BasicAuth(object):
28+
"""Sets the Authorization header as defined in RFC1945"""
29+
30+
def __init__(self, user_id, password):
31+
self.basic_cookie = base64.encodestring(
32+
'%s:%s' % (user_id, password)).strip()
33+
34+
def modify_request(self, http_request):
35+
http_request.headers['Authorization'] = 'Basic %s' % self.basic_cookie
36+
37+
ModifyRequest = modify_request

src/atom/client.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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)

src/atom/core.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
#
13
# Copyright (C) 2008 Google Inc.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,6 +15,10 @@
1315
# limitations under the License.
1416

1517

18+
# This module is used for version 2 of the Google Data APIs.
19+
# TODO: handle UTF-8 and unicode as done in src/atom/__init__.py
20+
21+
1622
__author__ = 'j.s@google.com (Jeff Scudder)'
1723

1824

@@ -175,6 +181,8 @@ def get_elements(self, tag=None, namespace=None, version=1):
175181
if _qname_matches(tag, namespace, element._qname):
176182
matches.append(element)
177183
return matches
184+
185+
GetElements = get_elements
178186

179187
def get_attributes(self, tag=None, namespace=None, version=1):
180188
"""Find all attributes which match the tag and namespace.
@@ -206,6 +214,8 @@ def get_attributes(self, tag=None, namespace=None, version=1):
206214
if _qname_matches(tag, namespace, qname):
207215
matches.append(XmlAttribute(qname, value))
208216
return matches
217+
218+
GetAttributes = get_attributes
209219

210220
def _harvest_tree(self, tree, version=1):
211221
"""Populates object members from the data in the tree Element."""
@@ -278,6 +288,8 @@ def to_string(self, version=1):
278288
"""Converts this object to XML."""
279289
return ElementTree.tostring(self._to_tree(version))
280290

291+
ToString = to_string
292+
281293
def _become_child(self, tree, version=1):
282294
"""Adds a child element to tree with the XML data in self."""
283295
new_child = ElementTree.Element('')
@@ -353,12 +365,13 @@ def xml_element_from_string(xml_string, target_class,
353365
version: int (optional) The version of the schema which should be used when
354366
converting the XML into an object. The default is 1.
355367
"""
356-
# xml_string = xml_string.encode('UTF-8')
357368
tree = ElementTree.fromstring(xml_string)
358-
#tree = ElementTree.fromstring(unicode(xml_string, encoding))
359369
return _xml_element_from_tree(tree, target_class, version)
360370

361371

372+
XmlElementFromString = xml_element_from_string
373+
374+
362375
def _xml_element_from_tree(tree, target_class, version=1):
363376
if target_class._qname is None:
364377
instance = target_class()

0 commit comments

Comments
 (0)