Skip to content

Commit d406c3a

Browse files
committed
minor fixes, typing, indexing, typo
1 parent 3674291 commit d406c3a

File tree

3 files changed

+35
-34
lines changed

3 files changed

+35
-34
lines changed

src/writegithubstat/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .githubstat import GithubAuth, WriteGithubStat, Referrers, Paths, StarsForks, ViewsClones

src/writegithubstat/githubstat.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import logging
55
import requests
66
from datetime import date
7-
7+
from typing import Dict, Any, List, Union
88
import pandas as pd
99

1010

@@ -15,7 +15,7 @@ def __init__(self, owner: str, repo: str, token: str) -> None:
1515
self._header = self._get_auth_header(token)
1616

1717
@staticmethod
18-
def _get_auth_header(token: str) -> dict:
18+
def _get_auth_header(token: str) -> Dict[str, str]:
1919
auth_header = {
2020
"Authorization": f"token {token}",
2121
"Accept": "application/vnd.github.spiderman-preview+json",
@@ -31,7 +31,7 @@ def repo(self) -> str:
3131
return self._repo
3232

3333
@property
34-
def header(self) -> dict:
34+
def header(self) -> Dict[str, str]:
3535
return self._header
3636

3737

@@ -42,80 +42,80 @@ def __init__(self, owner: str, repo: str) -> None:
4242

4343
@property
4444
@abstractmethod
45-
def urls(self):
45+
def urls(self) -> List[str]:
4646
pass
4747

4848
@property
4949
@abstractmethod
50-
def dimensions(self):
50+
def dimensions(self) -> List[str]:
5151
pass
5252

5353
@property
5454
@abstractmethod
55-
def measures(self):
55+
def measures(self) -> List[str]:
5656
pass
5757

5858
@abstractmethod
59-
def process_stat(self, responses):
59+
def process_stat(self, responses: List[Dict[str, Any]]) -> pd.DataFrame:
6060
pass
6161

6262

6363
class Referrers(GithubStatType):
6464
@property
65-
def urls(self):
65+
def urls(self) -> List[str]:
6666
return [
6767
f"https://api.github.com/repos/{self._owner}/{self._repo}/traffic/popular/referrers"
6868
]
6969

7070
@property
71-
def dimensions(self):
71+
def dimensions(self) -> List[str]:
7272
return ["referrer"]
7373

7474
@property
75-
def measures(self):
75+
def measures(self) -> List[str]:
7676
return ["count", "uniques"]
7777

78-
def process_stat(self, responses):
78+
def process_stat(self, responses: List[Dict[str, Any]]) -> pd.DataFrame:
7979
data = responses[0]
8080
df = pd.DataFrame(data)
8181
return df
8282

8383

8484
class Paths(GithubStatType):
8585
@property
86-
def urls(self):
86+
def urls(self) -> List[str]:
8787
return [
8888
f"https://api.github.com/repos/{self._owner}/{self._repo}/traffic/popular/paths"
8989
]
9090

9191
@property
92-
def dimensions(self):
92+
def dimensions(self) -> List[str]:
9393
return ["path", "title"]
9494

9595
@property
96-
def measures(self):
96+
def measures(self) -> List[str]:
9797
return ["count", "uniques"]
9898

99-
def process_stat(self, responses):
99+
def process_stat(self, responses: List[Dict[str, Any]]) -> pd.DataFrame:
100100
data = responses[0]
101101
df = pd.DataFrame(data)
102102
return df
103103

104104

105105
class StarsForks(GithubStatType):
106106
@property
107-
def urls(self):
107+
def urls(self) -> List[str]:
108108
return [f"https://api.github.com/repos/{self._owner}/{self._repo}"]
109109

110110
@property
111-
def dimensions(self):
111+
def dimensions(self) -> List[str]:
112112
return []
113113

114114
@property
115-
def measures(self):
115+
def measures(self) -> List[str]:
116116
return ["stars", "forks"]
117117

118-
def process_stat(self, responses):
118+
def process_stat(self, responses: List[Dict[str, Any]]) -> pd.DataFrame:
119119
data = responses[0]
120120
stars = data["stargazers_count"]
121121
forks = data["forks_count"]
@@ -129,23 +129,23 @@ def __init__(self, owner: str, repo: str, date: str) -> None:
129129
self._date = date
130130

131131
@property
132-
def urls(self):
132+
def urls(self) -> List[str]:
133133
return [
134134
f"https://api.github.com/repos/{self._owner}/{self._repo}/traffic/views",
135135
f"https://api.github.com/repos/{self._owner}/{self._repo}/traffic/clones",
136136
]
137137

138138
@property
139-
def dimensions(self):
139+
def dimensions(self) -> List[str]:
140140
return []
141141

142142
@property
143-
def measures(self):
144-
return ["views_total", "views_unique", "clones_total", "clones_total"]
143+
def measures(self) -> List[str]:
144+
return ["views_total", "views_unique", "clones_total", "clones_unique"]
145145

146-
def process_stat(self, responses):
146+
def process_stat(self, responses: List[Dict[str, Any]]) -> pd.DataFrame:
147147
views = self._get_actual_stat(responses[0], "views")
148-
clones = self._get_actual_stat(responses[0], "clones")
148+
clones = self._get_actual_stat(responses[1], "clones")
149149
df = pd.DataFrame(
150150
{
151151
"views_total": [views["count"]],
@@ -156,7 +156,9 @@ def process_stat(self, responses):
156156
)
157157
return df
158158

159-
def _get_actual_stat(self, data, name):
159+
def _get_actual_stat(
160+
self, data: Dict[str, Any], name: str
161+
) -> Dict[str, Union[int, str]]:
160162
try:
161163
stat = data[name][-1]
162164
if not stat["timestamp"].startswith(self._date):
@@ -170,16 +172,14 @@ def _get_actual_stat(self, data, name):
170172

171173
class GithubStatAPI:
172174
@staticmethod
173-
def get_stat(stat_type: GithubStatType, auth_header: dict) -> pd.DataFrame:
175+
def get_stat(
176+
stat_type: GithubStatType, auth_header: Dict[str, str]
177+
) -> pd.DataFrame:
174178
responses = []
175179
for url in stat_type.urls:
176180
response = requests.get(url, headers=auth_header)
177-
if response.status_code == 200:
178-
responses.append(response.json())
179-
else:
180-
raise requests.HTTPError(
181-
f"Request failed with status code {response.status_code}: {response.text}"
182-
)
181+
response.raise_for_status()
182+
responses.append(response.json())
183183
return stat_type.process_stat(responses)
184184

185185

@@ -216,7 +216,7 @@ def _insert_metadata(self, df: pd.DataFrame) -> pd.DataFrame:
216216
df.insert(2, "repo", self._auth.repo)
217217
return df
218218

219-
def _get_stored_stats(self, path: str) -> pd.DataFrame:
219+
def _get_stored_stats(self, path: Union[str, Path]) -> pd.DataFrame:
220220
try:
221221
df = pd.read_csv(path)
222222
return df

src/writegithubstat/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)