-
Notifications
You must be signed in to change notification settings - Fork 1
add-rates-graphql-support #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adithyasamavedhi-ttd
wants to merge
2
commits into
main
Choose a base branch
from
add-graphql-support-for-rates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| """Example: third-party data rate GraphQL operations via ttd-data-python's | ||
| DataClient. | ||
|
|
||
| TTD_AUTH_TOKEN=... required. Platform token, sent as `TTD-Auth`. | ||
| GRAPHQL_EXAMPLE_PROVIDER_ID=... required. Provider to operate on. | ||
| GRAPHQL_EXAMPLE_BRAND_ID=... optional. Brand to filter rates by. | ||
| GRAPHQL_EXAMPLE_ELEMENT_ID=... optional. Segment to price. | ||
|
|
||
| Reads run unconditionally. The batch submission at the end is a write, and is | ||
| skipped unless GRAPHQL_EXAMPLE_ELEMENT_ID and GRAPHQL_EXAMPLE_BRAND_ID are | ||
| both set. The submitter is taken from the token's email. | ||
|
|
||
| TTD_AUTH_TOKEN=... GRAPHQL_EXAMPLE_PROVIDER_ID=... \ | ||
| python examples/graphql_rates_example.py | ||
| """ | ||
|
|
||
| import json | ||
| import os | ||
|
|
||
| from ttd_data import DataClient | ||
| from ttd_data.graphql import Page | ||
|
|
||
|
|
||
| def required(name: str, description: str) -> str: | ||
| value = os.getenv(name, "").strip() | ||
| if not value: | ||
| raise SystemExit(f"Set {name} to {description}.") | ||
| return value | ||
|
|
||
|
|
||
| token = required("TTD_AUTH_TOKEN", "a platform token") | ||
| PROVIDER_ID = required("GRAPHQL_EXAMPLE_PROVIDER_ID", "the provider to operate on") | ||
|
|
||
| BRAND_ID = os.getenv("GRAPHQL_EXAMPLE_BRAND_ID", "").strip() | ||
| ELEMENT_ID = os.getenv("GRAPHQL_EXAMPLE_ELEMENT_ID", "").strip() | ||
|
|
||
| client = DataClient(ttd_auth=token) | ||
| rates = client.third_party_data_rate | ||
|
|
||
|
|
||
| def show(label: str, page: Page) -> None: | ||
| print(f"\n{'=' * 60}\n {label} ({page.total_count} total)\n{'=' * 60}") | ||
| print(json.dumps(page.nodes, indent=2)) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Reads | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| show( | ||
| "Brands for provider", | ||
| rates.query_brands(provider_id=PROVIDER_ID, first=10), | ||
| ) | ||
|
|
||
| show( | ||
| "Data rates for provider segments", | ||
| rates.query_segment_data_rates(provider_id=PROVIDER_ID, first=5), | ||
| ) | ||
|
|
||
| if not BRAND_ID: | ||
| print("\nSkipping brand filter (set GRAPHQL_EXAMPLE_BRAND_ID to run it).") | ||
| else: | ||
| show( | ||
| f"Data rates filtered to brand {BRAND_ID}", | ||
| rates.query_segment_data_rates( | ||
| provider_id=PROVIDER_ID, brand_id=BRAND_ID, first=5 | ||
| ), | ||
| ) | ||
|
|
||
| show( | ||
| "Data rate batches", | ||
| rates.query_data_rate_batches(provider_id=PROVIDER_ID, first=10), | ||
| ) | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Mutation — this submits a rate batch against the provider | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| if not (ELEMENT_ID and BRAND_ID): | ||
| print( | ||
| "\nSkipping batch submission (needs GRAPHQL_EXAMPLE_ELEMENT_ID " | ||
| "and GRAPHQL_EXAMPLE_BRAND_ID)." | ||
| ) | ||
| else: | ||
| # Omitting `subject` makes this a system (syndicated) rate. | ||
| result = rates.create_data_rate_batch( | ||
| provider_id=PROVIDER_ID, | ||
| data_rates=[ | ||
| { | ||
| "providerElementId": ELEMENT_ID, | ||
| "thirdPartyDataBrandId": BRAND_ID, | ||
| "cost": {"cpm": {"cpmCost": {"amount": 2.5, "currencyCode": "USD"}}}, | ||
| } | ||
| ], | ||
| ) | ||
| print(f"\n{'=' * 60}\n Submit data rate batch\n{'=' * 60}") | ||
| if result.data is None: | ||
| print(f"rejected: {json.dumps(result.errors, indent=2)}") | ||
| else: | ||
| print(json.dumps(result.data, indent=2)) | ||
| # Queued, not applied: read the batch back to follow its processing. | ||
| show( | ||
| "Submitted batch", | ||
| rates.query_data_rate_batches( | ||
| provider_id=PROVIDER_ID, batch_id=result.data["id"] | ||
| ), | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we include how to set subject in the example too?