Fetching data fixed by issuing GET requests instead + date for commits properly parsed.

This commit is contained in:
Ask Solem
2009-04-21 14:45:16 +02:00
parent a96ccd4531
commit 1069b99afc
4 changed files with 50 additions and 12 deletions
+4 -2
View File
@@ -6,8 +6,10 @@ from github2.commits import Commits
class Github(object):
def __init__(self, username, api_token):
self.request = GithubRequest(username=username, api_token=api_token)
def __init__(self, username, api_token, debug=False):
self.debug = debug
self.request = GithubRequest(username=username, api_token=api_token,
debug=self.debug)
self.issues = Issues(self.request)
self.users = Users(self.request)
self.repos = Repositories(self.request)
+2 -2
View File
@@ -7,8 +7,8 @@ class Commit(BaseData):
url = Attribute("Canonical URL for this commit.")
author = Attribute("Author metadata (dict with name/email.)")
id = Attribute("Commit ID.")
committed_date = DateAttribute("Date committed.")
authored_data = DateAttribute("Date authored.")
committed_date = DateAttribute("Date committed.", format="commit")
authored_data = DateAttribute("Date authored.", format="commit")
tree = Attribute("Tree SHA for this commit.")
committer = Attribute("Comitter metadata (dict with name/email.)")
+31 -3
View File
@@ -2,6 +2,9 @@ from datetime import datetime
GITHUB_TIMEZONE = "-0700"
GITHUB_DATE_FORMAT = "%Y/%m/%d %H:%M:%S"
#2009-03-21T18:01:48-07:00
COMMIT_DATE_FORMAT = "%Y-%m-%dT%H:%M:%S"
def ghdate_to_datetime(github_date):
@@ -14,6 +17,16 @@ def datetime_to_ghdate(datetime_):
return " ".join([date_without_tz, GITHUB_TIMEZONE])
def commitdate_to_datetime(commit_date):
date_without_tz = commit_date[:-6]
return datetime.strptime(date_without_tz, COMMIT_DATE_FORMAT)
def datetime_to_commitdate(datetime_):
date_without_tz = datetime_.strftime(COMMIT_DATE_FORMAT)
return "".join([date_without_tz, GITHUB_TIMEZONE])
class GithubCommand(object):
def __init__(self, request):
@@ -72,15 +85,30 @@ class Attribute(object):
class DateAttribute(Attribute):
format = "github"
converter_for_format = {
"github": {
"to": ghdate_to_datetime,
"from": datetime_to_ghdate,
},
"commit": {
"to": commitdate_to_datetime,
"from": datetime_to_commitdate,
},
}
def __init__(self, *args, **kwargs):
self.format = kwargs.pop("format", self.format)
super(DateAttribute, self).__init__(*args, **kwargs)
def to_python(self, value):
if value and not isinstance(value, datetime):
return ghdate_to_datetime(value)
return self.converter_for_format[self.format]["to"](value)
return value
def from_python(self, value):
if value and isinstance(value, datetime):
return datetime_to_ghdate(value)
return self.converter_for_format[self.format]["from"](value)
return value
@@ -105,7 +133,7 @@ class BaseDataType(type):
for attr_name, attr_value in kwargs.items():
if attr_name not in self._meta:
raise TypeError("%s.__init__() doesn't support the "
"%s argument." % (cls_name, attr_name))
"%s argument." % (name, attr_name))
attr = self._meta[attr_name]
setattr(self, attr_name, attr.to_python(attr_value))
+13 -5
View File
@@ -23,10 +23,11 @@ class GithubRequest(object):
"https": httplib.HTTPSConnection,
}
def __init__(self, username, api_token, url_prefix=None):
def __init__(self, username, api_token, url_prefix=None, debug=False):
self.username = username
self.api_token = api_token
self.url_prefix = url_prefix
self.debug = debug
if not self.url_prefix:
self.url_prefix = self.url_format % {
"github_url": self.github_url,
@@ -55,15 +56,22 @@ class GithubRequest(object):
def raw_request(self, url, extra_post_data):
resource = urlparse(url)
post_data = self.encode_authentication_data(extra_post_data)
connector = self.connector_for_scheme[resource.scheme]
post_data = None
headers = self.http_headers
headers["Accept"] = "text/html"
headers["Content-Length"] = str(len(post_data))
method = "GET"
if extra_post_data:
post_data = self.encode_authentication_data(extra_post_data)
headers["Content-Length"] = str(len(post_data))
method = "POST"
connector = self.connector_for_scheme[resource.scheme]
connection = connector(resource.hostname, resource.port)
connection.request("POST", resource.path, post_data, headers)
connection.request("GET", resource.path, post_data, headers)
response = connection.getresponse()
response_text = response.read().encode("utf-8")
if self.debug:
sys.stderr.write("URL:[%s] POST_DATA:%s RESPONSE_TEXT: [%s]\n" % (
url, post_data, response_text))
json = simplejson.loads(response_text)
if json.get("error"):
raise self.GithubError(json["error"][0]["error"])