예제 #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])