Skip to content

Commit 80cc46b

Browse files
authored
Merge pull request #408 from Adyen/user-agent-with-application-name
Support application_name in User-Agent
2 parents 68cfe33 + 431d894 commit 80cc46b

18 files changed

+138
-64
lines changed

Adyen/client.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def __init__(
7272
username=None,
7373
password=None,
7474
xapikey=None,
75+
application_name=None,
7576
review_payout_username=None,
7677
review_payout_password=None,
7778
store_payout_username=None, store_payout_password=None,
@@ -100,6 +101,7 @@ def __init__(
100101
self.username = username
101102
self.password = password
102103
self.xapikey = xapikey
104+
self.application_name = application_name
103105
self.review_payout_username = review_payout_username
104106
self.review_payout_password = review_payout_password
105107
self.store_payout_username = store_payout_username
@@ -349,7 +351,8 @@ def call_adyen_api(
349351

350352
headers = {
351353
self.APPLICATION_INFO_HEADER_NAME: settings.LIB_NAME,
352-
self.APPLICATION_VERSION_HEADER_NAME: settings.LIB_VERSION
354+
self.APPLICATION_VERSION_HEADER_NAME: settings.LIB_VERSION,
355+
'User-Agent': self.http_client.user_agent,
353356
}
354357

355358
# Adyen requires this header to be set and uses the combination of
@@ -388,8 +391,11 @@ def call_adyen_api(
388391
return adyen_result
389392

390393
def _init_http_client(self):
394+
user_agent_suffix = self.USER_AGENT_SUFFIX
395+
if self.application_name:
396+
user_agent_suffix = self.application_name + " " + user_agent_suffix
391397
self.http_client = HTTPClient(
392-
user_agent_suffix=self.USER_AGENT_SUFFIX,
398+
user_agent_suffix=user_agent_suffix,
393399
lib_version=self.LIB_VERSION,
394400
force_request=self.http_force,
395401
timeout=self.http_timeout,

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ adyen = Adyen.Adyen()
7373
# Configure the client
7474
adyen.client.xapikey = "YourXapikey"
7575
adyen.client.platform = "test" # change to "live" for production
76+
adyen.client.application_name = "MyTestApp" # if applicable, set application name (for tracking purposes)
77+
7678

7779
# Prepare the request
7880
request = {
@@ -109,6 +111,7 @@ from Adyen.services import AdyenCheckoutApi
109111
adyen_client = AdyenClient()
110112
adyen_client.xapikey = "YourXapikey"
111113
adyen_client.platform = "test"
114+
adyen_client.application_name = "MyTestApp" # if applicable, set application name (for tracking purposes)
112115

113116
# Instantiate the AdyenCheckoutApi service
114117
checkout_service = AdyenCheckoutApi(client=adyen_client)
@@ -125,6 +128,7 @@ Similarly you can instantiate a separate client for services that required diffe
125128
adyen_lem_client = AdyenClient()
126129
adyen_lem_client.xapikey = "YourLEMXapikey"
127130
adyen_lem_client.platform = "test"
131+
adyen_lem_client.application_name = "MyTestApp" # if applicable, set application name (for tracking purposes)
128132
~~~~
129133

130134
#### Force HTTP library

test/BalancePlatformTest.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_creating_balance_account(self):
3131
self.adyen.client.http_client.request.assert_called_once_with(
3232
'POST',
3333
f'{self.balance_platform_url}/balanceAccounts',
34-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
34+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
3535
json=request,
3636
xapikey="YourXapikey"
3737
)
@@ -49,7 +49,7 @@ def test_creating_account_holder(self):
4949
self.adyen.client.http_client.request.assert_called_once_with(
5050
'POST',
5151
f'{self.balance_platform_url}/accountHolders',
52-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
52+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
5353
json=request,
5454
xapikey="YourXapikey"
5555
)
@@ -63,7 +63,7 @@ def test_get_balance_platform(self):
6363
self.adyen.client.http_client.request.assert_called_once_with(
6464
'GET',
6565
f'{self.balance_platform_url}/balancePlatforms/{platform_id}',
66-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
66+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
6767
json=None,
6868
xapikey="YourXapikey"
6969
)
@@ -82,7 +82,7 @@ def test_creating_payment_instrument(self):
8282
self.adyen.client.http_client.request.assert_called_once_with(
8383
'POST',
8484
f'{self.balance_platform_url}/paymentInstruments',
85-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
85+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
8686
json=request,
8787
xapikey="YourXapikey"
8888
)
@@ -113,7 +113,7 @@ def test_creating_transfer_limit(self):
113113
self.adyen.client.http_client.request.assert_called_once_with(
114114
'POST',
115115
f'{self.balance_platform_url}/balancePlatforms/{balance_platform_id}/transferLimits',
116-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
116+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
117117
json=request,
118118
xapikey="YourXapikey"
119119
)
@@ -147,7 +147,7 @@ def test_creating_webhook(self):
147147
self.adyen.client.http_client.request.assert_called_once_with(
148148
'POST',
149149
f'{self.balance_platform_url}/balancePlatforms/{balance_platform_id}/webhooks/{webhook_id}/settings',
150-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
150+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
151151
json=request,
152152
xapikey="YourXapikey"
153153
)
@@ -164,7 +164,7 @@ def test_creating_payment_instrument_group(self):
164164
self.adyen.client.http_client.request.assert_called_once_with(
165165
'POST',
166166
f'{self.balance_platform_url}/paymentInstrumentGroups',
167-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
167+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
168168
json=request,
169169
xapikey="YourXapikey"
170170
)
@@ -179,7 +179,7 @@ def test_get_transaction_rule(self):
179179
'GET',
180180
f'{self.balance_platform_url}/'
181181
f'transactionRules/{transactionRuleId}',
182-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
182+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
183183
json=None,
184184
xapikey="YourXapikey"
185185
)
@@ -198,7 +198,7 @@ def test_update_network_token(self):
198198
self.adyen.client.http_client.request.assert_called_once_with(
199199
'PATCH',
200200
f'{self.balance_platform_url}/networkTokens/TK123ABC',
201-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
201+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
202202
json=request,
203203
xapikey="YourXapikey"
204204
)

test/BaseTest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ def create_client_from_file(self, status, request, filename=None):
2222
st = ""
2323
strjson = ""
2424

25-
self.ady.client.http_client = httpclient.HTTPClient
25+
# Ensure the actual http_client instance is initialized
26+
if not self.ady.client.http_init:
27+
self.ady.client._init_http_client()
28+
2629
self.ady.client.http_init = True
2730
self.ady.client.http_client.request = mock.MagicMock(
2831
return_value=[strjson, request, status, data])

test/BinLookupTest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_get_cost_estimate_success(self):
5555
'POST',
5656
'https://pal-test.adyen.com/pal/servlet/'
5757
f'BinLookup/{self.binLookup_version}/getCostEstimate',
58-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
58+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
5959
json={
6060
'merchantAccount': 'YourMerchantAccount',
6161
'amount': '1000',

test/CheckoutTest.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def test_payments_error_mocked(self):
9696
self.adyen.client.http_client.request.assert_called_once_with(
9797
'POST',
9898
f'{self.baseUrl}/payments',
99-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
99+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
100100
json={
101101
'returnUrl': 'https://your-company.com/...',
102102
'reference': '54431',
@@ -134,7 +134,7 @@ def test_payments_details_success_mocked(self):
134134
self.adyen.client.http_client.request.assert_called_once_with(
135135
'POST',
136136
f'{self.baseUrl}/payments/details',
137-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
137+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
138138
json={
139139
'paymentData': 'Hee57361f99....',
140140
'details': {'MD': 'sdfsdfsdf...', 'PaRes': 'sdkfhskdjfsdf...'}
@@ -323,7 +323,7 @@ def test_payments_capture_success_mocked(self):
323323
f'{self.baseUrl}/payments/{psp_reference}/captures',
324324
json=request,
325325
xapikey='YourXapikey',
326-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
326+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
327327
)
328328
self.assertEqual(psp_reference, result.message["paymentPspReference"])
329329
self.assertIsNotNone(result.message["pspReference"])
@@ -365,7 +365,7 @@ def test_orders_success(self):
365365
f'{self.baseUrl}/orders',
366366
json=request,
367367
xapikey='YourXapikey',
368-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
368+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
369369

370370
)
371371
self.assertEqual("8515930288670953", result.message['pspReference'])
@@ -387,7 +387,7 @@ def test_orders_cancel_success(self):
387387
f'{self.baseUrl}/orders/cancel',
388388
json=request,
389389
xapikey='YourXapikey',
390-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
390+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
391391
)
392392
self.assertEqual("8515931182066678", result.message['pspReference'])
393393
self.assertEqual("Received", result.message['resultCode'])
@@ -406,7 +406,7 @@ def test_paymentmethods_balance_success(self):
406406
f'{self.baseUrl}/paymentMethods/balance',
407407
json=request,
408408
xapikey='YourXapikey',
409-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
409+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
410410
)
411411
self.assertEqual("851611111111713K", result.message['pspReference'])
412412
self.assertEqual("Success", result.message['resultCode'])
@@ -442,7 +442,7 @@ def test_sessions_error(self):
442442
f'{self.baseUrl}/sessions',
443443
json={'merchantAccount': 'YourMerchantAccount'},
444444
xapikey='YourXapikey',
445-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
445+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
446446
)
447447
self.assertEqual(422, result.message['status'])
448448
self.assertEqual("130", result.message['errorCode'])
@@ -487,7 +487,7 @@ def test_payment_link(self):
487487
self.adyen.client.http_client.request.assert_called_once_with(
488488
'POST',
489489
f'{self.baseUrl}/paymentLinks',
490-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
490+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
491491
xapikey='YourXapikey',
492492
json=request
493493
)
@@ -504,7 +504,7 @@ def test_get_payment_link(self):
504504
self.adyen.client.http_client.request.assert_called_once_with(
505505
'GET',
506506
f'{self.baseUrl}/paymentLinks/{id}',
507-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
507+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
508508
xapikey="YourXapikey",
509509
json=None
510510
)
@@ -523,7 +523,7 @@ def test_update_payment_link(self):
523523
self.adyen.client.http_client.request.assert_called_once_with(
524524
'PATCH',
525525
f'{self.baseUrl}/paymentLinks/{id}',
526-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
526+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
527527
xapikey="YourXapikey",
528528
json=request
529529
)

test/DataProtectionTest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_data_erasure(self):
3131
'POST',
3232
f'{self.data_protection_url}'
3333
'/requestSubjectErasure',
34-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
34+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
3535
xapikey="YourXapikey",
3636
json=request
3737
)

test/DisputesTest.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_accept_dispute(self):
3030
self.adyen.client.http_client.request.assert_called_once_with(
3131
'POST',
3232
f'{self.disputes_url}/acceptDispute',
33-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
33+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
3434
json=request,
3535
xapikey="YourXapikey"
3636
)
@@ -49,7 +49,7 @@ def test_defend_dispute(self):
4949
self.adyen.client.http_client.request.assert_called_once_with(
5050
'POST',
5151
f'{self.disputes_url}/defendDispute',
52-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
52+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
5353
json=request,
5454
xapikey="YourXapikey"
5555
)
@@ -68,7 +68,7 @@ def test_delete_defense_dispute_document(self):
6868
self.adyen.client.http_client.request.assert_called_once_with(
6969
'POST',
7070
f'{self.disputes_url}/deleteDisputeDefenseDocument',
71-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
71+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
7272
json=request,
7373
xapikey="YourXapikey"
7474
)
@@ -86,7 +86,7 @@ def test_download_dispute_defense_document(self):
8686
self.adyen.client.http_client.request.assert_called_once_with(
8787
'POST',
8888
f'{self.disputes_url}/deleteDisputeDefenseDocument',
89-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
89+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
9090
json=request,
9191
xapikey="YourXapikey"
9292
)
@@ -104,7 +104,7 @@ def test_retrieve_applicable_defense_reasons(self):
104104
self.adyen.client.http_client.request.assert_called_once_with(
105105
'POST',
106106
f'{self.disputes_url}/retrieveApplicableDefenseReasons',
107-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
107+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
108108
json=request,
109109
xapikey="YourXapikey"
110110
)
@@ -129,7 +129,7 @@ def test_supply_defense_document(self):
129129
self.adyen.client.http_client.request.assert_called_once_with(
130130
'POST',
131131
f'{self.disputes_url}/supplyDefenseDocument',
132-
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION},
132+
headers={'adyen-library-name': 'adyen-python-api-library', 'adyen-library-version': settings.LIB_VERSION, 'User-Agent': 'adyen-python-api-library/' + settings.LIB_VERSION},
133133
json=request,
134134
xapikey="YourXapikey"
135135
)

0 commit comments

Comments
 (0)