Skip to content
Snippets Groups Projects
Unverified Commit 1d497f99 authored by Will Thompson's avatar Will Thompson Committed by GitHub
Browse files

Merge pull request #6 from endlessm/T29306-api-authorization

Fix API authentication
parents 0162f4e1 130fb3f7
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/python
# encoding: utf-8
import base64
import sys
import os
import argparse
from collections import namedtuple
from subprocess import Popen, PIPE
from urllib2 import urlopen
from urllib import urlencode
from urllib2 import Request, urlopen
import json
from urlparse import urlparse
......@@ -15,8 +17,8 @@ WHITELIST=[]
BLACKLIST=[]
LISTINGS_PER_PAGE = 100
ACCESS_TOKEN_PARAM = '?access_token=%s'
LISTING_PAGE_PARAM = '&per_page=%d&page=%d'
LISTINGS_PER_PAGE_PARAM = 'per_page'
LISTING_PAGE_PARAM = 'page'
GITHUB_API_HOST = 'https://api.github.com'
GIT_CLONE_CMD = 'git clone %s %s %s'
......@@ -73,12 +75,23 @@ class AttributeDict(dict):
self[attr] = value
def read_api_uri(uri, config, page=1):
uri += ACCESS_TOKEN_PARAM % config.token + LISTING_PAGE_PARAM % (LISTINGS_PER_PAGE, page)
params = {
LISTINGS_PER_PAGE_PARAM: LISTINGS_PER_PAGE,
LISTING_PAGE_PARAM: page,
}
uri += '?' + urlencode(params)
creds = base64.b64encode(config.token.encode('utf-8')).decode('ascii')
basic_auth = 'Basic {}'.format(creds)
headers = {
'Authorization': basic_auth,
}
if config.debug:
print "Trying:", uri
print "Trying URI {} with headers {}".format(uri, headers)
return urlopen(uri).read()
req = Request(uri, headers=headers)
return urlopen(req).read()
def get_json(uri, config, obj_type=AttributeDict, page=1):
return json.loads(read_api_uri(uri, config, page), object_hook=obj_type)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment