You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ponymail.apache.org by hu...@apache.org on 2021/05/09 17:44:34 UTC

[incubator-ponymail-foal] 01/03: Switch from requests to aiohttp.client

This is an automated email from the ASF dual-hosted git repository.

humbedooh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-ponymail-foal.git

commit 5cff73dcb29fa3ab4f42156e6e704c043e5ecb7f
Author: Daniel Gruno <hu...@apache.org>
AuthorDate: Sun May 9 19:41:01 2021 +0200

    Switch from requests to aiohttp.client
    
    This also eases the async aspect of things by not requiring a worker
    thread.
---
 server/plugins/oauthGeneric.py | 12 +++++-------
 server/plugins/oauthGithub.py  | 30 ++++++++++++------------------
 server/requirements.txt        |  1 -
 3 files changed, 17 insertions(+), 26 deletions(-)

diff --git a/server/plugins/oauthGeneric.py b/server/plugins/oauthGeneric.py
index 6bf5da1..78b1e74 100644
--- a/server/plugins/oauthGeneric.py
+++ b/server/plugins/oauthGeneric.py
@@ -1,6 +1,6 @@
 # Generic OAuth plugin
 import re
-import requests
+import aiohttp.client
 
 
 async def process(formdata, session, server):
@@ -10,10 +10,8 @@ async def process(formdata, session, server):
         oauth_domain = m.group(1)
         headers = {"User-Agent": "Pony Mail OAuth Agent/0.1"}
         # This is a synchronous process, so we offload it to an async runner in order to let the main loop continue.
-        rv = await server.runners.run(
-            requests.post, formdata["oauth_token"], headers=headers, data=formdata
-        )
-        js = rv.json()
-        js["oauth_domain"] = oauth_domain
-        js["authoritative"] = True
+        async with aiohttp.client.request("POST", formdata["oauth_token"], headers=headers, data=formdata) as rv:
+            js = await rv.json()
+            js["oauth_domain"] = oauth_domain
+            js["authoritative"] = True
     return js
diff --git a/server/plugins/oauthGithub.py b/server/plugins/oauthGithub.py
index 91c698a..341c702 100644
--- a/server/plugins/oauthGithub.py
+++ b/server/plugins/oauthGithub.py
@@ -9,7 +9,7 @@
 """
 
 import re
-import requests
+import aiohttp.client
 import plugins.server
 import typing
 
@@ -20,22 +20,16 @@ async def process(
     formdata["client_id"] = server.config.oauth.github_client_id
     formdata["client_secret"] = server.config.oauth.github_client_secret
 
-    rv = await server.runners.run(
-        requests.post, "https://github.com/login/oauth/access_token", data=formdata
-    )
-    m = re.search(r"access_token=([a-f0-9]+)", rv.text)
+    with aiohttp.client.request("POST", "https://github.com/login/oauth/access_token", data=formdata) as rv:
+        txt = await rv.read()
+        m = re.search(r"access_token=([a-f0-9]+)", txt)
 
-    if m:
-        rv = await server.runners.run(
-            requests.get,
-            "https://api.github.com/user",
-            headers={"authorization": "token %s" % m.group(1)},
-        )
-
-        js = rv.json()
-        js["oauth_domain"] = "github.com"
-        # Full name and email address might not always be available to us. Fake it till you make it.
-        js["name"] = js["name"] or js["login"]
-        js["email"] = js["email"] or "%s@users.github.com" % js["login"]
-        return js
+        if m:
+            with aiohttp.client.request("GET", "https://api.github.com/user", headers={"authorization": "token %s" % m.group(1)}) as rv:
+                js = rv.json()
+                js["oauth_domain"] = "github.com"
+                # Full name and email address might not always be available to us. Fake it till you make it.
+                js["name"] = js["name"] or js["login"]
+                js["email"] = js["email"] or "%s@users.github.com" % js["login"]
+                return js
     return None
diff --git a/server/requirements.txt b/server/requirements.txt
index 5396325..6a0058c 100644
--- a/server/requirements.txt
+++ b/server/requirements.txt
@@ -7,6 +7,5 @@ elasticsearch~=7.9.1             # AL2.0
 certifi~=2020.6.20               # MPL2.0
 netaddr~=0.8.0                   # BSD, MIT
 formatflowed~=2.0.0              # Python Software Foundation
-requests~=2.24.0                 # AL2.0
 google-auth~=1.21.1              # AL2.0
 aiosmtplib~=1.1.3                # MIT
\ No newline at end of file