Beispiel #1
0
    def from_http(cls, body, headers):
        args = json.loads(body)
        if not isinstance(args, list):
            raise ValueError("List expected")

        kwargs = {}
        for k, v in headers.items():
            k = k.lower()
            if k.startswith("x-servicelib-"):
                kwargs[k[len("x-servicelib-"):]] = json.loads(v)

        return cls(*args, **kwargs)
Beispiel #2
0
    def get_response(self, key):
        """Return the cached response object associated with the given key
        from the results cache, or `None` if no such response was found.

        """
        ret = self.get(key)
        if ret is None or ret == IN_FLIGHT:
            self.log.debug(
                "get_response(%s): Got `%s` from cache, retuning `None`", key, ret
            )
            return None

        try:
            ret = json.loads(ret)
        except Exception as exc:
            self.log.error(
                "Cannot decode JSON object <%s> for key '%s': %s",
                ret,
                key,
                exc,
                exc_info=True,
                stack_info=True,
            )

            # There is no point in keeping this cached value if we cannot
            # decode it.
            self.delete(key)

            return

        self.log.debug("get_response(%s): Returning %s", key, ret)
        return ret
Beispiel #3
0
 def from_http(cls, status, body, headers):
     cls.log.debug("from_http(status=%s, body=<%s>): Entering", status,
                   body)
     body_decoded = json.loads(body)
     if status.startswith("200 "):
         value = body_decoded
     else:
         value = errors.Serializable.from_dict(body_decoded)
     metadata = Metadata.from_http_headers({
         k[len("x-servicelib-"):]: v
         for (k, v) in headers.items() if k.startswith("x-servicelib-")
     })
     return cls(value, metadata, body)
Beispiel #4
0
    def process_hit(self, context, request_md5, response):
        response = json.loads(response)["result"]
        if valid_url(context, response):
            return "done", response, "hit"

        return "process_miss", None, None