Skip to content

Commit

Permalink
bump version to 1.0.0rc1
Browse files Browse the repository at this point in the history
  • Loading branch information
konstantintogoi committed Nov 30, 2024
1 parent 5892124 commit b09900a
Show file tree
Hide file tree
Showing 36 changed files with 1,797 additions and 2,359 deletions.
19 changes: 19 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Read the Docs configuration file for MkDocs projects
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

# Required
version: 2

# Set the version of Python and other tools you might need
build:
os: ubuntu-22.04
tools:
python: "3.7"

sphinx:
configuration: docs/source/conf.py

python:
install:
- requirements: docs/requirements.txt

25 changes: 0 additions & 25 deletions .travis.yml

This file was deleted.

18 changes: 18 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
httpx = "*"

[dev-packages]
sphinx = "*"
mkdocs = "*"
mkdocs-material = "*"
pytest = "*"
pytest-asyncio = "*"
pytest-localserver = "*"

[requires]
python_version = "3.7"
827 changes: 827 additions & 0 deletions Pipfile.lock

Large diffs are not rendered by default.

101 changes: 24 additions & 77 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.. image:: https://img.shields.io/badge/license-BSD-blue.svg
:target: https://github.com/KonstantinTogoi/aiookru/blob/master/LICENSE
:target: https://github.com/konstantintogoi/aiookru/blob/master/LICENSE

.. image:: https://img.shields.io/pypi/v/aiookru.svg
:target: https://pypi.python.org/pypi/aiookru
Expand All @@ -8,112 +8,59 @@
:target: https://pypi.python.org/pypi/aiookru

.. image:: https://readthedocs.org/projects/aiookru/badge/?version=latest
:target: https://aiookru.readthedocs.io/en/latest/
:target: https://aiookru.readthedocs.io/en/latest

.. image:: https://travis-ci.org/KonstantinTogoi/aiookru.svg
:target: https://travis-ci.org/KonstantinTogoi/aiookru
.. image:: https://github.com/konstantintogoi/aiookru/actions/workflows/pages/pages-build-deployment/badge.svg
:target: https://konstantintogoi.github.io/aiookru

.. index-start-marker1
aiookru
=======

aiookru is a python `ok.ru API <https://apiok.ru/>`_ wrapper.
The main features are:

* authorization (`Authorization Code <https://oauth.net/2/grant-types/authorization-code/>`_, `Implicit Flow <https://oauth.net/2/grant-types/implicit/>`_, `Password Grant <https://oauth.net/2/grant-types/password/>`_, `Refresh Token <https://oauth.net/2/grant-types/refresh-token/>`_)
* `REST API <https://apiok.ru/en/dev/methods/rest>`_ methods

async python `ok.ru API <https://apiok.ru/>`_ wrapper
for `REST API <https://apiok.ru/en/dev/methods/rest>`_ methods.

Usage
-----

To use `ok.ru API <https://apiok.ru/>`_ you need a registered app
and `ok.ru <https://ok.ru>`_ account.
For more details, see
`aiookru Documentation <https://aiookru.readthedocs.io/>`_.

Client application
~~~~~~~~~~~~~~~~~~

Use :code:`ClientSession` when REST API is needed in:

- client component of the client-server application
- standalone mobile/desktop application

i.e. when you embed your app's info (application key) in publicly available code.
To use `ok.ru API <https://apiok.ru/>`_ you need a registered app and an :code:`access_token`.

.. code-block:: python
from aiookru import ClientSession, API
import aiookru
session = ClientSession(app_id, app_key, access_token, session_secret_key)
api = API(session)
client_id = '12345678'
application_key = 'ABCDEFGHIJKLMNOPQ'
application_secret_key = '0A1B2C3D4E5F6G7H8I9K10L11M12N13O14P15Q'
redirect_uri = 'http://apiok.ru/oauth_callback'
events = await api.events.get()
friends = await api.friends.get()
Pass :code:`session_secret_key` and :code:`access_token`
that were received after authorization.
For more details, see
`authorization instruction <https://aiookru.readthedocs.io/en/latest/authorization.html>`_.

Server application
~~~~~~~~~~~~~~~~~~

Use :code:`ServerSession` when REST API is needed in:

- server component of the client-server application
- requests from your servers

.. code-block:: python
code = '' # get code from login form
from aiookru import ServerSession, API
async with aiookru.CodeGrant(client_id, application_secret_key, redirect_uri, code) as grant:
access_token = grant.access_token
refresh_token = grant.refresh_token
session = ServerSession(app_id, app_key, app_secret_key, access_token)
api = API(session)
async with aiookru.API(access_token, application_key, application_secret_key=application_secret_key) as okru:
events = await okru.events.get()
events = await api.events.get()
friends = await api.friends.get()
async with aiookru.RefreshGrant(client_id, application_secret_key, refresh_token) as grant:
access_token = grant.access_token
Pass :code:`app_secret_key` and :code:`access_token` that was received after authorization.
For more details, see
`authorization instruction <https://aiookru.readthedocs.io/en/latest/authorization.html>`_.
For more details, see `authorization instruction <https://konstantintogoi.github.io/aiookru/authorization>`_.

Installation
------------

.. code-block:: shell
pip install aiookru
$ pip install aiookru
or

.. code-block::
python setup.py install
Supported Python Versions
-------------------------

Python 3.5, 3.6, 3.7 and 3.8 are supported.

.. index-end-marker1
Test
----

Run all tests.

.. code-block:: shell
python setup.py test
Run tests with PyTest.

.. code-block:: shell
~~~~~~~~~~~~~~~~~~~~~~~~~

python -m pytest [-k TEST_NAME]
Python 3.7, 3.8, 3.9 are supported.

License
-------
Expand Down
31 changes: 3 additions & 28 deletions aiookru/__init__.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,3 @@
from . import exceptions, utils, parsers, sessions, api
from .exceptions import (
Error,
OAuthError,
InvalidGrantError,
InvalidClientError,
InvalidUserError,
APIError,
EmptyResponseError,
)
from .sessions import (
PublicSession,
TokenSession,
ClientSession,
ServerSession,
CodeSession,
CodeServerSession,
ImplicitSession,
ImplicitClientSession,
PasswordSession,
PasswordClientSession,
RefreshSession,
RefreshServerSession,
)
from .api import API


__version__ = '0.1.1.post1'
"""aiookru."""
from .api import API # noqa: F401
from .auth import CodeGrant, RefreshGrant # noqa: F401
91 changes: 78 additions & 13 deletions aiookru/api.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,100 @@
"""ok.ru API."""
from hashlib import md5
from typing import Any, Dict, Generator, Tuple

from .sessions import TokenSession
from .session import TokenSession


class API:
"""ok.ru API."""
"""ok.ru API.
Attributes:
session (TokenSession): session.
"""

__slots__ = ('session', )

def __init__(self, session: TokenSession):
self.session = session
def __init__(
self,
access_token: str,
application_key: str,
session_secret_key: str = '',
application_secret_key: str = '',
):
"""Set session."""
if session_secret_key:
secret_key = session_secret_key
elif application_secret_key:
plain_secret = access_token + application_secret_key
secret_key = md5(plain_secret.encode('utf-8')).hexdigest().lower()
else:
secret_key = ''
self.session = TokenSession(
application_key=application_key,
access_token=access_token,
secret_key=secret_key,
)

def __getattr__(self, name):
def __await__(self) -> Generator['API', None, None]:
"""Await self."""
yield self

async def __aenter__(self) -> 'API':
"""Enter."""
return self

async def __aexit__(self, *args: Tuple[Any, Any, Any]) -> None:
"""Exit."""
if not self.session.client.is_closed:
await self.session.client.aclose()

def __getattr__(self, name: str):
"""Return an API method."""
return APIMethod(self, name)

async def __call__(self, name, **params):
async def __call__(self, name: str, **params: Dict[str, Any]) -> 'APIMethod': # noqa
"""Call an API method by its name.
Args:
name (str): full method's name
params (Dict[str, Any]): query parameters
Return:
APIMethod
"""
return await getattr(self, name)(**params)


class APIMethod:
"""ok.ru REST API method."""

__slots__ = ('api', 'name')
__slots__ = ('_api', '_name')

def __init__(self, api: API, name: str):
self.api = api
self.name = name
"""Set method name."""
self._api = api
self._name = name

def __getattr__(self, name):
return APIMethod(self.api, self.name + '.' + name)
"""Chain methods.
Args:
name (str): method name
"""
return APIMethod(self._api, f'{self._name}.{name}')

async def __call__(self, **params: Dict[str, Any]) -> Dict[str, Any]:
"""Execute a request.
Args:
params (Dict[str, Any]): query parameters
Returns:
Dict[str, Any]
async def __call__(self, *args, **params):
params['method'] = self.name
return await self.api.session.request(params=params)
"""
params['method'] = self._name
return await self._api.session.request(params=params)
Loading

0 comments on commit b09900a

Please sign in to comment.