From 15d5d95aa8ef7bd7315917b01192b025167edbc6 Mon Sep 17 00:00:00 2001 From: Jason Kraus Date: Thu, 26 Apr 2012 14:18:42 -0700 Subject: [PATCH 1/2] added authorizations endpoint --- .gitignore | 7 +- github3/api.py | 8 ++- github3/models.py | 167 ++++++---------------------------------------- 3 files changed, 32 insertions(+), 150 deletions(-) diff --git a/.gitignore b/.gitignore index b802fae..ba0c8d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,6 @@ -toy.py \ No newline at end of file +toy.py +*.py[cdo] +*~ +bin/ +include/ +lib/ diff --git a/github3/api.py b/github3/api.py index b210135..6d5ab10 100644 --- a/github3/api.py +++ b/github3/api.py @@ -38,7 +38,7 @@ def login(self, username, password): """Logs user into Github with given credentials.""" # Attach auth to session. - self._s.auth = (username.password) + self._s.auth = (username, password) return True @@ -113,6 +113,10 @@ def __init__(self): def __repr__(self): return '' % (id(self)) + @property + def authorizations(self): + return self._get_resources(('authorizations'), Authorization) + # @property # def addons(self): # return self._get_resources(('addons'), Addon) @@ -127,4 +131,4 @@ def __repr__(self): class ResponseError(ValueError): - """The API Response was unexpected.""" \ No newline at end of file + """The API Response was unexpected.""" diff --git a/github3/models.py b/github3/models.py index 4380b62..6194cab 100644 --- a/github3/models.py +++ b/github3/models.py @@ -90,135 +90,39 @@ def new_from_dict(cls, d, h=None, **kwargs): return d -class App(BaseResource): - """Heroku App.""" +class Authorization(BaseResource): + """Github Authorization.""" - _strs = ['name', 'create_status', 'stack', 'repo_migrate_status'] - _ints = ['id', 'slug_size', 'repo_size', 'dynos', 'workers'] - _dates = ['created_at',] - _pks = ['name', 'id'] - - def __init__(self): - super(App, self).__init__() + _strs = ['url', 'scopes', 'token', 'note', 'note_url'] + _ints = ['id'] + _dates = ['created_at', 'updated_at'] + _pks = ['id'] def __repr__(self): - return "".format(self.name) + return "".format(self.id) - def new(self, name=None, stack='cedar'): - """Creates a new app.""" + def new(self, scopes=None, note=None, note_url=None): + """Creates a new authorization.""" payload = {} - if name: - payload['app[name]'] = name + if scopes: + payload['scopes'] = scopes - if stack: - payload['app[stack]'] = stack + if note: + payload['note'] = note + + if note_url: + payload['note_url'] = note_url r = self._h._http_resource( method='POST', - resource=('apps',), + resource=('authorizations',), data=payload ) - name = json.loads(r.content).get('name') - return self._h.apps.get(name) - - @property - def addons(self): - return self._h._get_resources( - resource=('apps', self.name, 'addons'), - obj=Addon, app=self - ) - - @property - def collaborators(self): - """The collaborators for this app.""" - return self._h._get_resources( - resource=('apps', self.name, 'collaborators'), - obj=Collaborator, app=self - ) - - @property - def domains(self): - """The domains for this app.""" - return self._h._get_resources( - resource=('apps', self.name, 'domains'), - obj=Domain, app=self - ) - - @property - def releases(self): - """The releases for this app.""" - return self._h._get_resources( - resource=('apps', self.name, 'releases'), - obj=Release, app=self - ) - - @property - def processes(self): - """The proccesses for this app.""" - return self._h._get_resources( - resource=('apps', self.name, 'ps'), - obj=Process, app=self, map=ProcessListResource - ) - - @property - def config(self): - """The envs for this app.""" - - return self._h._get_resource( - resource=('apps', self.name, 'config_vars'), - obj=ConfigVars, app=self - ) - - @property - def info(self): - """Returns current info for this app.""" - - return self._h._get_resource( - resource=('apps', self.name), - obj=App, - ) - - def rollback(self, release): - """Rolls back the release to the given version.""" - r = self._h._http_resource( - method='POST', - resource=('apps', self.name, 'releases'), - data={'rollback': release} - ) - return self.releases[-1] - - - def rename(self, name): - """Renames app to given name.""" - - r = self._h._http_resource( - method='PUT', - resource=('apps', self.name), - data={'app[name]': name} - ) - return r.ok - - def transfer(self, user): - """Transfers app to given username's account.""" - - r = self._h._http_resource( - method='PUT', - resource=('apps', self.name), - data={'app[transfer_owner]': user} - ) - return r.ok - - def maintenance(self, on=True): - """Toggles maintenance mode.""" - - r = self._h._http_resource( - method='POST', - resource=('apps', self.name, 'server', 'maintenance'), - data={'maintenance_mode': int(on)} - ) + _id = json.loads(r.content).get('id') + return self._h.apps.get(_id) return r.ok def destroy(self): @@ -226,38 +130,7 @@ def destroy(self): r = self._h._http_resource( method='DELETE', - resource=('apps', self.name) + resource=('authorizations', self.id) ) return r.ok - def logs(self, num=None, source=None, tail=False): - """Returns the requested log.""" - - # Bootstrap payload package. - payload = {'logplex': 'true'} - - if num: - payload['num'] = num - - if source: - payload['source'] = source - - if tail: - payload['tail'] = 1 - - # Grab the URL of the logplex endpoint. - r = self._h._http_resource( - method='GET', - resource=('apps', self.name, 'logs'), - data=payload - ) - - # Grab the actual logs. - r = requests.get(r.content) - - if not tail: - return r.content - else: - # Return line iterator for tail! - return r.iter_lines() - From a63e3de87c16bac86e2ae7e960702ce8255b123b Mon Sep 17 00:00:00 2001 From: Jason Kraus Date: Thu, 26 Apr 2012 14:45:41 -0700 Subject: [PATCH 2/2] serialize payload data properly --- github3/api.py | 7 +++++-- github3/models.py | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/github3/api.py b/github3/api.py index 6d5ab10..cc850e9 100644 --- a/github3/api.py +++ b/github3/api.py @@ -72,10 +72,13 @@ def _http_resource(self, method, resource, params=None, data=None): if not is_collection(resource): resource = [resource] - + + if data is not None and not isinstance(data, basestring): + data = json.dumps(data) + url = self._url_for(*resource) r = self._s.request(method, url, params=params, data=data) - + r.raise_for_status() return r diff --git a/github3/models.py b/github3/models.py index 6194cab..1bfc2e6 100644 --- a/github3/models.py +++ b/github3/models.py @@ -122,7 +122,7 @@ def new(self, scopes=None, note=None, note_url=None): ) _id = json.loads(r.content).get('id') - return self._h.apps.get(_id) + return self._h.authorizations.get(_id) return r.ok def destroy(self):