|
1 | 1 | import json |
2 | | -try: |
3 | | - from urllib.parse import urlparse |
4 | | -except ImportError: |
5 | | - from urlparse import urlparse |
6 | | - |
7 | | -from mailchimp3 import MailChimp |
8 | | - |
9 | | -from .batch_operations import BatchOperations |
10 | | - |
11 | | - |
12 | | -class Batch: |
13 | | - def __init__(self, client): |
14 | | - self._mc_client = client |
15 | | - self.operations = [] |
16 | | - self._run = False |
17 | | - self.id = None |
18 | | - |
19 | | - def __getitem__(self, item): |
20 | | - if item == 'id': |
21 | | - return self.id |
22 | | - |
23 | | - def __repr__(self): |
24 | | - return '<{}.{}: {} operation{}{}>'.format( |
25 | | - self.__class__.__module__, |
26 | | - self.__class__.__name__, |
27 | | - len(self.operations), |
28 | | - 's' if len(self.operations) != 1 else '', |
29 | | - '*' if not self._run else '', |
30 | | - ) |
31 | | - |
32 | | - def _pause_batch(fn): |
33 | | - def wrapper(self, *args, **kwargs): |
34 | | - self._mc_client._is_paused = True |
35 | | - resp = fn(self, *args, **kwargs) |
36 | | - self._mc_client._is_paused = False |
37 | | - return resp |
38 | | - return wrapper |
39 | | - |
40 | | - @_pause_batch |
41 | | - def status(self, **kwargs): |
42 | | - return self._mc_client.batch_operations.get(self, **kwargs) |
43 | | - |
44 | | - @_pause_batch |
45 | | - def run(self): |
46 | | - self._mc_client.batch_operations.create(self) |
47 | | - self._mc_client.batch = None |
48 | | - return self |
49 | | - |
50 | | - @_pause_batch |
51 | | - def delete(self): |
52 | | - return self._mc_client.batch_operations.delete(self) |
| 2 | +import uuid |
| 3 | +from six.moves.urllib.parse import urlparse |
| 4 | +from mailchimp_marketing.api.surveys_api import SurveysApi |
| 5 | +from mailchimp_marketing.api.account_export_api import AccountExportApi |
| 6 | +from mailchimp_marketing.api.account_exports_api import AccountExportsApi |
| 7 | +from mailchimp_marketing.api.activity_feed_api import ActivityFeedApi |
| 8 | +from mailchimp_marketing.api.authorized_apps_api import AuthorizedAppsApi |
| 9 | +from mailchimp_marketing.api.automations_api import AutomationsApi |
| 10 | +from mailchimp_marketing.api.batch_webhooks_api import BatchWebhooksApi |
| 11 | +from mailchimp_marketing.api.campaign_folders_api import CampaignFoldersApi |
| 12 | +from mailchimp_marketing.api.campaigns_api import CampaignsApi |
| 13 | +from mailchimp_marketing.api.connected_sites_api import ConnectedSitesApi |
| 14 | +from mailchimp_marketing.api.conversations_api import ConversationsApi |
| 15 | +from mailchimp_marketing.api.customer_journeys_api import CustomerJourneysApi |
| 16 | +from mailchimp_marketing.api.ecommerce_api import EcommerceApi |
| 17 | +from mailchimp_marketing.api.facebook_ads_api import FacebookAdsApi |
| 18 | +from mailchimp_marketing.api.file_manager_api import FileManagerApi |
| 19 | +from mailchimp_marketing.api.landing_pages_api import LandingPagesApi |
| 20 | +from mailchimp_marketing.api.lists_api import ListsApi |
| 21 | +from mailchimp_marketing.api.ping_api import PingApi |
| 22 | +from mailchimp_marketing.api.reporting_api import ReportingApi |
| 23 | +from mailchimp_marketing.api.reports_api import ReportsApi |
| 24 | +from mailchimp_marketing.api.root_api import RootApi |
| 25 | +from mailchimp_marketing.api.search_campaigns_api import SearchCampaignsApi |
| 26 | +from mailchimp_marketing.api.search_members_api import SearchMembersApi |
| 27 | +from mailchimp_marketing.api.template_folders_api import TemplateFoldersApi |
| 28 | +from mailchimp_marketing.api.templates_api import TemplatesApi |
| 29 | +from mailchimp_marketing.api.verified_domains_api import VerifiedDomainsApi |
| 30 | +from mailchimp_marketing import api_client |
| 31 | +from mailchimp_marketing import Client as MailChimpClient |
| 32 | + |
| 33 | +from .batches_api import BatchesApi |
53 | 34 |
|
54 | 35 |
|
55 | 36 | class FakeRequest: |
56 | | - def __init__(self, batch, **kwargs): |
57 | | - self.status_code = 200 |
58 | | - self.batch = batch |
59 | | - path = urlparse(kwargs.get('url')).path[4:] |
60 | | - operation = { |
61 | | - 'method': kwargs.get('method'), |
62 | | - 'path': path, |
| 37 | + def __init__(self, operation_id): |
| 38 | + self.ok = True |
| 39 | + self.headers = { |
| 40 | + "content-type": "application/json" |
63 | 41 | } |
64 | | - if 'json' in kwargs: |
65 | | - operation['body'] = json.dumps(kwargs['json']) |
66 | | - self.batch.operations.append(operation) |
| 42 | + self._operation_id = operation_id |
67 | 43 |
|
68 | 44 | def json(self): |
69 | | - return self.batch |
| 45 | + return { |
| 46 | + "okay": True, |
| 47 | + "operation_id": self._operation_id, |
| 48 | + } |
70 | 49 |
|
71 | 50 |
|
72 | | -class BatchMailChimp(MailChimp): |
73 | | - def __init__(self, *args, batch=False, **kwargs): |
74 | | - super(BatchMailChimp, self).__init__(*args, **kwargs) |
75 | | - self._is_batch = batch |
76 | | - self._is_paused = False |
77 | | - self.batch = None |
78 | | - # Batch Operations |
79 | | - self.batches = self.batch_operations = BatchOperations(self) |
| 51 | +class ApiClient(api_client.ApiClient): |
| 52 | + def __init__(self, config={}): |
| 53 | + self.operations = [] |
| 54 | + self.set_batch_mode(config.get("batch", False)) |
| 55 | + super().__init__(config) |
| 56 | + |
| 57 | + def set_batch_mode(self, state): |
| 58 | + self.batch_mode = state |
| 59 | + |
| 60 | + def set_config(self, config={}): |
| 61 | + if "batch" in config: |
| 62 | + if self.operations and not config["batch"]: |
| 63 | + raise Exception("Can’t disable batch mode") |
| 64 | + self.batch_mode = config["batch"] |
| 65 | + super().set_config(config) |
| 66 | + |
| 67 | + def request(self, method, url, query_params=None, headers=None, body=None): |
| 68 | + if not self.batch_mode: |
| 69 | + return super().request(method, url, query_params, headers, body) |
| 70 | + operation = { |
| 71 | + "method": method, |
| 72 | + "path": urlparse(url).path[4:], |
| 73 | + "operation_id": uuid.uuid4().hex, |
| 74 | + } |
| 75 | + if query_params: |
| 76 | + operation["params"] = query_params |
| 77 | + if body: |
| 78 | + operation["body"] = json.dumps(body) |
| 79 | + |
| 80 | + self.operations.append(operation) |
| 81 | + return FakeRequest(operation["operation_id"]) |
| 82 | + |
| 83 | + |
| 84 | +class Client(MailChimpClient): |
| 85 | + def __init__(self, config={}): |
| 86 | + self.api_client = ApiClient(config) |
| 87 | + |
| 88 | + self.Surveys = SurveysApi(self.api_client) |
| 89 | + self.accountExport = AccountExportApi(self.api_client) |
| 90 | + self.accountExports = AccountExportsApi(self.api_client) |
| 91 | + self.activityFeed = ActivityFeedApi(self.api_client) |
| 92 | + self.authorizedApps = AuthorizedAppsApi(self.api_client) |
| 93 | + self.automations = AutomationsApi(self.api_client) |
| 94 | + self.batchWebhooks = BatchWebhooksApi(self.api_client) |
| 95 | + self.batches = BatchesApi(self.api_client) |
| 96 | + self.campaignFolders = CampaignFoldersApi(self.api_client) |
| 97 | + self.campaigns = CampaignsApi(self.api_client) |
| 98 | + self.connectedSites = ConnectedSitesApi(self.api_client) |
| 99 | + self.conversations = ConversationsApi(self.api_client) |
| 100 | + self.customerJourneys = CustomerJourneysApi(self.api_client) |
| 101 | + self.ecommerce = EcommerceApi(self.api_client) |
| 102 | + self.facebookAds = FacebookAdsApi(self.api_client) |
| 103 | + self.fileManager = FileManagerApi(self.api_client) |
| 104 | + self.landingPages = LandingPagesApi(self.api_client) |
| 105 | + self.lists = ListsApi(self.api_client) |
| 106 | + self.ping = PingApi(self.api_client) |
| 107 | + self.reporting = ReportingApi(self.api_client) |
| 108 | + self.reports = ReportsApi(self.api_client) |
| 109 | + self.root = RootApi(self.api_client) |
| 110 | + self.searchCampaigns = SearchCampaignsApi(self.api_client) |
| 111 | + self.searchMembers = SearchMembersApi(self.api_client) |
| 112 | + self.templateFolders = TemplateFoldersApi(self.api_client) |
| 113 | + self.templates = TemplatesApi(self.api_client) |
| 114 | + self.verifiedDomains = VerifiedDomainsApi(self.api_client) |
| 115 | + self.batches = BatchesApi(self.api_client) |
80 | 116 |
|
81 | | - def _make_request(self, **kwargs): |
82 | | - if self._is_batch and not self._is_paused: |
83 | | - if not self.batch: |
84 | | - self.batch = Batch(self) |
85 | | - return FakeRequest(self.batch, **kwargs) |
86 | | - return super(BatchMailChimp, self)._make_request(**kwargs) |
| 117 | + def __repr__(self): |
| 118 | + return "<{module}.{name}: batch mode {batch_mode_toggle}>".format( |
| 119 | + module=self.__class__.__module__, |
| 120 | + name=self.__class__.__name__, |
| 121 | + batch_mode_toggle="ON" if self.api_client.batch_mode else "OFF", |
| 122 | + ) |
0 commit comments