Skip to content

Commit

Permalink
add arguments access_token and app_key to method Site.__init__
Browse files Browse the repository at this point in the history
  • Loading branch information
wchistow committed Jul 6, 2023
1 parent d24b1bb commit 3bf6763
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ToDo

+ [X] fix type of `**kwargs` argument in method `Site.get`. Now it's `dict[str, Any]`, but have to be only `Any`;
+ [ ] remove argument `api_key` of method `Site.__init__` and add `access_token` and `auth_token` arguments to it. Also, add usage of these arguments to method `Site.get`;
+ [X] remove argument `api_key` of method `Site.__init__` and add `access_token` and `app_key` arguments to it. Also, add usage of these arguments to method `Site.get`;
+ [X] sort constants in file `src/pystackapi/sites.py` by alphabet order;
+ [X] change type of return value in method `Site.get` and, also in `Site.get_*` from `dict` to subclass of `typing.TypedDict`;
+ [ ] add more examples to directory `examples/`;
Expand Down
12 changes: 8 additions & 4 deletions src/pystackapi/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ class Site:
version = '2.3'
base_url = f'https://api.stackexchange.com/{version}/'

def __init__(self, name: str, api_key: str | None = None) -> None:
def __init__(self, name: str, access_token: str | None = None,
app_key: str | None = None) -> None:
self.name = name
self.api_key = api_key
self.access_token = access_token
self.app_key = app_key

def get(self, query: str, **kwargs: Any) -> ResponseDict:
"""Returns result of calling of `query` to API."""
params = f'?site={self.name}'
if self.api_key is not None:
params += f'&access_token={self.api_key}'
if self.access_token is not None:
params += f'&access_token={self.access_token}'
if self.app_key is not None:
params += f'&key={self.app_key}'

if kwargs:
params += '&' + '&'.join((f'{k}={v}' for k, v in kwargs.items()))
Expand Down
18 changes: 18 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ def test_get_with_kwargs() -> None:
'https://api.stackexchange.com/2.3/ghgh/?site=stackoverflow&arg1=hello')


@lest.register
def test_get_with_access_token() -> None:
l_site = site_m.Site('stackoverflow', access_token='someaccesstoken')
l_site.get('ghgh/')

lest.assert_eq(requests.url,
'https://api.stackexchange.com/2.3/ghgh/?site=stackoverflow&access_token=someaccesstoken')


@lest.register
def test_get_with_app_key() -> None:
l_site = site_m.Site('stackoverflow', app_key='someappkey')
l_site.get('ghgh/')

lest.assert_eq(requests.url,
'https://api.stackexchange.com/2.3/ghgh/?site=stackoverflow&key=someappkey')


@lest.register
def test_get_info() -> None:
site.get_info()
Expand Down

0 comments on commit 3bf6763

Please sign in to comment.