Ejemplo n.º 1
0
 def get_user_info(self) -> dict:
     # TODO: use threads?
     try:
         return {
             **self._get_user_name_and_id(), "email":
             self._get_primary_email()
         }
     except RequestException:
         raise GithubError("Failed to communicate with the Github API.")
def convert_response_data_to_dictionary(text: str) -> dict:
    try:
        response_data = {}
        for key, value in [param.split("=") for param in text.split("&")]:
            response_data[key] = value
        return response_data
    except ValueError:
        logger.warning("Malformed data received from Github (%s)" % text)
        raise GithubError("Malformed data received from Github")
Ejemplo n.º 3
0
    def _get_access_token(self, code) -> str:
        data = {
            "code": code,
            "client_id": self.client_id,
            "client_secret": self.client_secret
        }
        response = requests.post(f"{GITHUB_OAUTH_URL}/access_token", data=data)

        if response.status_code != 200:
            raise GithubError(
                NON_200_ERROR_MESSAGE.format(response.status_code))

        response_json = convert_response_data_to_dictionary(response.text)
        if "error" in response_json:
            error_message = response_json["error_description"].replace(
                "+", " ")
            raise GithubError(error_message)

        return response_json["access_token"]
Ejemplo n.º 4
0
    def _get_primary_email(self):
        emails_response = requests.get(f"{GITHUB_API_URL}/user/emails",
                                       headers=self.headers)

        # response from github should be a list of dictionaries, this will find the first entry that is both verified
        # and marked as primary (there should only be one).
        primary_email_data = next(
            filter(
                lambda email_data: email_data["primary"] and email_data[
                    "verified"], emails_response.json()), None)

        if not primary_email_data:
            raise GithubError(
                "User does not have a verified email address with Github.")

        return primary_email_data["email"]