Last active
June 27, 2016 18:13
-
-
Save WesleyBatista/71ca5841515f81cf8523ea645a8fa0cc to your computer and use it in GitHub Desktop.
A class to get file content from github repos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
HOW TO GET AN ACCESS TOKEN?