예제 #1
0
 def lookup_operation(self, operation_id: str):
     for item in self.document.walk_links():
         if item.link.name == operation_id:
             return item.link
     text = 'Operation ID "%s" not found in schema.' % operation_id
     message = exceptions.ErrorMessage(text=text, code="invalid-operation")
     raise exceptions.ClientError(messages=[message])
예제 #2
0
    def get_url(self, link, params):
        url = urljoin(self.document.url, link.url)

        scheme = urlparse(url).scheme.lower()

        if not scheme:
            text = "URL missing scheme '%s'." % url
            message = exceptions.ErrorMessage(text=text, code="invalid-url")
            raise exceptions.ClientError(messages=[message])

        if scheme not in self.transport.schemes:
            text = "Unsupported URL scheme '%s'." % scheme
            message = exceptions.ErrorMessage(text=text, code="invalid-url")
            raise exceptions.ClientError(messages=[message])

        for field in link.get_path_fields():
            value = str(params[field.name])
            if "{%s}" % field.name in url:
                url = url.replace("{%s}" % field.name, quote(value, safe=""))
            elif "{+%s}" % field.name in url:
                url = url.replace("{+%s}" % field.name, quote(value, safe="/"))

        return url
예제 #3
0
    def get_encoder(self, encoding):
        """
        Given the value of the encoding, return the appropriate encoder for
        handling the request content.
        """
        content_type = encoding.split(';')[0].strip().lower()
        main_type = content_type.split('/')[0] + '/*'
        wildcard_type = '*/*'

        for codec in self.encoders:
            if codec.media_type in (content_type, main_type, wildcard_type):
                return codec

        text = "Unsupported encoding '%s' for request." % encoding
        message = exceptions.ErrorMessage(text=text,
                                          code='cannot-encode-request')
        raise exceptions.ClientError(messages=[message])
예제 #4
0
    def get_decoder(self, content_type=None):
        """
        Given the value of a 'Content-Type' header, return the appropriate
        decoder for handling the response content.
        """
        if content_type is None:
            return self.decoders[0]

        content_type = content_type.split(';')[0].strip().lower()
        main_type = content_type.split('/')[0] + '/*'
        wildcard_type = '*/*'

        for codec in self.decoders:
            if codec.media_type in (content_type, main_type, wildcard_type):
                return codec

        text = "Unsupported encoding '%s' in response Content-Type header." % content_type
        message = exceptions.ErrorMessage(text=text,
                                          code='cannot-decode-response')
        raise exceptions.ClientError(messages=[message])
예제 #5
0
파일: client.py 프로젝트: zaarab001/apistar
    def request(self, operation_id: str, **params):
        link = self.lookup_operation(operation_id)

        validator = validators.Object(
            properties={field.name: validators.Any()
                        for field in link.fields},
            required=[field.name for field in link.fields if field.required],
            additional_properties=False)
        try:
            validator.validate(params)
        except exceptions.ValidationError as exc:
            raise exceptions.ClientError(messages=exc.messages) from None

        method = link.method
        url = self.get_url(link, params)
        query_params = self.get_query_params(link, params)
        (content, encoding) = self.get_content_and_encoding(link, params)

        return self.transport.send(method,
                                   url,
                                   query_params=query_params,
                                   content=content,
                                   encoding=encoding)