python-amazon-ad-api 2.1.12__tar.gz → 2.1.13__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. {python_amazon_ad_api-2.1.12/python_amazon_ad_api.egg-info → python_amazon_ad_api-2.1.13}/PKG-INFO +1 -1
  2. python_amazon_ad_api-2.1.13/ad_api/auth/credentials.py +10 -0
  3. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/base/client.py +100 -4
  4. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/base/credential_provider.py +10 -1
  5. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/base/exceptions.py +71 -0
  6. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/base/helpers.py +20 -1
  7. python_amazon_ad_api-2.1.13/ad_api/version.py +1 -0
  8. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13/python_amazon_ad_api.egg-info}/PKG-INFO +1 -1
  9. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/setup.cfg +1 -1
  10. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/setup.py +1 -1
  11. python_amazon_ad_api-2.1.12/ad_api/auth/credentials.py +0 -6
  12. python_amazon_ad_api-2.1.12/ad_api/version.py +0 -1
  13. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/LICENSE +0 -0
  14. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/README.md +0 -0
  15. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/__init__.py +0 -0
  16. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/__init__.py +0 -0
  17. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/account.py +0 -0
  18. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/account_budgets.py +0 -0
  19. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/advertising_invitations.py +0 -0
  20. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/advertising_test_account.py +0 -0
  21. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/advertising_user_permissions.py +0 -0
  22. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/attribution.py +0 -0
  23. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/audiences.py +0 -0
  24. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/billing.py +0 -0
  25. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/brand_benchmarks.py +0 -0
  26. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/brand_metrics.py +0 -0
  27. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/creative_assets.py +0 -0
  28. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/dsp_advertiser.py +0 -0
  29. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/eligibility.py +0 -0
  30. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/exports.py +0 -0
  31. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/history.py +0 -0
  32. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/insights.py +0 -0
  33. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/localization.py +0 -0
  34. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/manager_accounts.py +0 -0
  35. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/marketing_mix_modeling.py +0 -0
  36. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/metadata.py +0 -0
  37. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/portfolios.py +0 -0
  38. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/portfolios_v3.py +0 -0
  39. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/profiles.py +0 -0
  40. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/recommendations.py +0 -0
  41. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/reports.py +0 -0
  42. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/__init__.py +0 -0
  43. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/ad_creatives_v4.py +0 -0
  44. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/ad_groups.py +0 -0
  45. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/ad_groups_v4.py +0 -0
  46. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/ads_v4.py +0 -0
  47. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/benchmarks.py +0 -0
  48. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/bid_recommendations.py +0 -0
  49. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/brands.py +0 -0
  50. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/budget_rules.py +0 -0
  51. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/campaigns.py +0 -0
  52. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/campaigns_v4.py +0 -0
  53. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/forecast.py +0 -0
  54. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/keywords.py +0 -0
  55. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/keywords_recommendations.py +0 -0
  56. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/landing_page_asins.py +0 -0
  57. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/media.py +0 -0
  58. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/migration.py +0 -0
  59. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/negative_keywords.py +0 -0
  60. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/negative_product_targeting.py +0 -0
  61. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/optimization_rules.py +0 -0
  62. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/product_targeting.py +0 -0
  63. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/recommendations.py +0 -0
  64. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/reports.py +0 -0
  65. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/stores.py +0 -0
  66. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/targeting_categories.py +0 -0
  67. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/targeting_recommendations.py +0 -0
  68. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sb/themes.py +0 -0
  69. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sd/__init__.py +0 -0
  70. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sd/ad_groups.py +0 -0
  71. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sd/bid_recommendations.py +0 -0
  72. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sd/brand_safety.py +0 -0
  73. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sd/budget_rules.py +0 -0
  74. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sd/campaigns.py +0 -0
  75. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sd/campaigns_budget_usage.py +0 -0
  76. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sd/creatives.py +0 -0
  77. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sd/forecast.py +0 -0
  78. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sd/negative_product_targeting.py +0 -0
  79. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sd/product_ads.py +0 -0
  80. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sd/product_targeting.py +0 -0
  81. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sd/recommendations.py +0 -0
  82. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sd/reports.py +0 -0
  83. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sd/snapshots.py +0 -0
  84. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sd/targeting_recommendations.py +0 -0
  85. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/__init__.py +0 -0
  86. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/ad_groups_v3.py +0 -0
  87. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/bid_recommendations_v3.py +0 -0
  88. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/budget_initial_recommendation.py +0 -0
  89. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/budget_recommendations.py +0 -0
  90. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/budget_rules.py +0 -0
  91. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/budget_rules_recommendations.py +0 -0
  92. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/campaign_consolidated_recommendations.py +0 -0
  93. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/campaign_negative_keywords_v3.py +0 -0
  94. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/campaign_negative_targets.py +0 -0
  95. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/campaigns_budget_usage.py +0 -0
  96. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/campaigns_v3.py +0 -0
  97. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/campaings_optimization.py +0 -0
  98. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/global_recommendations.py +0 -0
  99. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/keyword_groups_recommendations.py +0 -0
  100. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/keywords_v3.py +0 -0
  101. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/negative_keywords_v3.py +0 -0
  102. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/negative_product_targeting_v3.py +0 -0
  103. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/product_ads_v3.py +0 -0
  104. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/product_recommendations.py +0 -0
  105. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/product_targeting_v3.py +0 -0
  106. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/ranked_keywords_recommendations.py +0 -0
  107. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/schedule_based_bid_optimization.py +0 -0
  108. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/sp/target_promotion_groups.py +0 -0
  109. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/stores.py +0 -0
  110. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/stream.py +0 -0
  111. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/__init__.py +0 -0
  112. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/ad_associations.py +0 -0
  113. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/ad_extensions.py +0 -0
  114. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/ad_groups.py +0 -0
  115. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/ads.py +0 -0
  116. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/advertiser_accounts.py +0 -0
  117. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/brand_store_edition_publish_versions.py +0 -0
  118. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/brand_store_editions.py +0 -0
  119. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/brand_store_pages.py +0 -0
  120. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/brand_stores.py +0 -0
  121. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/campaigns.py +0 -0
  122. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/geo_locations.py +0 -0
  123. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/label_object_associations.py +0 -0
  124. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/labels.py +0 -0
  125. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/location_indexes.py +0 -0
  126. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/manager_accounts.py +0 -0
  127. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/portfolios.py +0 -0
  128. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/recommendation_types.py +0 -0
  129. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/recommendations.py +0 -0
  130. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/reports.py +0 -0
  131. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/selling_accounts.py +0 -0
  132. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/v1/targets.py +0 -0
  133. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/api/validation_configurations.py +0 -0
  134. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/auth/__init__.py +0 -0
  135. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/auth/access_token_client.py +0 -0
  136. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/auth/access_token_response.py +0 -0
  137. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/auth/exceptions.py +0 -0
  138. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/base/__init__.py +0 -0
  139. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/base/api_response.py +0 -0
  140. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/base/config.py +0 -0
  141. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/base/marketplaces.py +0 -0
  142. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/base/utils.py +0 -0
  143. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/shared/__init__.py +0 -0
  144. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/ad_api/shared/base_client.py +0 -0
  145. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/pyproject.toml +0 -0
  146. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/python_amazon_ad_api.egg-info/SOURCES.txt +0 -0
  147. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/python_amazon_ad_api.egg-info/dependency_links.txt +0 -0
  148. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/python_amazon_ad_api.egg-info/requires.txt +0 -0
  149. {python_amazon_ad_api-2.1.12 → python_amazon_ad_api-2.1.13}/python_amazon_ad_api.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-amazon-ad-api
3
- Version: 2.1.12
3
+ Version: 2.1.13
4
4
  Summary: Python wrapper for the Amazon Advertising API
5
5
  Home-page: https://github.com/denisneuf/python-amazon-ad-api
6
6
  Author: Daniel Alvaro
@@ -0,0 +1,10 @@
1
+ class Credentials:
2
+ def __init__(self, credentials):
3
+ self.client_id = credentials['client_id']
4
+ self.client_secret = credentials['client_secret']
5
+ self.refresh_token = credentials['refresh_token']
6
+ # Identifiers: not read here, AccessTokenClient only needs the three
7
+ # above. They stay so the class shows every key credentials.yml takes.
8
+ self.profile_id = credentials.get('profile_id')
9
+ self.account_id = credentials.get('account_id')
10
+ self.manager_account_id = credentials.get('manager_account_id')
@@ -1,12 +1,12 @@
1
1
  import json
2
2
  from json import JSONDecodeError
3
3
  import logging
4
- from cachetools import TTLCache
5
4
  from requests import request
6
5
  from ad_api.auth import AccessTokenClient, AccessTokenResponse
7
6
  from .api_response import ApiResponse
8
7
  from ad_api.shared.base_client import BaseClient
9
- from .exceptions import get_exception_for_code, get_exception_for_content
8
+ from .exceptions import (get_exception_for_code, get_exception_for_content,
9
+ MissingEndpointHeader, AmbiguousEndpointHeader)
10
10
  from .marketplaces import Marketplaces
11
11
  import os
12
12
  import requests
@@ -20,7 +20,23 @@ import ad_api.version as vd
20
20
  from typing import Any, Dict, Optional
21
21
 
22
22
  log = logging.getLogger(__name__)
23
- role_cache = TTLCache(maxsize=int(os.environ.get('AD_API_AUTH_CACHE_SIZE', 10)), ttl=3200)
23
+ # The three headers that name which account a call is about. A route may only
24
+ # send one of them; which one it sends is decided per route and, when a route
25
+ # accepts more than one, per call.
26
+ _IDENTITY_HEADERS = (
27
+ 'Amazon-Advertising-API-Scope',
28
+ 'Amazon-Ads-AccountId',
29
+ 'Amazon-Advertising-API-Manager-Account',
30
+ )
31
+
32
+ # selector= value -> header it sends. The keys are the same keys credentials.yml
33
+ # takes, so a caller writes the name it already knows.
34
+ _SELECTOR_HEADERS = {
35
+ 'profile_id': 'Amazon-Advertising-API-Scope',
36
+ 'account_id': 'Amazon-Ads-AccountId',
37
+ 'manager_account_id': 'Amazon-Advertising-API-Manager-Account',
38
+ }
39
+
24
40
  _DEFAULT_MARKETPLACE = Marketplaces[os.environ['AD_API_DEFAULT_MARKETPLACE']] if 'AD_API_DEFAULT_MARKETPLACE' in os.environ else Marketplaces.EU
25
41
 
26
42
 
@@ -37,7 +53,10 @@ class Client(BaseClient):
37
53
  access_token: Optional[str] = None,
38
54
  verify_additional_credentials: bool = True,
39
55
  ):
40
- self.credentials = CredentialProvider(account, credentials, verify_additional_credentials).credentials
56
+ _provider = CredentialProvider(account, credentials, verify_additional_credentials)
57
+ self.credentials = _provider.credentials
58
+ self.account_name = getattr(_provider, 'account', account)
59
+ self.credentials_source = getattr(_provider, 'source', 'yml')
41
60
  self._auth = AccessTokenClient(
42
61
  credentials=self.credentials,
43
62
  proxies=proxies,
@@ -64,6 +83,12 @@ class Client(BaseClient):
64
83
  }
65
84
  if profile_id := self.credentials.get('profile_id'):
66
85
  data['Amazon-Advertising-API-Scope'] = profile_id
86
+
87
+ if account_id := self.credentials.get('account_id'):
88
+ data['Amazon-Ads-AccountId'] = account_id
89
+
90
+ if manager_account_id := self.credentials.get('manager_account_id'):
91
+ data['Amazon-Advertising-API-Manager-Account'] = manager_account_id
67
92
  return data
68
93
 
69
94
  @property
@@ -221,6 +246,8 @@ class Client(BaseClient):
221
246
  params = {}
222
247
 
223
248
  method = params.pop('method')
249
+ endpoint_headers = params.pop('endpoint_headers', None)
250
+ selector = params.pop('selector', None)
224
251
 
225
252
  if headers is False:
226
253
  base_header = self.headers.copy()
@@ -232,6 +259,75 @@ class Client(BaseClient):
232
259
  base_header.update(headers)
233
260
  headers = base_header
234
261
 
262
+ elif endpoint_headers is not None:
263
+ # headers is None: self.headers is used at request time. Copy it so
264
+ # the filter below has something to mutate.
265
+ headers = self.headers.copy()
266
+
267
+ # A method that passed its own headers often carries only Accept and
268
+ # Content-Type. Rebuild the layer from the base so the filter sees what
269
+ # the property would have produced. With headers=None the old helper
270
+ # deleted the identity header earlier in _request itself, which is why
271
+ # this has to happen before the branches above.
272
+ if endpoint_headers is not None:
273
+ headers = dict(headers or {})
274
+ for h in _IDENTITY_HEADERS:
275
+ headers.pop(h, None)
276
+ for h, v in self.headers.items():
277
+ if h in _IDENTITY_HEADERS:
278
+ headers[h] = v
279
+
280
+ # Build the identity picture from self.headers, independently of the
281
+ # per-method headers dict (which often carries only Accept/Content-Type).
282
+ identity = {h: v for h, v in self.headers.items() if h in _IDENTITY_HEADERS}
283
+
284
+ # A route that declares headers sends only those, and fails loudly when
285
+ # none of them can be supplied: an identity header left out silently
286
+ # turns into a 401 or 400 from the service that names nothing useful.
287
+ if endpoint_headers is not None:
288
+ if endpoint_headers:
289
+ present = [h for h in endpoint_headers if h in identity]
290
+ if not present:
291
+ raise MissingEndpointHeader(
292
+ path, method, endpoint_headers,
293
+ getattr(self, 'account_name', 'default'),
294
+ list(identity.keys()),
295
+ getattr(self, 'credentials_source', 'yml'),
296
+ )
297
+ if selector is not None:
298
+ chosen = _SELECTOR_HEADERS.get(selector)
299
+ if chosen is None:
300
+ raise ValueError(
301
+ f"unknown selector {selector!r}; valid values are "
302
+ + ', '.join(repr(k) for k in _SELECTOR_HEADERS)
303
+ )
304
+ if chosen not in present:
305
+ raise MissingEndpointHeader(
306
+ path, method, [chosen],
307
+ getattr(self, 'account_name', 'default'),
308
+ list(identity.keys()),
309
+ getattr(self, 'credentials_source', 'yml'),
310
+ )
311
+ keep = {chosen}
312
+ elif len(present) > 1:
313
+ raise AmbiguousEndpointHeader(
314
+ path, method, present,
315
+ getattr(self, 'account_name', 'default'),
316
+ getattr(self, 'credentials_source', 'yml'),
317
+ )
318
+ else:
319
+ keep = set(present)
320
+ else:
321
+ keep = set()
322
+ # Apply the selection both to the base headers and to the dict
323
+ # the method supplied: the identity header must not survive in
324
+ # either place if this route did not declare it.
325
+ headers = headers or {}
326
+ for h in _IDENTITY_HEADERS:
327
+ headers.pop(h, None)
328
+ if h in keep and h in identity:
329
+ headers[h] = identity[h]
330
+
235
331
  request_data = data if method in ('POST', 'PUT', 'PATCH') else None
236
332
  res = request(
237
333
  method,
@@ -12,8 +12,11 @@ REQUIRED_CREDENTIALS = [
12
12
  'client_id',
13
13
  'client_secret',
14
14
  ]
15
+ # Nothing beyond the OAuth trio is required. profile_id, account_id and
16
+ # manager_account_id are destination identifiers, not credentials: a route
17
+ # decides which one it needs, and demanding them here rejects any account
18
+ # that holds only one of the three.
15
19
  ADDITIONAL_CREDENTIALS = [
16
- 'profile_id'
17
20
  ]
18
21
 
19
22
 
@@ -113,6 +116,12 @@ class CredentialProvider:
113
116
  credentials: Optional[Dict[str, str]] = None,
114
117
  verify_additional_credentials: bool = True,
115
118
  ):
119
+ # Where the credentials came from: code dict > env vars > credentials.yml.
120
+ self.account = account
121
+ self.source = ('code' if isinstance(credentials, dict)
122
+ else 'env' if self._get_env('AD_API_CLIENT_ID')
123
+ else 'yml')
124
+
116
125
  client_id_env = self._get_env('AD_API_CLIENT_ID')
117
126
  client_secret_env = self._get_env('AD_API_CLIENT_SECRET')
118
127
  refresh_token_env = self._get_env('AD_API_REFRESH_TOKEN')
@@ -1,3 +1,74 @@
1
+ class AmbiguousEndpointHeader(Exception):
2
+ """The route accepts several identity headers and the call did not pick one.
3
+
4
+ Only raised when more than one of the route's headers is actually
5
+ available; a route whose single declared header is present never gets
6
+ here. The message lists the candidates and the exact selector= value for
7
+ each, because the service decides what an answer covers from the header:
8
+ the scope answers for one marketplace and the account for every one of
9
+ them, so picking silently would return a different answer than the caller
10
+ asked for.
11
+ """
12
+
13
+ SELECTORS = {
14
+ 'Amazon-Advertising-API-Scope': 'profile_id',
15
+ 'Amazon-Ads-AccountId': 'account_id',
16
+ 'Amazon-Advertising-API-Manager-Account': 'manager_account_id',
17
+ }
18
+
19
+ def __init__(self, path, method, candidates, account, source='yml'):
20
+ self.path = path
21
+ self.method = method
22
+ self.candidates = candidates
23
+ self.account = account
24
+ self.source = source
25
+ options = ', '.join("selector='" + self.SELECTORS.get(h, h) + "'" for h in candidates)
26
+ super().__init__(
27
+ f"{method} {path} can use any of {candidates} and "
28
+ f"they are all available for account '{account}'. Pick one: {options}"
29
+ )
30
+
31
+
32
+ class MissingEndpointHeader(Exception):
33
+ """The route needs an identity header this account cannot supply.
34
+
35
+ Raised before the request leaves. The message names the route, the header
36
+ the route accepts, the credentials.yml key that would satisfy it, which
37
+ of the three the account does hold, and where the credentials came from
38
+ (a dict in code, the AD_API_* variables or the config file). Without it
39
+ the call went out with no identity and the service answered with a 401 or
40
+ a 400 that named none of that.
41
+ """
42
+
43
+ KEYS = {
44
+ 'Amazon-Advertising-API-Scope': 'profile_id',
45
+ 'Amazon-Ads-AccountId': 'account_id',
46
+ 'Amazon-Advertising-API-Manager-Account': 'manager_account_id',
47
+ }
48
+
49
+ def __init__(self, path, method, required, account, available, source='yml'):
50
+ self.path = path
51
+ self.method = method
52
+ self.required = required
53
+ self.account = account
54
+ self.available = available
55
+ self.source = source
56
+ self.missing = [self.KEYS.get(h, h) for h in required]
57
+ held = ', '.join(self.KEYS.get(h, h) for h in available) or 'none'
58
+ if source == 'code':
59
+ where = 'the credentials dict passed in code'
60
+ elif source == 'env':
61
+ where = 'the AD_API_* environment variables'
62
+ else:
63
+ where = f"credentials.yml (account '{account}')"
64
+ message = (
65
+ f"{method} {path} needs one of {required}; "
66
+ f"{', '.join(self.missing)} is missing from {where}. "
67
+ f"It does hold: {held}"
68
+ )
69
+ super().__init__(message)
70
+
71
+
1
72
  class AdvertisingApiException(Exception):
2
73
  def __init__(self, code, error, headers):
3
74
  try:
@@ -16,11 +16,30 @@ def fill_query_params(query, *args):
16
16
  T = TypeVar("T")
17
17
 
18
18
 
19
- def sp_endpoint(path, method="GET"):
19
+ def sp_endpoint(path, method="GET", headers=None):
20
+ """Bind a method to its route.
21
+
22
+ headers is the set of identity headers this route may send:
23
+ 'Amazon-Advertising-API-Scope', 'Amazon-Ads-AccountId',
24
+ 'Amazon-Advertising-API-Manager-Account', or [] for a route that takes
25
+ none of them. Left as None the route keeps the earlier behaviour and
26
+ sends whatever the credentials carry.
27
+
28
+ A route that lists one header sends that one. A route that lists several
29
+ lets the caller choose with selector=, whose values are the keys
30
+ credentials.yml takes ('profile_id', 'account_id', 'manager_account_id'),
31
+ and raises AmbiguousEndpointHeader when the call names none of them. A
32
+ route whose listed headers cannot be supplied raises MissingEndpointHeader
33
+ instead of sending a request the service will reject with a message naming
34
+ nothing useful.
35
+ """
36
+
20
37
  def decorator(function: T) -> T:
21
38
  @functools.wraps(function)
22
39
  def wrapper(*args, **kwargs):
23
40
  kwargs.update({"path": path, "method": method})
41
+ if headers is not None:
42
+ kwargs["endpoint_headers"] = list(headers)
24
43
  return function(*args, **kwargs)
25
44
 
26
45
  wrapper.__doc__ = function.__doc__
@@ -0,0 +1 @@
1
+ __version__ = "2.1.13"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-amazon-ad-api
3
- Version: 2.1.12
3
+ Version: 2.1.13
4
4
  Summary: Python wrapper for the Amazon Advertising API
5
5
  Home-page: https://github.com/denisneuf/python-amazon-ad-api
6
6
  Author: Daniel Alvaro
@@ -1,6 +1,6 @@
1
1
  [metadata]
2
2
  name = python-amazon-ad-api
3
- version = 2.1.12
3
+ version = 2.1.13
4
4
  author = Daniel Alvaro
5
5
  author_email = denisneuf@hotmail.com
6
6
  description = Python wrapper for the Amazon Advertising API
@@ -2,7 +2,7 @@ from setuptools import setup
2
2
 
3
3
  setup(
4
4
  name='python-amazon-ad-api',
5
- version='2.1.12',
5
+ version='2.1.13',
6
6
  install_requires=[
7
7
  "requests>=2.27.1,<2.35.0",
8
8
  "six>=1.16,<1.18",
@@ -1,6 +0,0 @@
1
- class Credentials:
2
- def __init__(self, credentials):
3
- self.client_id = credentials['client_id']
4
- self.client_secret = credentials['client_secret']
5
- self.refresh_token = credentials['refresh_token']
6
- self.profile_id = credentials.get('profile_id')
@@ -1 +0,0 @@
1
- __version__ = "2.1.12"