Skip to content

Commit

Permalink
Release: Version v0.1.2 (#20)
Browse files Browse the repository at this point in the history
- Lazy load PinterestSDKClient
- Add config.json support
- Add AdGroup methods: enable_auto_targeting, disable_auto_targeting
  • Loading branch information
dfana01 authored Nov 14, 2022
1 parent e9a0242 commit 0854c18
Show file tree
Hide file tree
Showing 17 changed files with 324 additions and 79 deletions.
19 changes: 16 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
build:
clean: clean-build clean-pyc ## Clean

clean-build: ## Clean python build
rm -fr build/
rm -fr dist/
rm -fr *.egg-info
rm -fr .tox

clean-pyc: ## Clean python binaries
find . -name '*.pyc' -exec rm -f {} +
find . -name '*.pyo' -exec rm -f {} +
find . -name '*~' -exec rm -f {} +

build: ## Build command
python -m build
ls -l dist

pip_release_install:
pip install twine build

publish_pypi_test: pip_release_install build
publish_pypi_test: clean pip_release_install build
twine upload -r testpypi dist/*

publish_pypi: pip_release_install build
publish_pypi: clean pip_release_install build
twine upload -r pypi dist/*
41 changes: 35 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,56 @@ You can now use the SDK.

## Getting Started

For use the client you need set basic variables for that you have two option setup environment variables (using a
.env file or set in your OS) or create a config.json.

### Setting up environment variables

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)
To configure the client using environment variables, you must create a **.env** file using [.env.example](./pinterest/.env.example)
as a template. For basic configuration and usage you need to set the following environment variables in the **.env** file:

```
PINTEREST_ACCESS_TOKEN='<access token>'
```
_or_
```
PINTEREST_APP_ID=<app id>
PINTEREST_APP_SECRET=<app secret>
PINTEREST_REFRESH_ACCESS_TOKEN='<refresh token>'
**or**
PINTEREST_ACCESS_TOKEN='<access token>'
```

Once you have established the environment variables, the client will be instantiated for you automatically.
Once you have established the environment variables, the client will be instantiated for you automatically.

**NOTE**:
* 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.
* 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.

### Setting up config.json

To configure the client using config.json, you must create a **config.json** file using [config.json.example](./pinterest/config.json.example)
as a template. For basic configuration and usage you need to set the following key in the **config.json** file:

```json
{
"app_id": "<app id>",
"app_secret": "<app secret>",
"refresh_access_token": "<refresh token>"
}
```

**or**

```json
{
"access_token": "<access token>"
}
```

Once you have established the keys, the client will be instantiated for you automatically.

**NOTE**:
* 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.
* 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.

For more information visit the [Authentication](https://developers.pinterest.com/docs/getting-started/authentication/#Refreshing%20an%20access%20token) page.

## Samples
Expand Down
14 changes: 14 additions & 0 deletions config.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"debug": "true",
"port": "0",
"app_id": "<app id>",
"app_secret": "<app secret>",
"redirect_uri": "localhost",
"response_type": "code",
"scope": "<scopes>",
"state": "dev",
"access_token_json_path": "./",
"access_token": "<access token>",
"refresh_access_token": "<refresh token>",
"api_uri": "https://api.pinterest.com/v5"
}
6 changes: 4 additions & 2 deletions pinterest/ads/ad_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from pinterest.generated.client.model.ad_account import AdAccount as GeneratedAdAccount
from pinterest.generated.client.model.ad_account_create_request import AdAccountCreateRequest

from pinterest.client import default_sdk_client
from pinterest.client import PinterestSDKClient
from pinterest.ads.campaigns import Campaign
from pinterest.ads.audiences import Audience
Expand Down Expand Up @@ -52,7 +51,7 @@ def create(
name:str,
owner_user_id:str,
country:str,
client:PinterestSDKClient = default_sdk_client,
client:PinterestSDKClient = None,
**kwargs
) -> AdAccount:
"""
Expand All @@ -77,6 +76,9 @@ def create(
Returns:
AdAccount: AdAccount Object
"""
if not client:
client = cls._get_client()

country = Country(country)
api_response = AdAccountsApi(client).ad_accounts_create(
ad_account_create_request=AdAccountCreateRequest(
Expand Down
30 changes: 27 additions & 3 deletions pinterest/ads/ad_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
from pinterest.generated.client.model.ad_group_create_request import AdGroupCreateRequest
from pinterest.generated.client.model.ad_group_update_request import AdGroupUpdateRequest

from pinterest.client import default_sdk_client
from pinterest.client import PinterestSDKClient
from pinterest.utils.base_model import PinterestBaseModel
from pinterest.utils.error_handling import verify_api_response
from pinterest.ads.ads import Ad


class AdGroup(PinterestBaseModel):
# pylint: disable=too-few-public-methods,too-many-locals,too-many-arguments,duplicate-code
"""
Expand Down Expand Up @@ -63,7 +63,7 @@ def create(
end_time:int = None,
tracking_url:str = None,
auto_targeting_enabled:bool = None,
client:PinterestSDKClient = default_sdk_client,
client:PinterestSDKClient = None,
**kwargs
) -> AdGroup:
"""
Expand Down Expand Up @@ -153,6 +153,8 @@ def create(
AdGroup: AdGroup Object
"""
billable_event = ActionType(billable_event)
if not client:
client = cls._get_client()

api_response = AdGroupsApi(client).ad_groups_create(
ad_account_id=str(ad_account_id),
Expand Down Expand Up @@ -217,7 +219,7 @@ def get_all(
page_size : int = None,
order : str = "ASCENDING",
bookmark : str = None,
client : PinterestSDKClient = default_sdk_client,
client : PinterestSDKClient = None,
**kwargs
) -> tuple[list[AdGroup], str]:
"""
Expand Down Expand Up @@ -253,6 +255,8 @@ def get_all(

raw_ad_group_list = []
return_bookmark = None
if not client:
client = cls._get_client()

ad_groups_api = AdGroupsApi(api_client=client)
api_response = ad_groups_api.ad_groups_list(
Expand Down Expand Up @@ -326,3 +330,23 @@ def list_ads(
client=self._client,
**kwargs
)

def enable_auto_targeting(self):
"""
Enable auto-targeting for ad group. Also known as <a
href='https://help.pinterest.com/en/business/article/expanded-targeting'>"expanded targeting"</a>.
Returns:
bool: true if ad group enable auto_targeting_enabled
"""
return self.update_fields(auto_targeting_enabled=True)

def disable_auto_targeting(self):
"""
Disable auto-targeting for ad group. Also known as <a
href='https://help.pinterest.com/en/business/article/expanded-targeting'>"expanded targeting"</a>.
Returns:
bool: true if ad group disable auto_targeting_enabled
"""
return self.update_fields(auto_targeting_enabled=False)
13 changes: 9 additions & 4 deletions pinterest/ads/ads.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from pinterest.generated.client.model.entity_status import EntityStatus
from pinterest.generated.client.model.ad_update_request import AdUpdateRequest

from pinterest.client import default_sdk_client
from pinterest.client import PinterestSDKClient
from pinterest.utils.error_handling import verify_api_response
from pinterest.utils.base_model import PinterestBaseModel
Expand Down Expand Up @@ -67,7 +66,7 @@ def create(cls,
name:str = None,
tracking_urls:dict = None,
view_tracking_url:str = None,
client:PinterestSDKClient = default_sdk_client,
client:PinterestSDKClient = None,
**kwargs
) -> Ad:
# pylint: disable=too-many-locals,too-many-arguments
Expand Down Expand Up @@ -111,13 +110,16 @@ def create(cls,
client (PinterestSDKClient, optional): PinterestSDKClient Object. Defaults to default_api_client.
Returns:
Ad: Ad Object.
Ad: The newly created Ad.
"""
# pylint: disable=too-many-arguments

creative_type=CreativeType(creative_type)
status=EntityStatus(status)

if not client:
client = cls._get_client()

api_response = AdsApi(client).ads_create(
ad_account_id=str(ad_account_id),
ad_create_request=[AdCreateRequest(
Expand Down Expand Up @@ -156,7 +158,7 @@ def get_all(
page_size : int = None,
order : str = "ASCENDING",
bookmark : str = None,
client : PinterestSDKClient = default_sdk_client,
client : PinterestSDKClient = None,
**kwargs
) -> tuple[list[Ad], str]:
# pylint: disable=too-many-arguments,too-many-locals
Expand Down Expand Up @@ -210,6 +212,9 @@ def get_all(
raw_ad_list = []
return_bookmark = None

if not client:
client = cls._get_client()

ads_api = AdsApi(api_client=client)
api_response = ads_api.ads_list(
ad_account_id=ad_account_id,
Expand Down
11 changes: 8 additions & 3 deletions pinterest/ads/audiences.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from pinterest.generated.client.model.audience_update_operation_type import AudienceUpdateOperationType
from pinterest.generated.client.model.objective_type import ObjectiveType

from pinterest.client import default_sdk_client
from pinterest.client import PinterestSDKClient
from pinterest.utils.error_handling import verify_api_response
from pinterest.utils.base_model import PinterestBaseModel
Expand Down Expand Up @@ -47,7 +46,7 @@ def create(
rule : dict,
audience_type : str,
description : str = None,
client: PinterestSDKClient = default_sdk_client,
client: PinterestSDKClient = None,
**kwargs
) -> Audience:
# pylint: disable=too-many-arguments
Expand Down Expand Up @@ -102,6 +101,9 @@ def create(
rule['objective_type'] = ObjectiveType(rule['objective_type'])
rule = AudienceRule(**rule)

if not client:
client = cls._get_client()

api_response = AudiencesApi(client).audiences_create(
ad_account_id=str(ad_account_id),
audience_create_request=AudienceCreateRequest(
Expand All @@ -128,7 +130,7 @@ def get_all(
page_size: int = None,
order: str = "ASCENDING",
bookmark: str = None,
client: PinterestSDKClient = default_sdk_client,
client: PinterestSDKClient = None,
**kwargs
) -> tuple[list[Audience], str]:
# pylint: disable=too-many-arguments
Expand Down Expand Up @@ -168,6 +170,9 @@ def get_all(
raw_audience_list = []
return_bookmark = None

if not client:
client = cls._get_client()

audiences_api = AudiencesApi(api_client=client)
api_response = audiences_api.audiences_list(
ad_account_id=ad_account_id,
Expand Down
15 changes: 10 additions & 5 deletions pinterest/ads/campaigns.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from pinterest.generated.client.model.objective_type import ObjectiveType

from pinterest.ads.ad_groups import AdGroup
from pinterest.client import default_sdk_client
from pinterest.client import PinterestSDKClient
from pinterest.utils.error_handling import verify_api_response
from pinterest.utils.base_model import PinterestBaseModel
Expand All @@ -36,7 +35,7 @@ def __init__(
Args:
ad_account_id (str): Campaign's Ad Account ID.
campaign_id (str): Campaign ID, must be associated with the Ad Account ID provided in the path.
client (PinterestSDKClient, optional): PinterestSDKClient Object. Defaults to default_api_client.
client (PinterestSDKClient, optional): PinterestSDKClient Object. Uses the default client, if not provided.
"""

PinterestBaseModel.__init__(
Expand Down Expand Up @@ -68,7 +67,7 @@ def create(
is_flexible_daily_budgets:bool = False,
default_ad_group_budget_in_micro_currency:int = None,
is_automated_campaign:bool = False,
client:PinterestSDKClient = default_sdk_client,
client:PinterestSDKClient = None,
**kwargs
) -> Campaign:
# pylint: disable=too-many-locals,too-many-arguments
Expand Down Expand Up @@ -152,7 +151,7 @@ def create(
of the associated advertiser account. Defaults to None.
is_automated_campaign (bool, optional): Specifies whether the campaign was created
in the automated campaign flow. Defaults to False.
client (PinterestSDKClient): PinterestSDKClient Object
client (PinterestSDKClient, optional): PinterestSDKClient Object, uses the default client, if not provided.
Keyword Args:
Any valid keyword arguments or query parameters for endpoint.
Expand All @@ -163,6 +162,9 @@ def create(

objective_type = ObjectiveType(objective_type)

if not client:
client = cls._get_client()

api_response = CampaignsApi(client).campaigns_create(
ad_account_id=str(ad_account_id),
campaign_create_request=[CampaignCreateRequest(
Expand Down Expand Up @@ -197,7 +199,7 @@ def get_all(
page_size:int = None,
order:str = "ASCENDING",
bookmark:str = None,
client:PinterestSDKClient = default_sdk_client,
client:PinterestSDKClient = None,
**kwargs
) -> tuple[list[Campaign], str]:
# pylint: disable=too-many-arguments
Expand Down Expand Up @@ -240,6 +242,9 @@ def get_all(
raw_campaign_list = []
return_bookmark = None

if not client:
client = cls._get_client()

campaigns_api = CampaignsApi(api_client=client)
api_response = campaigns_api.campaigns_list(
ad_account_id=ad_account_id,
Expand Down
Loading

0 comments on commit 0854c18

Please sign in to comment.