Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Use urllib to combine urls (#5) #22

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bananas_cli/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async def authenticate(session, client_id):
bearer_token = f.read()

session.set_header("Authorization", f"Bearer {bearer_token}")
status, data = await session.get("/user")
status, data = await session.get("user")
if status == 200:
return

Expand All @@ -100,7 +100,7 @@ async def authenticate(session, client_id):
code_challenge = base64.urlsafe_b64encode(digest).decode().rstrip("=")

status, data = await session.get(
"/user/authorize?"
"user/authorize?"
"audience=github&"
"redirect_uri=http%3A%2F%2Flocalhost%3A3977%2F&"
"response_type=code&"
Expand Down
2 changes: 1 addition & 1 deletion bananas_cli/commands/list_self.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@pass_session
@task
async def list_self(session):
status, data = await session.get("/package/self")
status, data = await session.get("package/self")
if status != 200:
log.error(f"Server returned invalid status code {status}: {data}")
raise Exit
Expand Down
6 changes: 3 additions & 3 deletions bananas_cli/commands/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async def upload(session, version, name, description, url, license, files):

starting_part = "/".join(parts) + "/"

status, data = await session.post("/new-package", json={})
status, data = await session.post("new-package", json={})
if status != 200:
log.error(f"Server returned invalid status code {status}: {data}")
raise Exit
Expand All @@ -61,7 +61,7 @@ async def upload(session, version, name, description, url, license, files):
if license:
payload["license"] = license

status, data = await session.put(f"/new-package/{upload_token}", json=payload)
status, data = await session.put(f"new-package/{upload_token}", json=payload)
if status == 400:
show_validation_errors(data)
raise Exit
Expand All @@ -74,7 +74,7 @@ async def upload(session, version, name, description, url, license, files):
log.info(f"Uploading {filename} ...")
session.tus_upload(upload_token, fullpath, filename)

status, data = await session.post(f"/new-package/{upload_token}/publish", json={})
status, data = await session.post(f"new-package/{upload_token}/publish", json={})
if status == 400:
show_validation_errors(data)
raise Exit
Expand Down
25 changes: 14 additions & 11 deletions bananas_cli/session.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import aiohttp
import logging
import urllib.parse

from tusclient.client import TusClient
from tusclient.exceptions import TusCommunicationError
Expand All @@ -14,8 +15,8 @@
class Session:
def __init__(self, api_url, tus_url):
self.session = None
self.api_url = api_url
self.tus_url = tus_url
self.api_url = f"{api_url}/"
self.tus_url = f"{tus_url}/"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do:

if not api.url.endswith("/"):
  api_url += "/"

or something.


self._headers = {}

Expand All @@ -36,27 +37,29 @@ async def _read_response(self, response):
return response.status, data

async def get(self, url):
response = await self.session.get(f"{self.api_url}{url}", headers=self._headers, allow_redirects=False)
full_url = urllib.parse.urljoin(self.api_url, url)
response = await self.session.get(full_url, headers=self._headers, allow_redirects=False)
return await self._read_response(response)

async def post(self, url, json):
response = await self.session.post(
f"{self.api_url}{url}", json=json, headers=self._headers, allow_redirects=False
)
full_url = urllib.parse.urljoin(self.api_url, url)
response = await self.session.post(full_url, json=json, headers=self._headers, allow_redirects=False)
return await self._read_response(response)

async def put(self, url, json):
response = await self.session.put(
f"{self.api_url}{url}", json=json, headers=self._headers, allow_redirects=False
)
full_url = urllib.parse.urljoin(self.api_url, url)
response = await self.session.put(full_url, json=json, headers=self._headers, allow_redirects=False)
return await self._read_response(response)

def tus_upload(self, upload_token, fullpath, filename):
tus = TusClient(f"{self.tus_url}/new-package/tus/")
full_url = urllib.parse.urljoin(self.tus_url, "new-package/tus/")
tus = TusClient(full_url)

try:
uploader = tus.uploader(
fullpath, chunk_size=UPLOAD_CHUNK_SIZE, metadata={"filename": filename, "upload-token": upload_token}
fullpath,
chunk_size=UPLOAD_CHUNK_SIZE,
metadata={"filename": filename, "upload-token": upload_token},
)
uploader.upload()
except TusCommunicationError:
Expand Down