How to use treq - 10 common examples

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 leapcode / soledad / tests / server / test_incoming_server.py View on Github external
def test_put_incoming_creates_a_blob_using_filesystem(self):
        self.prepare('filesystem')
        user_id, doc_id = self.user_id, uuid4().hex
        content = 'Hi'
        formatter = IncomingFormatter()
        incoming_endpoint = self.uri + '%s/%s' % (user_id, doc_id)
        yield treq.put(incoming_endpoint, BytesIO(content), persistent=False)

        db = self.state.open_database(user_id)
        consumer = DummyRequest([''])
        yield db.read_blob(user_id, doc_id, consumer, namespace='MX')
        flags = yield db.get_flags(user_id, doc_id, namespace='MX')
        data = consumer.written.pop()
        expected_preamble = formatter.preamble(content, doc_id)
        expected_preamble = decode_preamble(expected_preamble, True)
        written_preamble, written_content = data.split()
        written_preamble = decode_preamble(written_preamble, True)
        self.assertEquals(expected_preamble, written_preamble)
        self.assertEquals(content, written_content)
        self.assertIn(Flags.PENDING, flags)
github leapcode / soledad / tests / server / test_tac.py View on Github external
def _get(self, *args, **kwargs):
        kwargs['agent'] = Agent(reactor)
        return treq.get(*args, **kwargs)
github ClusterHQ / flocker / flocker / acceptance / testtools.py View on Github external
def make_post(host, port, data):
        request = post(
            "http://{host}:{port}".format(host=host, port=port),
            data=data,
            persistent=False
        )

        def failed(failure):
            Message.new(message_type=u"acceptance:http_query_failed",
                        reason=unicode(failure)).write()
            return False
        request.addCallbacks(content, failed)
        return request
    d = verify_socket(host, port)
github mozilla-services / autopush-loadtester / aplt / runner.py View on Github external
parsed = urlparse.urlparse(url)
                claims["aud"] = "{scheme}://{netloc}".format(
                    scheme=parsed.scheme,
                    netloc=parsed.netloc
                )
                log.msg("Setting VAPID 'aud' to {}".format(claims["aud"]))
            headers.update(self._vapid.sign(claims, self._crypto_key))
        if data:
            headers.update({
                "Content-Type": "application/octet-stream",
                "Content-Encoding": "aesgcm",
                "Crypto-key": crypto_key,
                "Encryption": self._encryption,
            })

        d = treq.post(url,
                      data=data,
                      headers=headers,
                      allow_redirects=False,
                      agent=self._agent)
        d.addCallback(self._sent_notification, processor)
        d.addErrback(self._error_notif, processor)
github ClusterHQ / flocker / flocker / acceptance / testtools.py View on Github external
def check_and_decode_json(result, response_code):
    """
    Given ``treq`` response object, extract JSON and ensure response code
    is the expected one.

    :param result: ``treq`` response.
    :param int response_code: Expected response code.

    :return: ``Deferred`` firing with decoded JSON.
    """
    def error(body):
        raise ResponseError(result.code, body)

    if result.code != response_code:
        d = content(result)
        d.addCallback(error)
        return d

    return json_content(result)
github ClusterHQ / flocker / flocker / acceptance / endtoend / test_restarts.py View on Github external
def query_server():
            req = get(
                "http://{host}:12345".format(host=node.public_address),
                persistent=False
            ).addCallbacks(content)
            return req
github ClusterHQ / flocker / flocker / node / functional / test_docker.py View on Github external
def send_request():
            """
            Send an HTTP request in a loop until the request is answered.
            """
            response = request(
                b"GET", b"http://127.0.0.1:%d" % (port,),
                persistent=False)

            def check_error(failure):
                """
                Catch ConnectionRefused errors and response timeouts and return
                False so that loop_until repeats the request.

                Other error conditions will be passed down the errback chain.
                """
                failure.trap(ConnectionRefusedError, ResponseNeverReceived)
                return False
            response.addErrback(check_error)
            return response
github benoitc / gunicorn / tests / test_001-valid-requests.py View on Github external
def a_case(fname):
    env = treq.load_py(os.path.splitext(fname)[0] + ".py")
    expect = env['request']
    cfg = env['cfg']
    req = treq.request(fname, expect)
    for case in req.gen_cases(cfg):
        case[0](*case[1:])
github leapcode / soledad / tests / server / test_incoming_server.py View on Github external
def test_put_incoming_creates_a_document_using_couch(self):
        self.prepare('couch')
        user_id, doc_id = self.user_id, uuid4().hex
        content, scheme = 'Hi', EncryptionSchemes.PUBKEY
        formatter = IncomingFormatter()
        incoming_endpoint = self.uri + '%s/%s' % (user_id, doc_id)
        yield treq.put(incoming_endpoint, BytesIO(content), persistent=False)
        db = self.state.open_database(user_id)

        doc = db.get_doc(doc_id)
        self.assertEquals(doc.content, formatter.format(content, scheme))
github leapcode / soledad / tests / benchmarks / test_legacy_vs_blobs.py View on Github external
def deliver_using_incoming_api(url, user_uuid, token, data):
    auth = 'Token %s' % base64.b64encode('%s:%s' % (user_uuid, token))
    uri = "%s/incoming/%s/%s?namespace=MX" % (url, user_uuid, uuid.uuid4().hex)
    return treq.put(uri, headers={'Authorization': auth}, data=BytesIO(data))