Skip to content

Instantly share code, notes, and snippets.

@WesleyBatista
Last active June 27, 2016 18:13
Show Gist options
  • Save WesleyBatista/71ca5841515f81cf8523ea645a8fa0cc to your computer and use it in GitHub Desktop.
Save WesleyBatista/71ca5841515f81cf8523ea645a8fa0cc to your computer and use it in GitHub Desktop.
A class to get file content from github repos
import requests
from requests.auth import HTTPBasicAuth
class GithubProvider(object):
"""docstring for GithubProvider"""
def __init__(self, repo, username, secret_key):
super(GithubProvider, self).__init__()
self._repo = repo
self._username = username
self.__secret_key = secret_key
@property
def __auth(self):
return HTTPBasicAuth(self._username, self.__secret_key)
@property
def base_url(self):
base_url = 'https://api.github.com/repos/{}/contents'.format(self._repo)
return base_url
def _get_file_uri(self, file_path):
uri = "{}/{}".format(self.base_url, file_path)
return uri
def _request_url(self, url):
request = requests.get(url, auth=self.__auth)
return request
def _get_request(self, file_path):
url = self._get_file_uri(file_path)
request = self._request_url(url)
return request
def _get_request_json(self, request):
return request.json()
def _get_request_text(self, request):
return request.text
def _get_download_url(self, request):
request_json = self._get_request_json(request)
download_url = request_json.get("download_url")
return download_url
def get_file_request(self, file_path):
request = self._get_request(file_path)
download_url = self._get_download_url(request)
file_request = self._request_url(download_url)
return file_request
def get_file_content(self, file_path):
file_request = self.get_file_request(file_path)
file_content = self._get_request_text(file_request)
return file_content
@WesleyBatista
Copy link
Author

WesleyBatista commented Jun 27, 2016

HOW TO GET AN ACCESS TOKEN?

  1. Logged with your GitHub account you can navigate to https://github.com/settings/tokens and click on "Generate new token".
  2. Give the proper permissions. I gave these ones:

captura de tela 2016-06-27 as 14 54 58

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment