예제 #1
0
    def login(self, credentials):
        # This is another special case client, because auth_token is not set yet (this is how we get the token)
        # So do not use the shared base client for this!
        response = requests.post('%slogin' % self.base_url,
                                 json=credentials.to_dict())
        try:
            token_dict = response.json()
            response.raise_for_status()
        except Exception:
            if response.status_code == 401:
                raise AuthenticationException("Invalid credentials")
            raise AuthenticationException("Login failed. Please try logging in using the token. "
                                          "Run login command without username and password parameters.")

        return token_dict.get('access_token')
예제 #2
0
파일: base.py 프로젝트: wheremiah/floyd-cli
 def check_response_status(self, response):
     """
     Check if response is successful. Else raise Exception.
     """
     if not (200 <= response.status_code < 300):
         try:
             message = response.json()["errors"]
         except Exception:
             message = None
         floyd_logger.debug("Error received : status_code: {}, message: {}".format(response.status_code,
                                                                                   message or response.content))
         if response.status_code == 400:
             raise BadRequestException(response)
         elif response.status_code == 401:
             raise AuthenticationException()
         elif response.status_code == 403:
             raise AuthorizationException()
         elif response.status_code == 404:
             raise NotFoundException()
         elif response.status_code == 429:
             raise OverLimitException(response.json().get("message"))
         elif response.status_code == 502:
             raise BadGatewayException()
         elif response.status_code == 504:
             raise GatewayTimeoutException()
         elif 500 <= response.status_code < 600:
             if 'Server under maintenance' in response.content.decode():
                 raise ServerException('Server under maintenance, please try again later.')
             else:
                 raise ServerException()
         else:
             msg = "An error occurred. Server response: {}".format(response.status_code)
             raise FloydException(message=msg)
예제 #3
0
    def get_user(self, access_token, is_apikey=False):
        # This is a special case client, because auth_token is not set yet (this is how we verify it)
        # So do not use the shared base client for this!
        auth = ""
        if is_apikey:
            auth = "apikey {}".format(access_token)
        else:
            auth = "Bearer {}".format(access_token)
        response = requests.get(self.base_url, headers={"Authorization": auth})
        try:
            user_dict = response.json()
            response.raise_for_status()
        except Exception:
            if response.status_code == 401:
                raise AuthenticationException("Invalid Token.\nSee http://docs.floydhub.com/faqs/authentication/ for help")  # noqa
            raise AuthenticationException("Login failed.\nSee http://docs.floydhub.com/faqs/authentication/ for help")

        return User.from_dict(user_dict)
예제 #4
0
    def get_user(self, access_token):
        response = requests.get(self.base_url,
                                headers={"Authorization": "Bearer {}".format(access_token)})
        try:
            user_dict = response.json()
        except Exception:
            raise AuthenticationException("Invalid Token")

        return User.from_dict(user_dict)
예제 #5
0
    def get_user(self, access_token):
        response = requests.get(self.base_url,
                                headers={"Authorization": "Bearer {}".format(access_token)})
        try:
            user_dict = response.json()
        except Exception:
            raise AuthenticationException("Invalid Token.\nSee http://docs.floydhub.com/faqs/authentication/ for help")

        return User.from_dict(user_dict)
예제 #6
0
    def check_response_status(self, response):
        """
        Check if response is successful. Else raise Exception.
        """
        if not (200 <= response.status_code < 300):
            try:
                message = response.json()["errors"]
            except Exception:
                message = None
            floyd_logger.debug("Error received : status_code: {}, message: {}".format(response.status_code,
                                                                                      message or response.content))

            if response.status_code == 401:
                raise AuthenticationException()
            elif response.status_code == 404:
                raise NotFoundException()
            elif response.status_code == 400:
                raise BadRequestException()
            elif response.status_code == 429:
                raise OverLimitException(response.json().get("message"))
            else:
                response.raise_for_status()