bdp: use requests wrapper (#68469)

This commit is contained in:
Benjamin Dauvergne 2022-08-29 14:21:07 +02:00
parent a8530e2806
commit 2f9a76c592
2 changed files with 55 additions and 3 deletions

View File

@ -1,6 +1,5 @@
import json
import requests
from django.db import models
from django.utils.translation import ugettext_lazy as _
from requests.auth import HTTPBasicAuth
@ -40,13 +39,15 @@ class Bdp(BaseResource):
def get_api(self, endpoint, **params):
options = self.requests_options()
return requests.get(self.service_url + '/api/' + endpoint, params=params, **options).json()
return self.requests.get(self.service_url + '/api/' + endpoint, params=params, **options).json()
def post_api(self, endpoint, obj):
data = json.dumps(obj)
headers = {'Content-Type': 'application/json'}
options = self.requests_options()
request = requests.post(self.service_url + '/api/' + endpoint, data=data, headers=headers, **options)
request = self.requests.post(
self.service_url + '/api/' + endpoint, data=data, headers=headers, **options
)
result = {
'status_code': request.status_code,
'x_request_id': request.headers.get('x-request-id'),

51
tests/test_bdp.py Normal file
View File

@ -0,0 +1,51 @@
# Copyright (C) 2021 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest
from httmock import HTTMock, response, urlmatch
from . import utils
class TestBdp:
@pytest.fixture
def connector(self, db):
from passerelle.apps.bdp.models import Bdp
return utils.setup_access_rights(
Bdp.objects.create(
slug='slug',
service_url='https://bdp.example.com',
username='username',
password='password',
)
)
def test_detail_view(self, app, connector):
@urlmatch(netloc=r'^bdp.example.com$', path=r'^/api/coin')
def api_coin(url, request):
return response(
200,
[
{
'id': 'id',
'text': 'coin',
}
],
)
with HTTMock(api_coin):
resp = app.get('/bdp/slug/coin/')
assert resp.json['data']