Exemple #1
0
    def session(self,
                authentication=None,
                headers=None,
                ssl=None,
                verify_ssl=True):
        """
        A wrapper method around aiohttp.ClientSession.
        
        :param authentication:     A username for the user.
        :type authentication:      ibmpairs.authentication.Basic or ibmpairs.authentication.OAuth2
        :param headers:            A dictionary of request headers.
        :type headers:             dict
        :param ssl:                SSL.
        :type ssl:                 str
        :param verify_ssl:         Verify SSL.
        :type verify_ssl:          bool
        :returns:                  A aiohttp.ClientSession using the attributes provided.
        :rtype:                    aiohttp.ClientSession
        """

        if headers is not None:
            self.set_headers(headers)
            msg = messages.DEBUG_CLIENT_SET_HEADERS.format(headers)
            logger.debug(msg)

        if authentication is not None:
            self.set_authentication(authentication)
            msg = messages.DEBUG_CLIENT_SET_HEADERS.format(authentication)
            logger.debug(msg)

        connector = aiohttp.TCPConnector(ssl=ssl, verify_ssl=verify_ssl)

        if self.authentication_mode(self._authentication) in ['Basic', 'None']:
            # If authentication.Basic then get set authenication tuple.
            if self.authentication_mode(self._authentication) in ['Basic']:
                authentication = aiohttp.BasicAuth(
                    self._authentication.username,
                    self._authentication.password)

            session = aiohttp.ClientSession(connector=connector,
                                            auth=authentication,
                                            headers=self._headers)
        elif self.authentication_mode(self._authentication) in ['OAuth2']:

            # Add bearer token to headers.
            token = 'Bearer ' + self._authentication.jwt_token
            self.append_header('Authorization', token)

            session = aiohttp.ClientSession(connector=connector,
                                            headers=self._headers)
        else:
            msg = messages.ERROR_CLIENT_AUTHENTICATION_MECHANISM.format(
                self.authentication_mode(self._authentication))
            logger.error(msg)
            raise common.PAWException(msg)

        return session
Exemple #2
0
    def get(self, url, headers=None, verify=True):
        """
        A wrapper method around requests.get.
        
        :param url:                A URL to GET.
        :type url:                 str
        :param headers:            A dictionary of request headers.
        :type headers:             dict
        :param verify:             Verify SSL.
        :type verify:              bool
        :returns:                  A requests.Response object.
        :rtype:                    requests.Response
        """

        response = None

        if headers is not None:
            self.set_headers(headers)
            msg = messages.DEBUG_CLIENT_SET_HEADERS.format('GET', headers)
            logger.debug(msg)

        if self.authentication_mode(
                self._authentication) in ['Basic', 'tuple', 'Dict', 'None']:
            # If Basic, construct an authentication tuple.
            if self.authentication_mode(self._authentication) in ['Basic']:
                authentication = self._authentication.get_credentials()

            response = requests.get(url,
                                    auth=authentication,
                                    headers=self._headers,
                                    verify=verify)
        elif self.authentication_mode(self._authentication) in ['OAuth2']:
            token = 'Bearer ' + self._authentication.jwt_token
            self.append_header('Authorization', token)
            response = requests.get(url, headers=self._headers, verify=verify)

            if response.status_code == 403:
                token_refresh_message = constants.CLIENT_TOKEN_REFRESH_MESSAGE
                if response.json() is not None:
                    response_string = json.dumps(response.json())
                    if token_refresh_message in response_string:
                        self._authentication.refresh_auth_token()
                        token = 'Bearer ' + self._authentication.jwt_token
                        self.append_header('Authorization', token)
                        response = requests.get(url,
                                                headers=self._headers,
                                                verify=verify)
        else:
            msg = messages.ERROR_AUTHENTICATION_TYPE_NOT_RECOGNIZED.format(
                type(self._authentication))
            logger.error(msg)
            raise Exception(msg)

        return response
Exemple #3
0
def set_client(input_client, global_client, self_client=None):
    """
    The method selects a ibmpairs.client.Client from a few choices.

    :param input_client:   A client input into the method that calls set_client().
    :type input_client:    ibmpairs.client.Client
    :param global_client:  The global client in the environment.
    :type global_client:   ibmpairs.client.Client 
    :param self_client:    The client held in the object that calls the method that calls set_client().
    :type self_client:     ibmpairs.client.Client
    :returns:              the selected client from the hierarchy.
    :rtype:                ibmpairs.client.Client
    :raises Exception:     If no client is present.
    """

    if input_client is not None:
        msg = messages.DEBUG_CLIENT_PROVIDED_FOUND
        logger.debug(msg)
        return input_client
    elif (input_client is None) and (self_client is not None):
        msg = messages.DEBUG_CLIENT_IN_OBJECT_FOUND
        logger.debug(msg)
        return self_client
    elif (input_client is None) and (self_client is None) and (global_client
                                                               is not None):
        msg = messages.DEBUG_CLIENT_GLOBAL_FOUND
        logger.debug(msg)
        return global_client
    else:
        msg = messages.ERROR_NO_CLIENT
        logger.error(msg)
        raise PAWException(msg)