зеркало из https://github.com/github/octokit.py.git
first commit
This commit is contained in:
Коммит
6206d8f947
|
@ -0,0 +1,2 @@
|
|||
*.pyc
|
||||
.test_config.sh
|
|
@ -0,0 +1,3 @@
|
|||
|
||||
from .client import Client
|
||||
from .exceptions import Error, NotFound, Unauthorized
|
|
@ -0,0 +1,35 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
octokit.client
|
||||
~~~~~~~~~~~~~~
|
||||
|
||||
This module contains the main Client class for octokit.py
|
||||
"""
|
||||
|
||||
# https://code.google.com/p/uri-templates/wiki/Implementations
|
||||
|
||||
from .resources import Resource
|
||||
|
||||
import requests
|
||||
|
||||
class Client(Resource):
|
||||
"""The main class for using octokit.py.
|
||||
|
||||
This class accepts as arguments any attributes that can be set on a
|
||||
Requests.Session() object. After instantiation, the session may be modified
|
||||
by accessing the `session` attribute.
|
||||
|
||||
Example usage:
|
||||
|
||||
>>> client = octokit.Client(auth = ('mastahyeti', 'oauth-token'))
|
||||
>>> client.session.proxies = {'http': 'foo.bar:3128'}
|
||||
>>> client.current_user.login
|
||||
'mastahyeti'
|
||||
"""
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
self.session = requests.sessions.Session()
|
||||
self.root_url = 'https://api.github.com'
|
||||
self.schema = {}
|
||||
[setattr(self.session, key, kwargs[key]) for key in kwargs]
|
|
@ -0,0 +1,31 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
octokit.exceptions
|
||||
~~~~~~~~~~~~~~~~~~
|
||||
|
||||
This module contains octokit.py exceptions.
|
||||
"""
|
||||
|
||||
|
||||
class Error(Exception):
|
||||
""" Something went wrong. """
|
||||
|
||||
class NotFound(Error):
|
||||
""" Status 404: The resource wasn't found. """
|
||||
|
||||
class Unauthorized(Error):
|
||||
""" Status 401/403: Not authorized to view the resource """
|
||||
|
||||
# Mapping of status code to Exception
|
||||
STATUS_ERRORS = {
|
||||
404: NotFound,
|
||||
401: Unauthorized,
|
||||
403: Unauthorized
|
||||
}
|
||||
|
||||
def handle_status(status):
|
||||
""" Raise the appropriate error given a status code. """
|
||||
if status == 200:
|
||||
return
|
||||
raise STATUS_ERRORS.get(status, Error)
|
|
@ -0,0 +1,55 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
octokit.resources
|
||||
~~~~~~~~~~~~~~~~~
|
||||
|
||||
This module contains the workhorse of octokit.py, the Resources.
|
||||
"""
|
||||
|
||||
from .exceptions import handle_status
|
||||
|
||||
class Resource(object):
|
||||
"""The workhorse of octokit.py, this class makes the API calls and interprets
|
||||
them into an accessible schema. The API calls and schema parsing are lazy and
|
||||
only happen when an attribute of the resource is requested.
|
||||
"""
|
||||
|
||||
def __init__(self, root_url, session):
|
||||
self.session = session
|
||||
self.root_url = root_url
|
||||
self.schema = {}
|
||||
|
||||
def __getattr__(self, name):
|
||||
self.ensure_schema_loaded()
|
||||
if name in self.schema:
|
||||
return self.schema[name]
|
||||
else:
|
||||
raise handle_status(404)
|
||||
|
||||
def keys(self):
|
||||
self.ensure_schema_loaded()
|
||||
return self.schema.keys()
|
||||
|
||||
def ensure_schema_loaded(self):
|
||||
if not self.schema:
|
||||
self.load_schema()
|
||||
|
||||
def load_schema(self):
|
||||
data = self.fetch_resource(self.root_url)
|
||||
self.schema = self.parse_schema(data)
|
||||
|
||||
def fetch_resource(self, url):
|
||||
response = self.session.get(url)
|
||||
handle_status(response.status_code)
|
||||
return response.json()
|
||||
|
||||
def parse_schema(self, data):
|
||||
schema = {}
|
||||
for key in data:
|
||||
name = key.split('_url')[0]
|
||||
if key.endswith('_url'):
|
||||
schema[name] = Resource(data[key], self.session)
|
||||
else:
|
||||
schema[name] = data[key]
|
||||
return schema
|
|
@ -0,0 +1,19 @@
|
|||
|
||||
from __future__ import print_function
|
||||
import unittest
|
||||
import os
|
||||
|
||||
import octokit
|
||||
|
||||
class OctokitTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
username = os.environ['OCTOKIT_TEST_USER']
|
||||
token = os.environ['OCTOKIT_TEST_TOKEN']
|
||||
auth = (username, token)
|
||||
self.client = octokit.Client(auth=auth)
|
||||
|
||||
def test_authentication(self):
|
||||
assert self.client.current_user.login == 'mastahyeti'
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
|
@ -0,0 +1,28 @@
|
|||
#!/bin/sh
|
||||
|
||||
echo "**************************"
|
||||
echo "Loading test configuration"
|
||||
echo "**************************"
|
||||
config_file="test_config.sh"
|
||||
if [ -e .${config_file} ]
|
||||
then
|
||||
source .${config_file}
|
||||
echo Config loaded.
|
||||
else
|
||||
echo You need to configure $config_file and move it to .${config_file}
|
||||
exit
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo ""
|
||||
echo "*************************"
|
||||
echo "Running tests in python 2"
|
||||
echo "*************************"
|
||||
python2 ./test.py
|
||||
|
||||
echo ""
|
||||
echo ""
|
||||
echo "*************************"
|
||||
echo "Running tests in python 3"
|
||||
echo "*************************"
|
||||
python3 ./test.py
|
|
@ -0,0 +1,4 @@
|
|||
# Move this to .test_config.sh once configured avoid it ending up in the repo
|
||||
|
||||
export OCTOKIT_TEST_USER="username"
|
||||
export OCTOKIT_TEST_TOKEN="oauth token"
|
Загрузка…
Ссылка в новой задаче