コード例 #1
0
ファイル: base.py プロジェクト: SUNILKUMARVADLA/Basic
    def delete(self):
        if not self.id:
            raise InvalidRequestError(
                'Cannot delete a {resource_type} resource without an '
                'identifier'.format(resource_type=self._name))

        return PlivoGenericResponse(
            self.client.send_request(self.__resource_uri, method='DELETE'))
コード例 #2
0
ファイル: base.py プロジェクト: SUNILKUMARVADLA/Basic
    def get(self, params=None):
        if not self.id:
            raise InvalidRequestError(
                'Cannot get a {resource_type} resource without an '
                'identifier'.format(resource_type=self._name))

        self.__resource_json = self.client.send_request(
            self.__resource_uri, data=params)
        self.__parse_json()
        return self
コード例 #3
0
ファイル: base.py プロジェクト: SUNILKUMARVADLA/Basic
    def create(self, params):
        if self.id:
            raise InvalidRequestError(
                'Cannot create a {resource_type} resource because another'
                ' {resource_type} resource exists with the same '
                'identifier: {identifier}.'.format(
                    resource_type=self._name, identifier=self.id))

        id_string = None
        if self._identifier_string:
            id_string = self._identifier_string

        return PlivoGenericResponse(
            self.client.send_request(
                self.__resource_uri, data=params, method='POST'), id_string)
コード例 #4
0
ファイル: base.py プロジェクト: SUNILKUMARVADLA/Basic
    def _update(self, params):
        if not self.id:
            raise InvalidRequestError(
                'Cannot update a {resource_type} resource without an '
                'identifier'.format(resource_type=self._name))

        response_json = self.client.send_request(
            self.__resource_uri, method='POST', data=params)

        for key in params:
            self.__dict__[key] = params[key]

        for key in response_json:
            self.__dict__[key] = response_json[key]

        return self
コード例 #5
0
ファイル: client.py プロジェクト: SUNILKUMARVADLA/Basic
    def process_response(self,
                         method,
                         response,
                         response_type=None,
                         objects_type=None):
        """Processes the API response based on the status codes and method used
        to access the API
        """

        try:
            response_json = response.json(object_hook=lambda x: ResponseObject(
                x) if isinstance(x, dict) else x)
            if response_type:
                r = response_type(self, response_json.__dict__)
                response_json = r

            if 'objects' in response_json and objects_type:
                response_json.objects = [
                    objects_type(self, obj.__dict__)
                    for obj in response_json.objects
                ]
        except ValueError:
            response_json = None

        if response.status_code == 400:
            if response_json and 'error' in response_json:
                raise ValidationError(response_json.error)
            raise ValidationError(
                'A parameter is missing or is invalid while accessing resource'
                'at: {url}'.format(url=response.url))

        if response.status_code == 401:
            if response_json and 'error' in response_json:
                raise AuthenticationError(response_json.error)
            raise AuthenticationError(
                'Failed to authenticate while accessing resource at: '
                '{url}'.format(url=response.url))

        if response.status_code == 404:
            if response_json and 'error' in response_json:
                raise ResourceNotFoundError(response_json.error)
            raise ResourceNotFoundError(
                'Resource not found at: {url}'.format(url=response.url))

        if response.status_code == 405:
            if response_json and 'error' in response_json:
                raise InvalidRequestError(response_json.error)
            raise InvalidRequestError(
                'HTTP method "{method}" not allowed to access resource at: '
                '{url}'.format(method=method, url=response.url))

        if response.status_code == 500:
            if response_json and 'error' in response_json:
                raise PlivoServerError(response_json.error)
            raise PlivoServerError(
                'A server error occurred while accessing resource at: '
                '{url}'.format(url=response.url))

        if method == 'DELETE':
            if response.status_code != 204:
                raise PlivoRestError('Resource at {url} could not be '
                                     'deleted'.format(url=response.url))

        elif response.status_code not in [200, 201, 202]:
            raise PlivoRestError(
                'Received status code {status_code} for the HTTP method '
                '"{method}"'.format(status_code=response.status_code,
                                    method=method))

        return response_json