Пример #1
0
 def create(self, call_uuid, rating, issues=[], notes=""):
     if len(call_uuid) == 0:
         raise ValidationError('call_uuid cannot be empty')
     if not rating:
         raise ValidationError('rating cannot be empty')
     request_path = FEEDBACK_API_PATH.format(call_uuid)
     params_dict = {}
     params_dict['rating'] = rating
     if len(issues) > 0:
         params_dict['issues'] = issues
     if len(notes) > 0:
         params_dict['notes'] = notes
     params_dict['is_callinsights_request'] = True
     params_dict['callinsights_request_path'] = request_path
     return self.client.request('POST', ('Call', ), params_dict)
Пример #2
0
 def update(self, name=None, city=None, address=None):
     if not (name or city or address):
         raise ValidationError(
             'One parameter of name, city and address is required')
     return self.client.request(
         'POST', tuple(), {'name': name,
                           'city': city,
                           'address': address})
Пример #3
0
 def wrapper(self, *args, **kwargs):
     params = inspect.getcallargs(wrapped, *args, **kwargs)
     for arg_name, validators in to_validate.items():
         for validator in validators:
             params[arg_name], errs = validator(
                 arg_name, params.get(arg_name, None))
             if errs:
                 raise ValidationError(errs)
     return wrapped(**params)
Пример #4
0
    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