Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async client using httpx #55

Merged
merged 15 commits into from
Dec 15, 2023
Merged

Async client using httpx #55

merged 15 commits into from
Dec 15, 2023

Conversation

tiwilliam
Copy link
Contributor

Please note that this PR include commits from #54 which should be rebased before merge.

This PR adds an asynchronous client backed by httpx. I've decided to keep the sync clients API as is for full backwards-compatibility, it simply wraps the async code.

I've updated documentation accordingly and also took some freedom to restructure parts of it to make more sense with this new client setup.

I also created some VSCode project setting for this project, no strong opinion to include them but took me some minutes to get the environment rolling with linter and formatter without it.

Example usage:

>>> import bankid
>>> client = bankid.AsyncBankIDJSONClient(certificates=(
...     'path/to/certificate.pem',
...     'path/to/key.pem',
... ))
>>> await client.authenticate(end_user_ip='194.168.2.25')
{
    'orderRef': 'ee3421ea-2096-4000-8130-82648efe0927',
    'autoStartToken': 'e8df5c3c-c67b-4a01-bfe5-fefeab760beb',
    'qrStartToken': '01f94e28-857f-4d8a-bf8e-6c5a24466658',
    'qrStartSecret': 'b4214886-3b5b-46ab-bc08-6862fddc0e06'
}

...and sync:

>>> import bankid
>>> client = bankid.BankIDJSONClient(certificates=(
...     'path/to/certificate.pem',
...     'path/to/key.pem',
... ))
>>> client.authenticate(end_user_ip='194.168.2.25')
{
    'orderRef': 'ee3421ea-2096-4000-8130-82648efe0927',
    'autoStartToken': 'e8df5c3c-c67b-4a01-bfe5-fefeab760beb',
    'qrStartToken': '01f94e28-857f-4d8a-bf8e-6c5a24466658',
    'qrStartSecret': 'b4214886-3b5b-46ab-bc08-6862fddc0e06'
}

Preview of documentation updates:
Screenshot 2023-11-08 at 00 11 23

Preview of updated getting started:
Screenshot 2023-11-08 at 00 11 35

@tiwilliam
Copy link
Contributor Author

Resolves #53.

Copy link
Collaborator

@Majsvaffla Majsvaffla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only looked at this briefly for now. Nice things 😎

bankid/jsonclient.py Outdated Show resolved Hide resolved
bankid/jsonclient.py Outdated Show resolved Hide resolved

response = await self.client.post(
self._collect_endpoint,
json=dict(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't keep the dict literals here? Is it a linter/formatter thing? Not that it matters a lot in this case but AFAIK it performs better with literals.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only a personal preference, like getting better syntax coloring. No strong opinion though.

bankid/jsonclient.py Outdated Show resolved Hide resolved
bankid/jsonclient.py Outdated Show resolved Hide resolved
tiwilliam and others added 4 commits November 8, 2023 11:06
Co-authored-by: David Svenson <davidsvenson@outlook.com>
Co-authored-by: David Svenson <davidsvenson@outlook.com>
Co-authored-by: David Svenson <davidsvenson@outlook.com>
@hbldh hbldh changed the base branch from master to develop December 15, 2023 13:06
@hbldh
Copy link
Owner

hbldh commented Dec 15, 2023

@tiwilliam I would like it merge this into pybankid, because it is functionality that is highly desired, but there are too many things I want to separate in this.

If you develop a async client, I would prefer to have the async client and its documentation added, not also some cleaning of Python 2.7 relics, removal of file headers you did not agree with, a restructuring of the documentation to how you prefer to have it and definitely not replacing the fully functioning synchronous client with a asyncio wrapper around your async client. For synchronous handling, I would prefer to use the regular httpx.Client class instead of wrapping the httpx.AsyncClient in a temporary event loop and run it.

These should in my opinion be done in several PRs, each one focusing on one thing. This is just too much disparate changes in one PR. It changes everything and potentially destabilizes things in doing so.

That said, I plan to merge this into the develop branch and do some reworking of the internals. Thank you for you contribution, but please do smaller changes in the future, focusing on one thing at a time.

@hbldh hbldh merged commit b742236 into hbldh:develop Dec 15, 2023
18 checks passed
@tiwilliam
Copy link
Contributor Author

@hbldh I could have cleaned this up a bit or split it in separate PRs and iterated on keeping the sync code approach if you just would have given me the chance after feedback. Some of the Python 2.7 changes are from #54 and can be easily rebased as mention in the first comment, I wanted to avoid some merge conflicts which could have been messy.

@tiwilliam tiwilliam deleted the async branch December 23, 2023 11:28
@hbldh hbldh mentioned this pull request Jan 23, 2024
@hbldh hbldh mentioned this pull request Mar 21, 2024
@hbldh
Copy link
Owner

hbldh commented Mar 28, 2024

Version 1.0.0 of PyBankID, with async and sync clients integrating with the v6 BankID API, has now been officially released and is available on PyPI.

hbldh added a commit that referenced this pull request Apr 24, 2024
* Update README.rst

Corrected typo in examples

* Update __init__.py

Fix breakage with urllib 2.0.x

* Github Action fixes

* Version bump

* Bump reqs for example

* Bundle the BankID Test certificate

The BankID pages now returns a captcha instead of the actual certificate when fetching with requests. The actual cert is now bundled instead of fetched each time.

* Failure detection in openssl test cert conversion

* Add possibility to provide p12 test cert through existing file

* Bundle the BankID Test certificate in pem format

Bundle pem formats as well.

* Python 2.7 compat. fix

* Rmoving certutils test for the time being

* Remove Python 2.7 support

* Remove six dependency

* Async client using httpx (#55)

* Test against Python 3.12

* Install setuptools after testing

* Swap out pkg_resources for importlib

* Downgrade importlib-resources to 5.12.0

* Always use compat package

* Read required packages in setup.py from requirements.txt

* Drop unused six and update docs

* Async client

* Tidy up async wrapper

* Install requirements-dev.txt on CI

* Add two more packages to requirements-dev.txt from CI

* Update bankid/jsonclient.py

Co-authored-by: David Svenson <davidsvenson@outlook.com>

* Update bankid/jsonclient.py

Co-authored-by: David Svenson <davidsvenson@outlook.com>

* Drop unused TypeVar

* Update bankid/jsonclient.py

Co-authored-by: David Svenson <davidsvenson@outlook.com>

---------

Co-authored-by: David Svenson <davidsvenson@outlook.com>

* Add support for RP v6.0

* First draft of v6 clients

Sync and Async clients
Implementing parts of v6 API
Removing all v5 and v5.1 API implementations
Lacking documentation rewrite

Builds on #53, #54, #56, #57, #58

* Corrected the example app to work with v1.0.0

* Documentation update

* Cleanup before PR

Documentation fixes
Renaming and docstring fixes
Demo app modifications
Version bump

* Remove .vscode folder

* Minor doc change

* Updated README.rst

* CI changes

Removed testing in windows and macos
Also removed 3.7 and 3.8 from test matrix.

* Upgrading CI action versions

* Implemented phone/auth and phone/sign

* Update certutils.py

Make it even easier to retrieve the test certificate by writing it into the current directory if no path is supplied.

* Expose QR code helper explicitly.

This simplifies making use of it without having access to a client instance.

* Update README.rst - use pytest instead of py.test

pytest is the "new" name :)

* Version 1.0.1 - Docfix and QR method separate

* Cache ip addresses in test suite.

Also, only keep a sync version of the ip_address fixture.

This avoids httpbin flakyness/unrelibility since the ip address fetch
only needs to happen once.

* Use builtin importlib.resources.

Supporting Python >=3.9 does not require using the backport.

Also, use joinpath() to simplify the retrival of the path+return
pathlib.Path instead of str.

* Documentation updates

* Dropping use of httpbin for external ip

* Fix for async test

* Add mypy to dev deps.

* mypy --install-types

* Drop duplicate method.

* Fix type errors and add type annotations.

* Add type checking to CI.

* Add CONTRIBUTING.md

---------

Co-authored-by: Simon Olofsson <36161882+dotchetter@users.noreply.github.com>
Co-authored-by: Colin 't Hart <colinthart@gmail.com>
Co-authored-by: Stefan Berg <sfb@consultron.com>
Co-authored-by: William Tisäter <william@defunct.cc>
Co-authored-by: David Svenson <davidsvenson@outlook.com>
Co-authored-by: Amin Solhizadeh <amin.solhizadeh@oneflow.com>
Co-authored-by: Andreas Pelme <andreas@pelme.se>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants