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

Refactored how we get results from Mercadopago. #271

Merged
merged 5 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ jobs:
python-version: 3.9
- name: Bootstrap everything
run: |
pip install -U docker-compose
pip install -U "docker < 7" # compatibility issues; see https://github.com/docker/docker-py/issues/3194
pip install -U docker-compose
docker-compose up -d
docker-compose exec -T web pip install -r /code/config/requirements-dev.txt
- name: Run tests
Expand Down
2 changes: 1 addition & 1 deletion config/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ django-storages[azure]==1.11.1
django==3.1.14
google-api-python-client==1.12.8
gunicorn==20.0.4
mercadopago==1.1.1
mercadopago==2.2.1
oauth2client==4.1.3
Pillow==10.0.1
psycopg2_binary==2.8.6
Expand Down
33 changes: 22 additions & 11 deletions website/members/management/commands/_mp.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
import logging
import os

from mercadopago import MP
from mercadopago import SDK

LIMIT = 500

logger = logging.getLogger('mercadopago')


def get_raw_mercadopago_info():
"""Get records from Mercadopago API."""
mercadopago_client_id = os.getenv('MERCADOPAGO_CLIENT_ID')
mercadopago_client_secret = os.getenv('MERCADOPAGO_CLIENT_SECRET')
"""Get records from Mercadopago API.

mp = MP(mercadopago_client_id, mercadopago_client_secret)
As different hits bring different "total" values indicated, we record what is the
max of those, and only stop hitting when we get no results from one of those endpoints.
"""
logger.debug('Connecting with mercadopago')
auth_token = os.getenv('MERCADOPAGO_AUTH_TOKEN')
mp = SDK(auth_token)

filters = {'status': 'approved'}
filters = {'status': 'approved', 'limit': LIMIT, 'begin_date': 'NOW-3MONTHS'}
offset = 0
results = []
max_total_exposed = 0
while True:
response = mp.search_payment(filters, limit=LIMIT, offset=offset)
filters["offset"] = offset
response = mp.payment().search(filters)
assert response['status'] == 200

quant_results = len(response['response']['results'])
paging = response['response']['paging']
max_total_exposed = max(max_total_exposed, paging["total"])
logger.debug(
'Getting response from mercadopago, paging %s', response['response']['paging'])
'Getting response from mercadopago, len=%d paging=%s', quant_results, paging)

results.extend(response['response']['results'])
if len(response['response']['results']) < LIMIT:
break
offset += LIMIT
if not response['response']['results']:
if paging["total"] == max_total_exposed:
# didn't get any result and reported "total" is the biggest seen so far
break
offset += quant_results
facundobatista marked this conversation as resolved.
Show resolved Hide resolved

logger.info('Got response from mercadopago, %d items', len(results))
return results
Loading