Skip to content

Commit 554dcf8

Browse files
committed
Refactor application
1 parent 4441432 commit 554dcf8

4 files changed

Lines changed: 69 additions & 93 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import ssl
2+
import certifi
3+
4+
import asyncclick as click
5+
6+
from github_deploy.commands._constants import REPOS_URL
7+
8+
9+
async def get(*, session, url, headers=None, skip_missing=False):
10+
ssl_context = ssl.create_default_context(cafile=certifi.where())
11+
12+
async with session.get(
13+
url,
14+
headers=headers,
15+
timeout=70,
16+
ssl_context=ssl_context,
17+
raise_for_status=not skip_missing,
18+
) as response:
19+
if skip_missing and response.status == 404:
20+
return {}
21+
22+
value = await response.json()
23+
return value
24+
25+
26+
async def put(*, session, url, data, headers=None):
27+
ssl_context = ssl.create_default_context(cafile=certifi.where())
28+
29+
async with session.put(
30+
url,
31+
json=data,
32+
headers=headers,
33+
timeout=70,
34+
ssl_context=ssl_context,
35+
raise_for_status=True,
36+
) as response:
37+
value = await response.json()
38+
return value
39+
40+
41+
async def delete(*, session, url, data, headers=None):
42+
ssl_context = ssl.create_default_context(cafile=certifi.where())
43+
44+
async with session.delete(
45+
url,
46+
json=data,
47+
headers=headers,
48+
timeout=70,
49+
ssl_context=ssl_context,
50+
raise_for_status=True,
51+
) as response:
52+
value = await response.json()
53+
return value
54+
55+
56+
async def list_repos(*, session, org, token):
57+
headers = {
58+
"Authorization": "token {token}".format(token=token),
59+
"Accept": "application/vnd.github.v3+json",
60+
}
61+
url = REPOS_URL.format(org=org)
62+
click.echo("Retrieving repos at {}".format(url))
63+
response = await get(session=session, url=url, headers=headers)
64+
return response

github_deploy/commands/_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ def get_repo(*, org, project):
55
def can_upload(*, repo, include_private):
66
return (
77
True
8-
if include_private and repo["private"] == True
8+
if include_private and repo["private"] is True
99
else not repo["private"]
1010
)

github_deploy/commands/delete.py

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,13 @@
11
import asyncio
2-
import ssl
32

43
import aiohttp
54
import asyncclick as click
6-
import certifi
75

8-
from github_deploy.commands._constants import BASE_URL, REPOS_URL
6+
from github_deploy.commands._constants import BASE_URL
7+
from github_deploy.commands._http_utils import delete, get, list_repos
98
from github_deploy.commands._utils import get_repo
109

1110

12-
async def get(*, session, url, headers=None, skip_missing=False):
13-
ssl_context = ssl.create_default_context(cafile=certifi.where())
14-
15-
async with session.get(
16-
url,
17-
headers=headers,
18-
timeout=70,
19-
ssl_context=ssl_context,
20-
raise_for_status=not skip_missing,
21-
) as response:
22-
if skip_missing and response.status == 404:
23-
return {}
24-
25-
value = await response.json()
26-
return value
27-
28-
29-
async def delete(*, session, url, data, headers=None):
30-
ssl_context = ssl.create_default_context(cafile=certifi.where())
31-
32-
async with session.delete(
33-
url,
34-
json=data,
35-
headers=headers,
36-
timeout=70,
37-
ssl_context=ssl_context,
38-
raise_for_status=True,
39-
) as response:
40-
value = await response.json()
41-
return value
42-
43-
4411
async def delete_content(
4512
*,
4613
session,
@@ -135,17 +102,6 @@ async def handle_file_delete(*, repo, dest, token, semaphore, session):
135102
)
136103

137104

138-
async def list_repos(*, session, org, token):
139-
headers = {
140-
"Authorization": "token {token}".format(token=token),
141-
"Accept": "application/vnd.github.v3+json",
142-
}
143-
url = REPOS_URL.format(org=org)
144-
click.echo("Retrieving repos at {}".format(url))
145-
response = await get(session=session, url=url, headers=headers)
146-
return response
147-
148-
149105
@click.command()
150106
@click.option(
151107
"--org",

github_deploy/commands/upload.py

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,15 @@
11
import asyncio
22
import base64
3-
import ssl
43

54
import aiofiles
65
import aiohttp
76
import asyncclick as click
8-
import certifi
97

10-
from github_deploy.commands._constants import BASE_URL, REPOS_URL
8+
from github_deploy.commands._constants import BASE_URL
9+
from github_deploy.commands._http_utils import put, list_repos, get
1110
from github_deploy.commands._utils import get_repo, can_upload
1211

1312

14-
async def get(*, session, url, headers=None, skip_missing=False):
15-
ssl_context = ssl.create_default_context(cafile=certifi.where())
16-
17-
async with session.get(
18-
url,
19-
headers=headers,
20-
timeout=70,
21-
ssl_context=ssl_context,
22-
raise_for_status=not skip_missing,
23-
) as response:
24-
if skip_missing and response.status == 404:
25-
return {}
26-
27-
value = await response.json()
28-
return value
29-
30-
31-
async def put(*, session, url, data, headers=None):
32-
ssl_context = ssl.create_default_context(cafile=certifi.where())
33-
34-
async with session.put(
35-
url,
36-
json=data,
37-
headers=headers,
38-
timeout=70,
39-
ssl_context=ssl_context,
40-
raise_for_status=True,
41-
) as response:
42-
value = await response.json()
43-
return value
44-
45-
4613
async def upload_content(
4714
*,
4815
session,
@@ -165,17 +132,6 @@ async def handle_file_upload(
165132
)
166133

167134

168-
async def list_repos(*, session, org, token):
169-
headers = {
170-
"Authorization": "token {token}".format(token=token),
171-
"Accept": "application/vnd.github.v3+json",
172-
}
173-
url = REPOS_URL.format(org=org)
174-
click.echo("Retrieving repos at {}".format(url))
175-
response = await get(session=session, url=url, headers=headers)
176-
return response
177-
178-
179135
@click.command()
180136
@click.option(
181137
"--org",

0 commit comments

Comments
 (0)