How to use the treq.json_content function in treq

To help you get started, we’ve selected a few treq examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github CPChain / pdash / cpchain / wallet / net.py View on Github external
def hide_product(self, market_hash=''):
        header = {"MARKET-KEY": self.public_key, "MARKET-TOKEN": self.token,
                  'Content-Type': 'application/json'}
        logger.debug(self.public_key)
        data = {'market_hash': market_hash}
        url = self.url + 'product/v1/product/hide/'
        logger.debug('hide product payload: %s', data)
        logger.debug('hide product url: %s', url)
        resp = yield treq.post(url, headers=header, json=data, persistent=False)
        confirm_info = yield treq.json_content(resp)
        logger.debug('hide product to market confirm: %s', confirm_info)
        return confirm_info['status']
github matrix-org / synapse / scripts-dev / benchmark_room.py View on Github external
async def do_request(cmd, resp_type, *extra_args):

        method, path, body = cmd
        resp = await treq.request(method, endpoint + path, data=body.encode("utf8"))
        body = await treq.content(resp)
        assert resp.code == 200, body
        resp_body = await treq.json_content(resp)
        matrix_response = resp_type.from_dict(resp_body, *extra_args)
        return matrix_response
github matrix-org / synapse / synapse / http / matrixfederationclient.py View on Github external
"""
    Reads the JSON body of a response, with a timeout

    Args:
        reactor (IReactor): twisted reactor, for the timeout
        timeout_sec (float): number of seconds to wait for response to complete
        request (MatrixFederationRequest): the request that triggered the response
        response (IResponse): response to the request

    Returns:
        dict: parsed JSON response
    """
    try:
        check_content_type_is_json(response.headers)

        d = treq.json_content(response)
        d = timeout_deferred(d, timeout=timeout_sec, reactor=reactor)

        body = yield make_deferred_yieldable(d)
    except Exception as e:
        logger.warning(
            "{%s} [%s] Error reading response: %s",
            request.txn_id,
            request.destination,
            e,
        )
        raise
    logger.info(
        "{%s} [%s] Completed: %d %s",
        request.txn_id,
        request.destination,
        response.code,
github ClusterHQ / powerstrip-flocker / powerstripflocker / adapter.py View on Github external
def dataset_exists():
                d = self.client.get(self.base_url + "/state/datasets")
                d.addCallback(treq.json_content)
                def check_dataset_exists(datasets):
                    """
                    The /v1/state/datasets API seems to show the volume as
                    being on two hosts at once during a move. We assume
                    therefore that when it settles down to only show it on one
                    host that this means the move is complete.
                    """
                    print "Got", self.ip, "datasets:", datasets
                    matching_datasets = []
                    for dataset in datasets:
                        if dataset["dataset_id"] == dataset_id:
                            matching_datasets.append(dataset)
                    if len(matching_datasets) == 1:
                        if matching_datasets[0]["primary"] == self.ip:
                            return True
                    return False
github twisted / txacme / src / txacme / client.py View on Github external
to construct one.
        :param int timeout: Number of seconds to wait for an HTTP response
            during ACME server interaction.

        :return: The constructed client.
        :rtype: Deferred[`Client`]
        """
        action = LOG_ACME_CONSUME_DIRECTORY(
            url=url, key_type=key.typ, alg=alg.name)
        with action.context():
            check_directory_url_type(url)
            jws_client = _default_client(jws_client, reactor, key, alg)
            jws_client.timeout = timeout
            return (
                DeferredContext(jws_client.get(url.asText()))
                .addCallback(json_content)
                .addCallback(messages.Directory.from_json)
                .addCallback(
                    tap(lambda d: action.add_success_fields(directory=d)))
                .addCallback(cls, reactor, key, jws_client)
                .addActionFinish())
github mochi / vor / vor / elasticsearch.py View on Github external
def collectStats(self):
        d = self.treq.get(self.endpoint, auth=self.auth)
        d.addCallback(treq.json_content)
        d.addCallback(self.flatten)
        d.addErrback(log.err)
        return d
github CPChain / pdash / cpchain / wallet / net.py View on Github external
def myproducts(self):
        header = {"MARKET-KEY": self.public_key, "MARKET-TOKEN": self.token,
                  'Content-Type': 'application/json'}
        url = utils.build_url(self.url + "product/v1/allproducts/my_products/", {})
        logger.debug(url)
        resp = yield treq.get(url, headers=header)
        products_info = yield treq.json_content(resp)
        return products_info
github CPChain / pdash / cpchain / wallet / net.py View on Github external
def query_records(self, address):
        url = self.url + 'records/v1/record/'
        header = {"MARKET-KEY": self.public_key, "MARKET-TOKEN": self.token, 'Content-Type': 'application/json'}
        url = utils.build_url(url, {'address': address})
        resp = yield treq.get(url, headers=header, persistent=False)
        data_info = yield treq.json_content(resp)
        return data_info
github ClusterHQ / flocker / flocker / control / configuration_storage / consul.py View on Github external
def got_response(response):
            if response.code in success_codes:
                action.addSuccessFields(response_code=response.code)
                d = json_content(response)
                d.addCallback(lambda decoded_body:
                              (decoded_body, response.headers))
                return d
            else:
                d = content(response)
                d.addCallback(error, response.code)
                return d
        headers = {}
github CPChain / pdash / cpchain / wallet / net.py View on Github external
def add_follow_tag(self, tag="tag1"):
        header = {"MARKET-KEY": self.public_key, "MARKET-TOKEN": self.token,
                  'Content-Type': 'application/json'}
        logger.debug(self.public_key)
        data = {'public_key': self.public_key, 'tag': tag}
        url = self.url + 'product/v1/my_tag/subscribe/'
        logger.debug('add following tag payload: %s', data)
        logger.debug('add following tag url: %s', url)
        resp = yield treq.post(url, headers=header, json=data, persistent=False)
        confirm_info = yield treq.json_content(resp)
        logger.debug('add following tag to market confirm: %s', confirm_info)
        return confirm_info['status']