diff --git a/github/Repository.py b/github/Repository.py index fca9d8f2..97523f1b 100644 --- a/github/Repository.py +++ b/github/Repository.py @@ -1067,9 +1067,6 @@ class Repository(github.GithubObject.GithubObject): assert isinstance(callback, (str, unicode)), callback assert secret is github.GithubObject.NotSet or isinstance(secret, (str, unicode)), secret - boundary = "----------------------------3c3ba8b523b2" - eol = "\r\n" - post_parameters = { "hub.mode": mode, "hub.topic": "https://github.com/" + self._full_name + "/events/" + event, @@ -1078,25 +1075,11 @@ class Repository(github.GithubObject.GithubObject): if secret is not github.GithubObject.NotSet: post_parameters["hub.secret"] = secret - input = "" - for name, value in post_parameters.iteritems(): - input += "--" + boundary + eol - input += "Content-Disposition: form-data; name=\"" + name + "\"" + eol - input += eol - input += value + eol - input += "--" + boundary + "--" + eol - - requestHeaders = { - "Authorization": "Basic amFjcXVldjY6Y2xhdmllcg==", - "Content-Type": "multipart/form-data; boundary=" + boundary, - "Content-Length": len(input), - } - - responseHeaders, output = self._requester.requestRawAndCheck( + responseHeaders, output = self._requester.requestMultipartAndCheck( "POST", "/hub", - requestHeaders, - input, + None, + post_parameters, ) @property diff --git a/github/Requester.py b/github/Requester.py index 60951cb5..448b769f 100644 --- a/github/Requester.py +++ b/github/Requester.py @@ -80,6 +80,13 @@ class Requester: raise GithubException.GithubException(status, output) return headers, output + def requestMultipartAndCheck(self, verb, url, requestHeaders, input): + status, headers, output = self.requestMultipart(verb, url, requestHeaders, input) + output = self.__structuredFromJson(output) + if status >= 400: + raise GithubException.GithubException(status, output) + return headers, output + def __structuredFromJson(self, data): if len(data) == 0: return None @@ -109,12 +116,41 @@ class Requester: return status, responseHeaders, output - def requestRawAndCheck(self, verb, url, requestHeaders, input): - status, headers, output = self.requestRaw(verb, url, requestHeaders, input) - output = self.__structuredFromJson(output) - if status >= 400: - raise GithubException.GithubException(status, output) - return headers, output + def requestMultipart(self, verb, url, parameters, input): + assert verb in ["HEAD", "GET", "POST", "PATCH", "PUT", "DELETE"] + if parameters is None: + parameters = dict() + + requestHeaders = dict() + self.__authenticate(requestHeaders, parameters) + if self.__userAgent is not None: + requestHeaders["User-Agent"] = self.__userAgent + + url = self.__makeAbsoluteUrl(url) + url = self.__addParametersToUrl(url, parameters) + + boundary = "----------------------------3c3ba8b523b2" + eol = "\r\n" + + encoded_input = "" + if input is not None: + requestHeaders["Content-Type"] = "multipart/form-data; boundary=" + boundary + + for name, value in input.iteritems(): + encoded_input += "--" + boundary + eol + encoded_input += "Content-Disposition: form-data; name=\"" + name + "\"" + eol + encoded_input += eol + encoded_input += value + eol + encoded_input += "--" + boundary + "--" + eol + + requestHeaders["Content-Length"] = len(encoded_input) + + status, responseHeaders, output = self.requestRaw(verb, url, requestHeaders, encoded_input) + + if "x-ratelimit-remaining" in responseHeaders and "x-ratelimit-limit" in responseHeaders: + self.rate_limiting = (int(responseHeaders["x-ratelimit-remaining"]), int(responseHeaders["x-ratelimit-limit"])) + + return status, responseHeaders, output def requestRaw(self, verb, url, requestHeaders, input): cnx = self.__createConnection()