예제 #1
0
    def _post_request(self, url, body):
        """
        Create a HTTP post request.
        """

        access_token = self.connection.access_token
        api_headers = {
            'content-type': 'application/json',
            'Authorization': 'Bearer {0}'.format(access_token)
        }

        response = requests.post(url, headers=api_headers, data=body)

        if response.status_code == 200:
            return json.loads(response.text)

        elif response.status_code == 401:
            raise UnauthorizedClientError(
                'Wrong client secret or/and refresh token', response.text)

        elif response.status_code == 404:
            raise NotFoundClientError('Client ID doesn\'t exist',
                                      response.text)

        elif response.status_code == 400:
            raise WrongParamsError('Some of the parameters were wrong',
                                   response.text)

        elif response.status_code == 500:
            raise InternalServerError('Internal server error', response.text)
예제 #2
0
def post_request(jobs_url, access_token, data):
    """
    Post request
    :param jobs_url:
    :param access_token:
    :param data:
    :return:
    """

    api_headers = {
        'content-type': 'application/json',
        'Authorization': 'Bearer {0}'.format(access_token)
    }

    response = requests.post(jobs_url, headers=api_headers, json=data)

    if response.status_code == 200:
        result = json.loads(response.text)
        return result

    elif response.status_code == 400:
        raise WrongParamsError('Some of the parameters are wrong',
                               response.text)

    elif response.status_code == 401:
        raise InvalidTokenError('Invalid token, try to refresh it',
                                response.text)

    elif response.status_code == 403:
        raise NoPrivilegeError(
            'Forbidden, the user has insufficient privilege', response.text)

    elif response.status_code == 404:
        raise NotFoundItemError('Not found item with ID', response.text)

    elif response.status_code == 498:
        raise ExpiredTokenError('Expired token, try to refresh it',
                                response.text)

    elif response.status_code == 500:
        raise InternalServerError('Internal server error', response.text)

    else:
        raise FyleSDKError('Error: {0}'.format(response.status_code),
                           response.text)
예제 #3
0
    def _get_request(self, url, params):
        """
        Create a HTTP get request.
        """

        access_token = self.connection.access_token
        api_headers = {
            'content-type': 'application/json',
            'Authorization': 'Bearer {0}'.format(access_token)
        }
        api_params = {}

        for k in params:
            # ignore all unused params
            if not params[k] is None:
                p = params[k]

                # convert boolean to lowercase string
                if isinstance(p, bool):
                    p = str(p).lower()

                api_params[k] = p

        response = requests.get(
            url,
            headers=api_headers,
            params=api_params
        )

        if response.status_code == 200:
            return json.loads(response.text)

        elif response.status_code == 401:
            raise UnauthorizedClientError('Wrong client secret or/and refresh token', response.text)

        elif response.status_code == 404:
            raise NotFoundClientError('Client ID doesn\'t exist', response.text)

        elif response.status_code == 400:
            raise WrongParamsError('Some of the parameters were wrong', response.text)

        elif response.status_code == 500:
            raise InternalServerError('Internal server error', response.text)