Skip to content

Commit 0854c18

Browse files
authored
Release: Version v0.1.2 (#20)
- Lazy load PinterestSDKClient - Add config.json support - Add AdGroup methods: enable_auto_targeting, disable_auto_targeting
1 parent e9a0242 commit 0854c18

17 files changed

Lines changed: 324 additions & 79 deletions

Makefile

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
1-
build:
1+
clean: clean-build clean-pyc ## Clean
2+
3+
clean-build: ## Clean python build
4+
rm -fr build/
5+
rm -fr dist/
6+
rm -fr *.egg-info
7+
rm -fr .tox
8+
9+
clean-pyc: ## Clean python binaries
10+
find . -name '*.pyc' -exec rm -f {} +
11+
find . -name '*.pyo' -exec rm -f {} +
12+
find . -name '*~' -exec rm -f {} +
13+
14+
build: ## Build command
215
python -m build
316
ls -l dist
417

518
pip_release_install:
619
pip install twine build
720

8-
publish_pypi_test: pip_release_install build
21+
publish_pypi_test: clean pip_release_install build
922
twine upload -r testpypi dist/*
1023

11-
publish_pypi: pip_release_install build
24+
publish_pypi: clean pip_release_install build
1225
twine upload -r pypi dist/*

README.md

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,56 @@ You can now use the SDK.
5555

5656
## Getting Started
5757

58+
For use the client you need set basic variables for that you have two option setup environment variables (using a
59+
.env file or set in your OS) or create a config.json.
60+
5861
### Setting up environment variables
5962

60-
To configure the client using environment variables, you must create a **.env** file using [.env.example](https://github.com/pinterest/pinterest-python-sdk/blob/main/.env.example)
63+
To configure the client using environment variables, you must create a **.env** file using [.env.example](./pinterest/.env.example)
6164
as a template. For basic configuration and usage you need to set the following environment variables in the **.env** file:
6265

63-
```
64-
PINTEREST_ACCESS_TOKEN='<access token>'
65-
```
66-
_or_
6766
```
6867
PINTEREST_APP_ID=<app id>
6968
PINTEREST_APP_SECRET=<app secret>
69+
7070
PINTEREST_REFRESH_ACCESS_TOKEN='<refresh token>'
71+
**or**
72+
PINTEREST_ACCESS_TOKEN='<access token>'
7173
```
7274

73-
Once you have established the environment variables, the client will be instantiated for you automatically.
75+
Once you have established the environment variables, the client will be instantiated for you automatically.
7476

7577
**NOTE**:
7678
* Setting the `PINTEREST_ACCESS_TOKEN` (which is valid for thirty days) will require the token value to be replaced when it expires. You will need to manually reinsantiate the client when the **access_token** expires.
7779
* Setting the `PINTEREST_REFRESH_ACCESS_TOKEN` (which is valid for a year) will allow the SDK to regenerate the new access token whenever it is required.
7880

81+
### Setting up config.json
82+
83+
To configure the client using config.json, you must create a **config.json** file using [config.json.example](./pinterest/config.json.example)
84+
as a template. For basic configuration and usage you need to set the following key in the **config.json** file:
85+
86+
```json
87+
{
88+
"app_id": "<app id>",
89+
"app_secret": "<app secret>",
90+
"refresh_access_token": "<refresh token>"
91+
}
92+
```
93+
94+
**or**
95+
96+
```json
97+
{
98+
"access_token": "<access token>"
99+
}
100+
```
101+
102+
Once you have established the keys, the client will be instantiated for you automatically.
103+
104+
**NOTE**:
105+
* Setting the `access_token` (which is valid for thirty days) will require the token value to be replaced when it expires. You will need to manually reinsantiate the client when the **access_token** expires.
106+
* Setting the `refresh_access_token` (which is valid for a year) will allow the SDK to regenerate the new access token whenever it is required.
107+
79108
For more information visit the [Authentication](https://developers.pinterest.com/docs/getting-started/authentication/#Refreshing%20an%20access%20token) page.
80109

81110
## Samples

config.json.example

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"debug": "true",
3+
"port": "0",
4+
"app_id": "<app id>",
5+
"app_secret": "<app secret>",
6+
"redirect_uri": "localhost",
7+
"response_type": "code",
8+
"scope": "<scopes>",
9+
"state": "dev",
10+
"access_token_json_path": "./",
11+
"access_token": "<access token>",
12+
"refresh_access_token": "<refresh token>",
13+
"api_uri": "https://api.pinterest.com/v5"
14+
}

pinterest/ads/ad_accounts.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from pinterest.generated.client.model.ad_account import AdAccount as GeneratedAdAccount
1010
from pinterest.generated.client.model.ad_account_create_request import AdAccountCreateRequest
1111

12-
from pinterest.client import default_sdk_client
1312
from pinterest.client import PinterestSDKClient
1413
from pinterest.ads.campaigns import Campaign
1514
from pinterest.ads.audiences import Audience
@@ -52,7 +51,7 @@ def create(
5251
name:str,
5352
owner_user_id:str,
5453
country:str,
55-
client:PinterestSDKClient = default_sdk_client,
54+
client:PinterestSDKClient = None,
5655
**kwargs
5756
) -> AdAccount:
5857
"""
@@ -77,6 +76,9 @@ def create(
7776
Returns:
7877
AdAccount: AdAccount Object
7978
"""
79+
if not client:
80+
client = cls._get_client()
81+
8082
country = Country(country)
8183
api_response = AdAccountsApi(client).ad_accounts_create(
8284
ad_account_create_request=AdAccountCreateRequest(

pinterest/ads/ad_groups.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
from pinterest.generated.client.model.ad_group_create_request import AdGroupCreateRequest
1212
from pinterest.generated.client.model.ad_group_update_request import AdGroupUpdateRequest
1313

14-
from pinterest.client import default_sdk_client
1514
from pinterest.client import PinterestSDKClient
1615
from pinterest.utils.base_model import PinterestBaseModel
1716
from pinterest.utils.error_handling import verify_api_response
1817
from pinterest.ads.ads import Ad
1918

19+
2020
class AdGroup(PinterestBaseModel):
2121
# pylint: disable=too-few-public-methods,too-many-locals,too-many-arguments,duplicate-code
2222
"""
@@ -63,7 +63,7 @@ def create(
6363
end_time:int = None,
6464
tracking_url:str = None,
6565
auto_targeting_enabled:bool = None,
66-
client:PinterestSDKClient = default_sdk_client,
66+
client:PinterestSDKClient = None,
6767
**kwargs
6868
) -> AdGroup:
6969
"""
@@ -153,6 +153,8 @@ def create(
153153
AdGroup: AdGroup Object
154154
"""
155155
billable_event = ActionType(billable_event)
156+
if not client:
157+
client = cls._get_client()
156158

157159
api_response = AdGroupsApi(client).ad_groups_create(
158160
ad_account_id=str(ad_account_id),
@@ -217,7 +219,7 @@ def get_all(
217219
page_size : int = None,
218220
order : str = "ASCENDING",
219221
bookmark : str = None,
220-
client : PinterestSDKClient = default_sdk_client,
222+
client : PinterestSDKClient = None,
221223
**kwargs
222224
) -> tuple[list[AdGroup], str]:
223225
"""
@@ -253,6 +255,8 @@ def get_all(
253255

254256
raw_ad_group_list = []
255257
return_bookmark = None
258+
if not client:
259+
client = cls._get_client()
256260

257261
ad_groups_api = AdGroupsApi(api_client=client)
258262
api_response = ad_groups_api.ad_groups_list(
@@ -326,3 +330,23 @@ def list_ads(
326330
client=self._client,
327331
**kwargs
328332
)
333+
334+
def enable_auto_targeting(self):
335+
"""
336+
Enable auto-targeting for ad group. Also known as <a
337+
href='https://help.pinterest.com/en/business/article/expanded-targeting'>"expanded targeting"</a>.
338+
339+
Returns:
340+
bool: true if ad group enable auto_targeting_enabled
341+
"""
342+
return self.update_fields(auto_targeting_enabled=True)
343+
344+
def disable_auto_targeting(self):
345+
"""
346+
Disable auto-targeting for ad group. Also known as <a
347+
href='https://help.pinterest.com/en/business/article/expanded-targeting'>"expanded targeting"</a>.
348+
349+
Returns:
350+
bool: true if ad group disable auto_targeting_enabled
351+
"""
352+
return self.update_fields(auto_targeting_enabled=False)

pinterest/ads/ads.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from pinterest.generated.client.model.entity_status import EntityStatus
1212
from pinterest.generated.client.model.ad_update_request import AdUpdateRequest
1313

14-
from pinterest.client import default_sdk_client
1514
from pinterest.client import PinterestSDKClient
1615
from pinterest.utils.error_handling import verify_api_response
1716
from pinterest.utils.base_model import PinterestBaseModel
@@ -67,7 +66,7 @@ def create(cls,
6766
name:str = None,
6867
tracking_urls:dict = None,
6968
view_tracking_url:str = None,
70-
client:PinterestSDKClient = default_sdk_client,
69+
client:PinterestSDKClient = None,
7170
**kwargs
7271
) -> Ad:
7372
# pylint: disable=too-many-locals,too-many-arguments
@@ -111,13 +110,16 @@ def create(cls,
111110
client (PinterestSDKClient, optional): PinterestSDKClient Object. Defaults to default_api_client.
112111
113112
Returns:
114-
Ad: Ad Object.
113+
Ad: The newly created Ad.
115114
"""
116115
# pylint: disable=too-many-arguments
117116

118117
creative_type=CreativeType(creative_type)
119118
status=EntityStatus(status)
120119

120+
if not client:
121+
client = cls._get_client()
122+
121123
api_response = AdsApi(client).ads_create(
122124
ad_account_id=str(ad_account_id),
123125
ad_create_request=[AdCreateRequest(
@@ -156,7 +158,7 @@ def get_all(
156158
page_size : int = None,
157159
order : str = "ASCENDING",
158160
bookmark : str = None,
159-
client : PinterestSDKClient = default_sdk_client,
161+
client : PinterestSDKClient = None,
160162
**kwargs
161163
) -> tuple[list[Ad], str]:
162164
# pylint: disable=too-many-arguments,too-many-locals
@@ -210,6 +212,9 @@ def get_all(
210212
raw_ad_list = []
211213
return_bookmark = None
212214

215+
if not client:
216+
client = cls._get_client()
217+
213218
ads_api = AdsApi(api_client=client)
214219
api_response = ads_api.ads_list(
215220
ad_account_id=ad_account_id,

pinterest/ads/audiences.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from pinterest.generated.client.model.audience_update_operation_type import AudienceUpdateOperationType
1616
from pinterest.generated.client.model.objective_type import ObjectiveType
1717

18-
from pinterest.client import default_sdk_client
1918
from pinterest.client import PinterestSDKClient
2019
from pinterest.utils.error_handling import verify_api_response
2120
from pinterest.utils.base_model import PinterestBaseModel
@@ -47,7 +46,7 @@ def create(
4746
rule : dict,
4847
audience_type : str,
4948
description : str = None,
50-
client: PinterestSDKClient = default_sdk_client,
49+
client: PinterestSDKClient = None,
5150
**kwargs
5251
) -> Audience:
5352
# pylint: disable=too-many-arguments
@@ -102,6 +101,9 @@ def create(
102101
rule['objective_type'] = ObjectiveType(rule['objective_type'])
103102
rule = AudienceRule(**rule)
104103

104+
if not client:
105+
client = cls._get_client()
106+
105107
api_response = AudiencesApi(client).audiences_create(
106108
ad_account_id=str(ad_account_id),
107109
audience_create_request=AudienceCreateRequest(
@@ -128,7 +130,7 @@ def get_all(
128130
page_size: int = None,
129131
order: str = "ASCENDING",
130132
bookmark: str = None,
131-
client: PinterestSDKClient = default_sdk_client,
133+
client: PinterestSDKClient = None,
132134
**kwargs
133135
) -> tuple[list[Audience], str]:
134136
# pylint: disable=too-many-arguments
@@ -168,6 +170,9 @@ def get_all(
168170
raw_audience_list = []
169171
return_bookmark = None
170172

173+
if not client:
174+
client = cls._get_client()
175+
171176
audiences_api = AudiencesApi(api_client=client)
172177
api_response = audiences_api.audiences_list(
173178
ad_account_id=ad_account_id,

pinterest/ads/campaigns.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from pinterest.generated.client.model.objective_type import ObjectiveType
1313

1414
from pinterest.ads.ad_groups import AdGroup
15-
from pinterest.client import default_sdk_client
1615
from pinterest.client import PinterestSDKClient
1716
from pinterest.utils.error_handling import verify_api_response
1817
from pinterest.utils.base_model import PinterestBaseModel
@@ -36,7 +35,7 @@ def __init__(
3635
Args:
3736
ad_account_id (str): Campaign's Ad Account ID.
3837
campaign_id (str): Campaign ID, must be associated with the Ad Account ID provided in the path.
39-
client (PinterestSDKClient, optional): PinterestSDKClient Object. Defaults to default_api_client.
38+
client (PinterestSDKClient, optional): PinterestSDKClient Object. Uses the default client, if not provided.
4039
"""
4140

4241
PinterestBaseModel.__init__(
@@ -68,7 +67,7 @@ def create(
6867
is_flexible_daily_budgets:bool = False,
6968
default_ad_group_budget_in_micro_currency:int = None,
7069
is_automated_campaign:bool = False,
71-
client:PinterestSDKClient = default_sdk_client,
70+
client:PinterestSDKClient = None,
7271
**kwargs
7372
) -> Campaign:
7473
# pylint: disable=too-many-locals,too-many-arguments
@@ -152,7 +151,7 @@ def create(
152151
of the associated advertiser account. Defaults to None.
153152
is_automated_campaign (bool, optional): Specifies whether the campaign was created
154153
in the automated campaign flow. Defaults to False.
155-
client (PinterestSDKClient): PinterestSDKClient Object
154+
client (PinterestSDKClient, optional): PinterestSDKClient Object, uses the default client, if not provided.
156155
157156
Keyword Args:
158157
Any valid keyword arguments or query parameters for endpoint.
@@ -163,6 +162,9 @@ def create(
163162

164163
objective_type = ObjectiveType(objective_type)
165164

165+
if not client:
166+
client = cls._get_client()
167+
166168
api_response = CampaignsApi(client).campaigns_create(
167169
ad_account_id=str(ad_account_id),
168170
campaign_create_request=[CampaignCreateRequest(
@@ -197,7 +199,7 @@ def get_all(
197199
page_size:int = None,
198200
order:str = "ASCENDING",
199201
bookmark:str = None,
200-
client:PinterestSDKClient = default_sdk_client,
202+
client:PinterestSDKClient = None,
201203
**kwargs
202204
) -> tuple[list[Campaign], str]:
203205
# pylint: disable=too-many-arguments
@@ -240,6 +242,9 @@ def get_all(
240242
raw_campaign_list = []
241243
return_bookmark = None
242244

245+
if not client:
246+
client = cls._get_client()
247+
243248
campaigns_api = CampaignsApi(api_client=client)
244249
api_response = campaigns_api.campaigns_list(
245250
ad_account_id=ad_account_id,

0 commit comments

Comments
 (0)