Implementing a provider¶
Checklist for adding a new ad network to SPAPI.
1. Provider class (providers/<name>.py)¶
Subclass spapi.providers.provider.Provider and implement:
| Method / attribute | Purpose |
|---|---|
get_token() |
Exchange credentials for access token + expiry epoch |
api_url |
Base REST URL for the vendor |
headers |
Auth headers (usually Bearer) |
set_token_file_name() |
Override when multiple accounts share one provider |
campaign() |
Return your Campaign subclass |
report() |
Return your Report subclass |
Token bootstrap runs in Provider.__init__: Redis → file → get_token().
2. Campaign class (entities/<name>.py)¶
Subclass spapi.entities.campaign.Campaign:
| Method | Purpose |
|---|---|
get_campaign() |
Fetch one campaign; fill campaign_properties |
get_campaigns() |
List campaigns from the API |
get_extension() |
Extra JSON blob for vendor-specific fields |
post_create_campaign() |
Create campaign on vendor |
update_campaign() |
Push property changes |
| Provider-specific methods | Audiences, inventory, ad groups, etc. |
Use Campaign.insert_fields / update_fields for DB sync compatibility.
3. Report class (reports/<name>.py)¶
Subclass spapi.reports.report.Report:
| Method | Purpose |
|---|---|
get_cost_report(data_date=...) |
Daily cost/clicks normalized to cost_fields |
get_medium_report(...) |
Publisher/medium breakdown when applicable |
| Revenue helpers | As required by the network |
4. Decorator class (decorators/<name>.py)¶
Subclass spapi.decorators.decorator.Decorators with static methods that
prepare mutation payloads:
toggle_campaign,archive_campaignupdate_campaign_bid,update_campaign_budget,update_campaign_nameupdate_bulk_medium_bid, etc.
Decorators are applied by dealer/harvester automation — keep signatures aligned with existing providers.
5. Registration¶
Export the provider from spapi/__init__.py and providers/__init__.py so
consumers can import spapi and instantiate by name.
6. Tests¶
Add tests/test_<name>.py with mocked HTTP (see existing Taboola/Pinterest
tests). Run pytest before opening a PR to stage.
7. Documentation¶
Update references.md with the vendor API link and add a short
usage snippet to the root README.md.