mirror of
https://github.com/status-im/PyGithub.git
synced 2026-09-02 03:41:08 +00:00
Merge branch 'topic/RememberHeaders' into develop (#192)
Conflicts: github/Issue.py github/MainClass.py github/PaginatedList.py github/Repository.py github/Requester.py
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
# Copyright 2012 Steve English <steve.english@navetas.com> #
|
||||
# Copyright 2012 Vincent Jacques <vincent@vincent-jacques.net> #
|
||||
# Copyright 2012 Zearin <zearin@gonk.net> #
|
||||
# Copyright 2013 AKFish <akfish@gmail.com> #
|
||||
# Copyright 2013 Ed Jackson <ed.jackson@gmail.com> #
|
||||
# Copyright 2013 Jonathan J Hunt <hunt@braincorporation.com> #
|
||||
# Copyright 2013 Mark Roddy <markroddy@gmail.com> #
|
||||
@@ -63,7 +64,70 @@ class Requester:
|
||||
cls.__httpConnectionClass = httplib.HTTPConnection
|
||||
cls.__httpsConnectionClass = httplib.HTTPSConnection
|
||||
|
||||
#############################################################
|
||||
# For Debug
|
||||
@classmethod
|
||||
def setDebugFlag(cls, flag):
|
||||
cls.DEBUG_FLAG = flag
|
||||
|
||||
@classmethod
|
||||
def setOnCheckMe(cls, onCheckMe):
|
||||
cls.ON_CHECK_ME = onCheckMe
|
||||
|
||||
DEBUG_FLAG = False
|
||||
|
||||
DEBUG_FRAME_BUFFER_SIZE = 1024
|
||||
|
||||
DEBUG_HEADER_KEY = "DEBUG_FRAME"
|
||||
|
||||
ON_CHECK_ME = None
|
||||
|
||||
def NEW_DEBUG_FRAME(self, requestHeader):
|
||||
'''
|
||||
Initialize a debug frame with requestHeader
|
||||
Frame count is updated and will be attached to respond header
|
||||
The structure of a frame: [requestHeader, statusCode, responseHeader, raw_data]
|
||||
Some of them may be None
|
||||
'''
|
||||
if not self.DEBUG_FLAG:
|
||||
return
|
||||
|
||||
new_frame = [requestHeader, None, None, None]
|
||||
if self._frameCount < self.DEBUG_FRAME_BUFFER_SIZE - 1:
|
||||
self._frameBuffer.append(new_frame)
|
||||
else:
|
||||
self._frameBuffer[0] = new_frame
|
||||
|
||||
self._frameCount = len(self._frameBuffer) - 1
|
||||
|
||||
def DEBUG_ON_RESPONSE(self, statusCode, responseHeader, data):
|
||||
'''
|
||||
Update current frame with response
|
||||
Current frame index will be attached to responseHeader
|
||||
'''
|
||||
if not self.DEBUG_FLAG:
|
||||
return
|
||||
|
||||
self._frameBuffer[self._frameCount][1:4] = [statusCode, responseHeader, data]
|
||||
responseHeader[self.DEBUG_HEADER_KEY] = self._frameCount
|
||||
|
||||
def check_me(self, obj):
|
||||
if self.DEBUG_FLAG and self.ON_CHECK_ME is not None:
|
||||
frame = None
|
||||
if self.DEBUG_HEADER_KEY in obj._headers:
|
||||
frame_index = obj._headers[self.DEBUG_HEADER_KEY]
|
||||
frame = self._frameBuffer[frame_index]
|
||||
self.ON_CHECK_ME(obj, frame)
|
||||
|
||||
def _initializeDebugFeature(self):
|
||||
self._frameCount = 0
|
||||
self._frameBuffer = []
|
||||
|
||||
#############################################################
|
||||
|
||||
def __init__(self, login_or_token, password, base_url, timeout, client_id, client_secret, user_agent, per_page):
|
||||
self._initializeDebugFeature()
|
||||
|
||||
if password is not None:
|
||||
login = login_or_token
|
||||
if atLeastPython3:
|
||||
@@ -111,6 +175,9 @@ class Requester:
|
||||
|
||||
def __check(self, status, responseHeaders, output):
|
||||
output = self.__structuredFromJson(output)
|
||||
# Log frame
|
||||
self.DEBUG_ON_RESPONSE(status, responseHeaders, output)
|
||||
|
||||
if status >= 400:
|
||||
raise self.__createException(status, output)
|
||||
return responseHeaders, output
|
||||
@@ -177,6 +244,8 @@ class Requester:
|
||||
if input is not None:
|
||||
requestHeaders["Content-Type"], encoded_input = encode(input)
|
||||
|
||||
self.NEW_DEBUG_FRAME(requestHeaders)
|
||||
|
||||
status, responseHeaders, output = self.__requestRaw(verb, url, requestHeaders, encoded_input)
|
||||
|
||||
if "x-ratelimit-remaining" in responseHeaders and "x-ratelimit-limit" in responseHeaders:
|
||||
|
||||
Reference in New Issue
Block a user