Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wkpn committed Feb 19, 2020
1 parent f373fad commit b418b37
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ Simple usage (in this case ``client.close()`` must be called when client is no l
>>> from aio2ch import Api
>>> client = Api()
...
>>> ...
>>> await client.close()
Or you can use it as a context manager

.. code-block:: python
>>> async with Api() as client:
... await client.get_boards()
... boards = await client.get_boards()
Get all boards

Expand Down
16 changes: 11 additions & 5 deletions aio2ch/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@
class Api:
__slots__ = '_api_client'

def __init__(self, api_url: Optional[str] = None):
self._api_client: ApiClient = ApiClient(api_url=api_url)
def __init__(self, api_url: Optional[str] = None, **kwargs: Any):
"""
main API class
:param api_url: main API endpoint. can be any of these: 2ch.hk, 2ch.pm (rest are redirects to 2ch.hk)
:param kwargs: any additional args you want to pass to the client e.g. proxies or headers
"""

self._api_client: ApiClient = ApiClient(api_url=api_url, **kwargs)

async def get_boards(self,
return_status: Optional[bool] = False
Expand Down Expand Up @@ -57,7 +63,7 @@ async def get_board_threads(self,

if keywords:
keywords = tuple(keyword.lower() for keyword in keywords)
threads = [thread for thread in threads if any(k in thread.comment.lower() for k in keywords)]
threads = tuple(thread for thread in threads if any(k in thread.comment.lower() for k in keywords))

if return_status:
return status, threads
Expand Down Expand Up @@ -185,8 +191,8 @@ async def download(api_client, semaphore, file):

await asyncio.gather(*download_tasks)

async def _get(self, url, **kwargs: Any) -> Tuple[int, Dict]:
status_code, json_data = await self._api_client.request(method='GET', url=url, **kwargs)
async def _get(self, url) -> Tuple[int, Dict]:
status_code, json_data = await self._api_client.request(method='GET', url=url)
return status_code, json_data

async def close(self) -> None:
Expand Down
10 changes: 2 additions & 8 deletions aio2ch/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,17 @@

class ApiClient:
def __init__(self, api_url: Optional[str], **kwargs: Any):
"""
ApiClient
:param api_url: main API endpoint. can be any of these: 2ch.hk, 2ch.pm (rest are redirects to 2ch.hk)
:param kwargs: any additional args you want to pass to the client e.g. proxies
"""
self._api_url: str = API_URL if not api_url else api_url
self.client: AsyncClient = AsyncClient(**kwargs)

async def request(self, method: str, url: str, **kwargs: Any) -> Tuple[int, Dict]:
async def request(self, method: str, url: str) -> Tuple[int, Dict]:
"""
Request
:param method: request method
:param url: request url
:param kwargs: any additional args you want to pass e.g. headers, cookies etc.
:return: response with status code and json data on success
"""
response = await self.client.request(method=method, url=url, **kwargs)
response = await self.client.request(method=method, url=url)

return response.status_code, response.json()

Expand Down
18 changes: 11 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@
'httpx==0.11.1',
'aiofiles'
],
tests_require=['pytest', 'pytest-asyncio', 'pytest-cov'],
tests_require=[
'pytest',
'pytest-asyncio',
'pytest-cov'
],
keywords=['2ch', 'Двач', 'Dvach', 'api', 'wrapper', 'async'],
zip_safe=False,
classifiers=[
'Development Status :: 5 - Production/Stable',
'Framework :: AsyncIO',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7'
'Development Status :: 5 - Production/Stable',
'Framework :: AsyncIO',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7'
]
)

0 comments on commit b418b37

Please sign in to comment.