Example #1
0
    def request(self,
                method,
                url,
                headers=None,
                params=None,
                data=None,
                raw_response=False):
        """Makes requests to Redmine and returns result in json format"""
        kwargs = dict(
            self.requests, **{
                'headers': headers or {},
                'params': params or {},
                'data': data or {},
            })

        if 'Content-Type' not in kwargs['headers'] and method in ('post',
                                                                  'put'):
            kwargs['data'] = json.dumps(data)
            kwargs['headers']['Content-Type'] = 'application/json'

        if self.impersonate is not None:
            kwargs['headers']['X-Redmine-Switch-User'] = self.impersonate

        # We would like to be authenticated by API key by default
        if 'key' not in kwargs['params'] and self.key is not None:
            kwargs['params']['key'] = self.key
        else:
            kwargs['auth'] = (self.username, self.password)

        response = getattr(requests, method)(url, **kwargs)

        if response.status_code in (200, 201):
            if raw_response:
                return response
            elif not response.content.strip():
                return True
            else:
                return json_response(response.json)
        elif response.status_code == 401:
            raise AuthError
        elif response.status_code == 404:
            raise ResourceNotFoundError
        elif response.status_code == 409:
            raise ConflictError
        elif response.status_code == 412 and self.impersonate is not None:
            raise ImpersonateError
        elif response.status_code == 413:
            raise RequestEntityTooLargeError
        elif response.status_code == 422:
            errors = json_response(response.json)['errors']
            raise ValidationError(
                to_string(', '.join(e if is_string(e) else ': '.join(e)
                                    for e in errors)))
        elif response.status_code == 500:
            raise ServerError

        raise UnknownError(response.status_code)
Example #2
0
    def request(self, method, url, headers=None, params=None, data=None, raw_response=False):
        """Makes requests to Redmine and returns result in json format"""
        kwargs = dict(self.requests, **{
            'headers': headers or {},
            'params': params or {},
            'data': data or {},
        })

        if 'Content-Type' not in kwargs['headers'] and method in ('post', 'put'):
            kwargs['data'] = json.dumps(data)
            kwargs['headers']['Content-Type'] = 'application/json'

        if self.impersonate is not None:
            kwargs['headers']['X-Redmine-Switch-User'] = self.impersonate

        # We would like to be authenticated by API key by default
        if 'key' not in kwargs['params'] and self.key is not None:
            kwargs['params']['key'] = self.key
        else:
            kwargs['auth'] = (self.username, self.password)

        print(url)

        response = getattr(requests, method)(url, **kwargs)

        if response.status_code in (200, 201):
            if raw_response:
                return response
            elif not response.content.strip():
                return True
            else:
                try:
                    return json_response(response.json)
                except (ValueError, TypeError):
                    raise JSONDecodeError(response)
        elif response.status_code == 401:
            raise AuthError
        elif response.status_code == 403:
            raise ForbiddenError
        elif response.status_code == 404:
            raise ResourceNotFoundError
        elif response.status_code == 409:
            raise ConflictError
        elif response.status_code == 412 and self.impersonate is not None:
            raise ImpersonateError
        elif response.status_code == 413:
            raise RequestEntityTooLargeError
        elif response.status_code == 422:
            errors = json_response(response.json)['errors']
            raise ValidationError(to_string(', '.join(e if is_string(e) else ': '.join(e) for e in errors)))
        elif response.status_code == 500:
            raise ServerError

        raise UnknownError(response.status_code)