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

Fix: close client session to avoid resource leak #70

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

guyskk
Copy link

@guyskk guyskk commented Feb 22, 2022

Fix requests session not closed which will cause resource leak.

@guyskk
Copy link
Author

guyskk commented Feb 24, 2022

Hi @matin, any feedbacks on the pull request?

@matin matin self-requested a review February 24, 2022 15:01
@matin
Copy link
Member

matin commented Feb 24, 2022

@guyskk thanks for the contribution! This approach would definitely work, but what about the following approach instead?

from datetime import date

from cep import Transferencia

tr_details = dict(
    fecha=date(2019, 4, 12),
    clave_rastreo='CUENCA1555093850',
    emisor='90646',  # STP
    receptor='40012',  # BBVA
    cuenta='012180004643051249',
    monto=8.17,
)
with Transferencia.validar(**tr_details) as tr:
    pdf = tr.descargar()

Using contextlib for the above would make it more consistent with how sessions, clients, etc that need to be closed after an operation (successful or failed) are typically mitigated. It also removes the need to manually call Client.close().

What do you think?

@guyskk
Copy link
Author

guyskk commented Feb 24, 2022

@matin The problem is Transferencia.validar may return None, which cause not able to use context manager.

If we can move validar and descargar to another class (or client class), then use context manager is straightforward. eg:

with TransferenciaClient() as client:
    tr = client.validar(**tr_details)
    pdf = tr.descargar()

But it may be a break change, or keep the old api, only deprecate them.

@guyskk
Copy link
Author

guyskk commented Mar 3, 2022

Hi @matin, I implemented transferencia client use context manager, and reserved all existing api, only deprecated them in docs. Could you help review again?

@matin
Copy link
Member

matin commented Mar 4, 2022

Hi @matin, I implemented transferencia client use context manager, and reserved all existing api, only deprecated them in docs. Could you help review again?

Looks good to me.

@rogelioLpz can you review as well?

@matin matin requested review from rogelioLpz and removed request for matin March 4, 2022 21:15
@matin
Copy link
Member

matin commented Mar 4, 2022

@guyskk can you bump the version as well?

@guyskk
Copy link
Author

guyskk commented Mar 7, 2022

@matin bumped version from 0.1.7 to 0.2.0, increased minor version because the PR add new APIs.

@matin
Copy link
Member

matin commented Mar 7, 2022

Leaving to @rogelioLpz for final review

@rogelioLpz
Copy link
Member

Hi @guyskk and thanks for the contribution!
Not is necessary maintain the both class Transferencia and TransferenciaClient

Please apply your changes in Transferencia and delete TransferenciaClient

@guyskk
Copy link
Author

guyskk commented Mar 12, 2022

Hi @rogelioLpz , the Transferencia.validar may return None, which cause not able to use context manager, so we have to add another class. #70 (comment)

@guyskk
Copy link
Author

guyskk commented Apr 13, 2022

Hi @rogelioLpz , any suggestions or feedbacks on the PR?

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