Valid Response:
import requests
with requests.Session() as req:
req.auth = authdata
req.headers.update({
'x-amz-access-token': access
})
r = req.get(
'https://sellingpartnerapi-na.amazon.com/orders/v0/orders/', params=params)
print(r)
But using httpx am getting:
import httpx
async with httpx.AsyncClient(timeout=None) as client:
client.auth = authdata
client.headers.update({
'x-amz-access-token': access
})
r = await client.get('https://sellingpartnerapi-na.amazon.com/orders/v0/orders/', params=params)
print(r)
Full Traceback:
Traceback (most recent call last):
File "c:\Users\AmericaN\Desktop\Lab\code.py", line 60, in <module>
trio.run(main)
File "C:\Users\AmericaN\Desktop\Lab\MyEnv\lib\site-packages\trio\_core\_run.py", line 1932, in run
raise runner.main_task_outcome.error
File "c:\Users\AmericaN\Desktop\Lab\code.py", line 56, in main
await get_orders(authdata, await get_token())
File "c:\Users\AmericaN\Desktop\Lab\code.py", line 49, in get_orders
r = await client.get('https://sellingpartnerapi-na.amazon.com/orders/v0/orders/', params=params)
File "C:\Users\AmericaN\Desktop\Lab\MyEnv\lib\site-packages\httpx\_client.py", line 1539, in get
return await self.request(
File "C:\Users\AmericaN\Desktop\Lab\MyEnv\lib\site-packages\httpx\_client.py", line 1361, in request
response = await self.send(
File "C:\Users\AmericaN\Desktop\Lab\MyEnv\lib\site-packages\httpx\_client.py", line 1396, in send
response = await self._send_handling_auth(
File "C:\Users\AmericaN\Desktop\Lab\MyEnv\lib\site-packages\httpx\_client.py", line 1428, in _send_handling_auth
request = await auth_flow.__anext__()
File "C:\Users\AmericaN\Desktop\Lab\MyEnv\lib\site-packages\httpx\_auth.py", line 92, in async_auth_flow
request = next(flow)
File "C:\Users\AmericaN\Desktop\Lab\MyEnv\lib\site-packages\httpx\_auth.py", line 115, in auth_flow
yield self._func(request)
File "C:\Users\AmericaN\Desktop\Lab\MyEnv\lib\site-packages\requests_auth_aws_sigv4\__init__.py", line 109, in __call__
p = urlparse(r.url)
File "C:\Program Files\Python39\lib\urllib\parse.py", line 389, in urlparse
url, scheme, _coerce_result = _coerce_args(url, scheme)
File "C:\Program Files\Python39\lib\urllib\parse.py", line 125, in _coerce_args
return _decode_args(args) + (_encode_result,)
File "C:\Program Files\Python39\lib\urllib\parse.py", line 109, in _decode_args
return tuple(x.decode(encoding, errors) if x else '' for x in args)
File "C:\Program Files\Python39\lib\urllib\parse.py", line 109, in <genexpr>
return tuple(x.decode(encoding, errors) if x else '' for x in args)
AttributeError: 'URL' object has no attribute 'decode'
Solved By using the following : httpx-auth
Related
I am trying to create a webservice to update a wallet pass using apns push notifications. I am using httpx for this as it can use http/2. I have the following test code for this:
import httpx
import ssl
import asyncio
async def send_push():
context = ssl.create_default_context()
context.load_verify_locations(cafile = "/Users/valley/Desktop/External_Finder/Learning_Centers_Development/Arcade-Pass/certs/passcertificate.pem")
payload = {
"aps" : ""
}
async with httpx.AsyncClient(http2 = True, cert = "/Users/valley/Desktop/External_Finder/Learning_Centers_Development/Arcade-Pass/certs/ArcadePassCertKey.pem") as client:
r = await client.post("https://api.sandbox.push.apple.com:2197/3/device/4c526af3e9cd29cc0c6f8954de5f68fd1d00348696fe4a984581e35f19fe1ddf", data = payload)
print(r.http_version)
print(r.text)
asyncio.run(send_push())
When I try to run this, I am getting the following traceback and error:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/httpx/_transports/default.py", line 60, in map_httpcore_exceptions
yield
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/httpx/_transports/default.py", line 353, in handle_async_request
resp = await self._pool.handle_async_request(req)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/httpcore/_async/connection_pool.py", line 253, in handle_async_request
raise exc
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/httpcore/_async/connection_pool.py", line 237, in handle_async_request
response = await connection.handle_async_request(request)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/httpcore/_async/connection.py", line 90, in handle_async_request
return await self._connection.handle_async_request(request)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/httpcore/_async/http2.py", line 146, in handle_async_request
raise exc
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/httpcore/_async/http2.py", line 114, in handle_async_request
status, headers = await self._receive_response(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/httpcore/_async/http2.py", line 231, in _receive_response
event = await self._receive_stream_event(request, stream_id)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/httpcore/_async/http2.py", line 262, in _receive_stream_event
await self._receive_events(request, stream_id)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/httpcore/_async/http2.py", line 291, in _receive_events
raise RemoteProtocolError(event)
httpcore.RemoteProtocolError: <ConnectionTerminated error_code:ErrorCodes.NO_ERROR, last_stream_id:0, additional_data:7b22726561736f6e223a22426164436572746966>
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "test_push_notifications.py", line 24, in <module>
asyncio.run(send_push())
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/runners.py", line 43, in run
return loop.run_until_complete(main)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/base_events.py", line 608, in run_until_complete
return future.result()
File "test_push_notifications.py", line 19, in send_push
r = await client.post("https://api.sandbox.push.apple.com:2197/3/device/4c526af3e9cd29cc0c6f8954de5f68fd1d00348696fe4a984581e35f19fe1ddf", data = payload)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/httpx/_client.py", line 1848, in post
return await self.request(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/httpx/_client.py", line 1533, in request
return await self.send(request, auth=auth, follow_redirects=follow_redirects)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/httpx/_client.py", line 1620, in send
response = await self._send_handling_auth(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/httpx/_client.py", line 1648, in _send_handling_auth
response = await self._send_handling_redirects(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/httpx/_client.py", line 1685, in _send_handling_redirects
response = await self._send_single_request(request)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/httpx/_client.py", line 1722, in _send_single_request
response = await transport.handle_async_request(request)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/httpx/_transports/default.py", line 353, in handle_async_request
resp = await self._pool.handle_async_request(req)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/contextlib.py", line 131, in __exit__
self.gen.throw(type, value, traceback)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/httpx/_transports/default.py", line 77, in map_httpcore_exceptions
raise mapped_exc(message) from exc
httpx.RemoteProtocolError: <ConnectionTerminated error_code:ErrorCodes.NO_ERROR, last_stream_id:0, additional_data:7b22726561736f6e223a22426164436572746966>
Can someone please help me navigate this error and send a successful request to APNS?
I'm observing closedResourceError when sending multiple requests using httpx async client. httpx version used is 0.19.0. Has anyone faced similar issue? if it is an issue and fixed in later version?
Logs:
resp = await asyncio.gather(*[self.async_send_request(method,self.dest_host,self.dest_port,uri,i,payload=payload,headers=headers) for i in range(int(times) )])
File "/env/lib64/python3.9/site-packages/ocnftest_lib/scp_Pcf_client.py", line 175, in async_send_request
return await self.conn.request(method,"http://{}:{}{}".format(self.dest_host,self.dest_port,uri),data=payload,headers=headers,allow_redirects=False)
File "/env/lib64/python3.9/site-packages/httpx/_client.py", line 1494, in request
response = await self.send(
File "/env/lib64/python3.9/site-packages/httpx/_client.py", line 1586, in send
response = await self._send_handling_auth(
File "/env/lib64/python3.9/site-packages/httpx/_client.py", line 1616, in _send_handling_auth
response = await self._send_handling_redirects(
File "/env/lib64/python3.9/site-packages/httpx/_client.py", line 1655, in _send_handling_redirects
response = await self._send_single_request(request, timeout)
File "/env/lib64/python3.9/site-packages/httpx/_client.py", line 1699, in _send_single_request
) = await transport.handle_async_request(
File "/env/lib64/python3.9/site-packages/httpx/_transports/default.py", line 281, in handle_async_request
) = await self._pool.handle_async_request(
File "/env/lib64/python3.9/site-packages/httpcore/_async/connection_pool.py", line 234, in handle_async_request
response = await connection.handle_async_request(
File "/env/lib64/python3.9/site-packages/httpcore/_async/connection.py", line 148, in handle_async_request
return await self.connection.handle_async_request(
File "/env/lib64/python3.9/site-packages/httpcore/_async/http2.py", line 165, in handle_async_request
return await h2_stream.handle_async_request(
File "/env/lib64/python3.9/site-packages/httpcore/_async/http2.py", line 339, in handle_async_request
await self.send_headers(method, url, headers, has_body, timeout)
File "/env/lib64/python3.9/site-packages/httpcore/_async/http2.py", line 398, in send_headers
await self.connection.send_headers(self.stream_id, headers, end_stream, timeout)
File "/env/lib64/python3.9/site-packages/httpcore/_async/http2.py", line 274, in send_headers
await self.socket.write(data_to_send, timeout)
File "/env/lib64/python3.9/site-packages/httpcore/_backends/anyio.py", line 77, in write
return await self.stream.send(data)
File "/env/lib64/python3.9/site-packages/anyio/_backends/_asyncio.py", line 1116, in send
raise ClosedResourceError
anyio.ClosedResourceError
Sample Code
async def send_request(self,payload,times,method,uri,headers):
resp = await asyncio.gather(*[self.async_send_request(method,self.dest_host,self.dest_port,uri,i,payload=payload,headers=headers) for i in range(int(times) )])
await self.conn.aclose()
log.logger.info("[+] #{} {} {}".format(method,resp[int(times)-1].status_code,uri))
return resp[int(times)-1]
async def async_send_request(self,method,dest_host,dest_port,uri,times,payload=None,headers=None):
if self.security == 'secure':
return await self.conn.request(method,"https://{}:{}{}".format(self.dest_host,self.dest_port,uri),data=payload,headers=headers,allow_redirects=False)
else:
return await self.conn.request(method,"http://{}:{}{}".format(self.dest_host,self.dest_port,uri),data=payload,headers=headers,allow_redirects=False)
def send_message:
self.conn = httpx.AsyncClient(http2=True,http1=False,proxies=self.proxies,timeout=10)
response = asyncio.run(self.send_request(payload,times,method,uri,headers))
This is the logs I received from my unittest for my APIs flask application which is using flask-restful, it showed that I have an int object not iterable error.
Really appreciate if anyone tell me what is actually wrong in my unittest codes :(
.
======================================================================
ERROR: test_brand_create (test.brand.BrandTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/david/ITP-Team3/test/brand.py", line 37, in test_brand_create
response = tester.post('/api/brand/', data=json.dumps(payload), headers=headers)
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/test.py", line 1140, in post
return self.open(*args, **kw)
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/flask/testing.py", line 217, in open
return super().open(
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/test.py", line 1089, in open
response = self.run_wsgi_app(request.environ, buffered=buffered)
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/test.py", line 956, in run_wsgi_app
rv = run_wsgi_app(self.application, environ, buffered=buffered)
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/test.py", line 1255, in run_wsgi_app
for item in app_iter:
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/wsgi.py", line 462, in __next__
return self._next()
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/wrappers/response.py", line 50, in _iter_encoded
for item in iterable:
TypeError: 'int' object is not iterable
======================================================================
ERROR: test_brand_put (test.brand.BrandTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/david/ITP-Team3/test/brand.py", line 49, in test_brand_put
response = tester.put('/api/brand/'+id, data=json.dumps(payload), headers=headers)
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/test.py", line 1145, in put
return self.open(*args, **kw)
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/flask/testing.py", line 217, in open
return super().open(
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/test.py", line 1089, in open
response = self.run_wsgi_app(request.environ, buffered=buffered)
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/test.py", line 956, in run_wsgi_app
rv = run_wsgi_app(self.application, environ, buffered=buffered)
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/test.py", line 1255, in run_wsgi_app
for item in app_iter:
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/wsgi.py", line 462, in __next__
return self._next()
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/wrappers/response.py", line 50, in _iter_encoded
for item in iterable:
TypeError: 'int' object is not iterable
----------------------------------------------------------------------
Ran 11 tests in 2.370s
FAILED (errors=2)
Here's my test code that I had the error on:
# brand.py
def test_brand_create(self):
tester = app.test_client(self)
headers = login(tester)
payload = {'brandName': 'test1'}
response = tester.post('/api/brand/', data=json.dumps(payload), headers=headers)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content_type, 'application/json')
def test_brand_put(self):
tester = app.test_client(self)
headers = login(tester)
payload = {'brandName': 'test'}
print("TYPE", type(get_id(app, Brand, 'test1')))
print("VALUE", get_id(app, Brand, 'test1'))
id = get_id(app, Brand, 'test1')
response = tester.put('/api/brand/'+id, data=json.dumps(payload), headers=headers)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content_type, 'application/json')
and the utils code as well
# utils.py
def login(tester):
headers = {'Content-Type': 'application/json'}
# put user and password in .env file
payload = {'username': os.getenv("user"), 'password_hash': os.getenv("password")}
response = tester.post('/api/user/login/', data= json.dumps(payload), headers=headers)
jwt_str = response.data.decode('utf-8')
header_jwt= ast.literal_eval(jwt_str)
return {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + header_jwt['jwt_token']}
def get_id(app, model, brandName) -> None:
with app.app_context():
test_query = model.query.filter_by(brandName=brandName).first()
id_lookup = model.query.get(test_query.id)
return str(id_lookup.id)
I'm having an issue where i try to print a invite link into console, but instead it prints out
<generator object Client.create_invite at 0x000001D310A183B8>
<generator object Client.create_invite at 0x000001D310A65410>
i am using this code:
#client.event
async def on_ready():
#await client.change_presence(game=Game(name="with humans"))
print("Logged in as " + client.user.name)
await asyncio.sleep(3)
for server in client.servers:
for channel in server.channels:
channel_type = channel.type
if str(channel_type) == 'text':
invitelinknew = client.create_invite(destination=channel, xkcd=True, max_uses=100)
print(str(invitelinknew))
break
i tried changing print(invitelinknew) to print(str(invitelinknew)), but it didn't change the outcome
EDIT:
New errors when consuming the genrator with invitelinknew2 = list(invitelinknew)
and print(invitelinknew2):
Traceback (most recent call last):
File "C:\Program Files\Python36\lib\site-packages\discord\client.py", line 307, in _run_event
yield from getattr(self, event)(*args, **kwargs)
File "C:/Users/Rasmus/Python/discordbot/botnoggi2.py", line 128, in on_ready
invitelinknew2 = list(invitelinknew)
File "C:\Program Files\Python36\lib\site-packages\discord\client.py", line 2628, in create_invite
data = yield from self.http.create_invite(destination.id, **options)
File "C:\Program Files\Python36\lib\site-packages\discord\http.py", line 137, in request
r = yield from self.session.request(method, url, **kwargs)
File "C:\Program Files\Python36\lib\site-packages\aiohttp\client.py", line 555, in __iter__
resp = yield from self._coro
File "C:\Program Files\Python36\lib\site-packages\aiohttp\client.py", line 202, in _request
yield from resp.start(conn, read_until_eof)
File "C:\Program Files\Python36\lib\site-packages\aiohttp\client_reqrep.py", line 640, in start
message = yield from httpstream.read()
File "C:\Program Files\Python36\lib\site-packages\aiohttp\streams.py", line 641, in read
result = yield from super().read()
File "C:\Program Files\Python36\lib\site-packages\aiohttp\streams.py", line 476, in read
yield from self._waiter
AssertionError: yield from wasn't used with future
Future exception was never retrieved
future: <Future finished exception=ServerDisconnectedError()>
aiohttp.errors.ServerDisconnectedError
According to the documentation create_invite is a coroutine and requires the await keyword
Change your code to include it such as
if channel.type == discord.ChannelType.text:
invitelinknew = await client.create_invite(destination=channel, xkcd=True, max_uses=100)
print(invitelinknew.url)
I'm doing a POST request to an API, the code looks like this:
#gen.coroutine
def call():
...
response = yield AsyncHTTPClient().fetch(
HTTPRequest(
url='https://api.mywebsite.com/v1/users',
headers=headers,
method='POST',
body=json.dumps(body),
validate_cert=False
)
)
print response, response.body
if __name__ == "__main__":
tornado.ioloop.IOLoop.current().run_sync(call)
The server responds first time with 201 Created and the second time with 200 OK.
But for that code I get this error for the first time. The second time works
Traceback (most recent call last):
File "t.py", line 49, in <module>
tornado.ioloop.IOLoop.current().run_sync(call)
File "/usr/lib/python2.7/dist-packages/tornado/ioloop.py", line 389, in run_sync
return future_cell[0].result()
File "/usr/lib/python2.7/dist-packages/tornado/concurrent.py", line 129, in result
raise_exc_info(self.__exc_info)
File "/usr/lib/python2.7/dist-packages/tornado/stack_context.py", line 302, in wrapped
ret = fn(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/tornado/gen.py", line 574, in inner
self.set_result(key, result)
File "/usr/lib/python2.7/dist-packages/tornado/gen.py", line 500, in set_result
self.run()
File "/usr/lib/python2.7/dist-packages/tornado/gen.py", line 529, in run
yielded = self.gen.throw(*exc_info)
File "t.py", line 43, in call
validate_cert=False
File "/usr/lib/python2.7/dist-packages/tornado/gen.py", line 520, in run
next = self.yield_point.get_result()
File "/usr/lib/python2.7/dist-packages/tornado/gen.py", line 409, in get_result
return self.runner.pop_result(self.key).result()
File "/usr/lib/python2.7/dist-packages/tornado/concurrent.py", line 131, in result
return super(TracebackFuture, self).result(timeout=timeout)
File "/usr/lib/python2.7/dist-packages/concurrent/futures/_base.py", line 401, in result
return self.__get_result()
File "/usr/lib/python2.7/dist-packages/concurrent/futures/_base.py", line 360, in __get_result
raise self._exception
AssertionError
Looks like you are using sync function HTTPRequest in future
from tornado.httpclient import AsyncHTTPClient
from tornado.httpclient import HTTPResponse
def call():
response = None
http_client = AsyncHTTPClient()
try:
body = json.dumps(body)
response: HTTPResponse = yield http_client.fetch('https://api.mywebsite.com/v1/users', method='POST', body=str(body), headers=headers, request_timeout=5)
except Exception as e:
print('get_request error:{0}'.format(e))