def request_token( username, password, scopes, user_agent, code_2fa=None, base_url=MainClass.DEFAULT_BASE_URL, timeout=MainClass.DEFAULT_TIMEOUT, client_id=None, client_secret=None, per_page=MainClass.DEFAULT_PER_PAGE): requester = Requester(username, password, base_url, timeout, client_id, client_secret, user_agent, per_page) if code_2fa: request_header = {'x-github-otp': code_2fa} else: request_header = None status, headers, data = requester.requestJson( "POST", "/authorizations", input={"scopes": scopes, "note": str(user_agent)}, headers=request_header) if status == 401 and re.match(r'.*required.*', headers['x-github-otp']): raise Require2FAError() else: data = json.loads(data) return Authorization(requester, headers, data, True)
def get_blame(self, file_path, branch='master'): assert isinstance(file_path, (str, unicode)), file_path requester = Requester( None, None, "https://github.com", MainClass.DEFAULT_TIMEOUT, None, None, self._requester._Requester__userAgent, MainClass.DEFAULT_PER_PAGE, False ) status, headers, body = \ requester._Requester__requestRaw(None, 'GET', '%s/blame/%s/%s' % (self.html_url, branch, file_path), { 'User-Agent': requester._Requester__userAgent }, None) if status >= 400: raise requester._Requester__createException(status, headers, body) return body
def setUp(self): super(BaseTestCase, self).setUp() self.language = LanguageFactory(name='Python') # Mock github requests self.connectionMock = mock.Mock() connectionClass = mock.Mock() connectionClass.return_value = self.connectionMock Requester.injectConnectionClasses(connectionClass, connectionClass)
class ConnectGitHub(object): def __init__(self, client_id=GITHUB_CLIENT_ID, client_secret=GITHUB_CLIENT_SECRET): self.client_id = client_id self.client_secret = client_secret self.requester = Requester( client_id=self.client_id, client_secret=self.client_secret, base_url=GITHUB_BASE_URL, login_or_token=None, password=None, timeout=DEFAULT_TIMEOUT, user_agent=DEFAULT_USER_AGENT, per_page=10, api_preview=False) def authorize_url(self, **kwargs): """First step of the authentication process - Redirect user to request GitHub access. """ kwargs.update({'client_id': self.client_id}) return GITHUB_AUTHORIZE_URL + "?" + urllib.urlencode(kwargs) def get_access_token(self, code): """Second step of the authentication process - GitHub redirects back to your site. A temporary `code` is given after the first step is successfully completed. Use the code to request an access token for the user. """ params = {'client_id': self.client_id, 'client_secret': self.client_secret, 'code': code} headers, data = self.requester.requestJsonAndCheck( verb="POST", url=GITHUB_TOKEN_URL, parameters=params) return data['data'].split('&')[0].split('=')[-1]
def __init__(self, client_id=GITHUB_CLIENT_ID, client_secret=GITHUB_CLIENT_SECRET): self.client_id = client_id self.client_secret = client_secret self.requester = Requester( client_id=self.client_id, client_secret=self.client_secret, base_url=BASE_URL, login_or_token=None, password=None, timeout=DEFAULT_TIMEOUT, user_agent=DEFAULT_USER_AGENT, per_page=0)
def setup(self, request_environ=None): if request_environ is not None: self.config.set_request_environ({k: v for k, v in request_environ.iteritems() if k.startswith(Config.ENV_PREFIX)}) log_file = self.config.get(DEFAULTSECT, "log_file") if log_file: if not os.path.exists(os.path.dirname(log_file)): os.makedirs(os.path.dirname(log_file)) log.info(logging.root.handlers) del logging.root.handlers[:] logging.root.addHandler(FileHandler(log_file, 'a')) if self.config.get(DEFAULTSECT, "debug", False): log_level = logging.DEBUG else: log_level = self.config.get(DEFAULTSECT, 'log_level', 'INFO').upper() logging.root.setLevel(log_level) logging_config_file = self.config.get(DEFAULTSECT, "logging_config_file") if logging_config_file: fileConfig(logging_config_file, disable_existing_loggers=False) if self.config.get(DEFAULTSECT, "debug", False): keys = request_environ.keys() keys.sort() for key in keys: log.debug('%s: %s', key, repr(request_environ[key])) log.info('Starting Captain Hooks.') # Fix Github client to use the genvent concurrent HTTP client classes Requester.injectConnectionClasses(CachingHTTPConnection, CachingHTTPSConnection) self.__flask = Flask(__name__) services.get(DatabaseService).boot(self) services.get(WebHookEndpoint).boot(self) services.get(ApiEndpoint).boot(self) return self.__flask
def _get_rate_limit_and_wait(obj: Requester): headers, data = obj.requestJsonAndCheck("GET", "/rate_limit") if data is None: raise ValueError('got no data from request') limits = RateLimit(obj, headers, data["resources"], True) reset = _get_last_reset_datetime_utc(limits) now = datetime.now(timezone.utc) diff = reset - now seconds = diff.total_seconds() current_tz = datetime.utcnow().astimezone().tzinfo logger.info(f"Rate limit exceeded") logger.info(f"Reset is at {reset.astimezone(current_tz)}.") if seconds > 0.0: # Extra 10s to ensure it's ready logger.info( f"Waiting for {diff + timedelta(seconds=RATE_LIMIT_PAD_SECONDS)}") time.sleep(seconds + RATE_LIMIT_PAD_SECONDS) logger.info(f"Done waiting - resuming at {datetime.now()}")
def tearDown(self): super(BaseTestCase, self).tearDown() Requester.resetConnectionClasses()
secret_path = Path( __file__ ).parent.parent / 'src' / 'conductor' / 'secrets' / 'db' / 'connections' if 'CI' in environ: secret_path = Path.cwd() / 'secrets' / 'db' / 'connections' SECRETS = json.loads(secret_path.read_text(encoding='utf-8')) REQUESTER = Requester( login_or_token='token', password=None, jwt=None, base_url='http://gis.utah.gov', timeout=100, user_agent='pytest-conductor', per_page=25, verify=False, retry=0, pool_size=1, ) def test_imports(): assert conductor is not None def test_can_extract_metadata_from_issue_body(): headers = {} attributes = { 'body':