Skip to content

Add client for checking opt out status #41

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions examples/sample_optout_status_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import json
import sys

from uid2_client import OptOutStatusClient


def _usage():
print(
'Usage: python3 sample_optout_status_client.py <base_url> <api_key> <client_secret> <advertising_id_1> <advertising_id_2> ... <advertising_id_3>'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: maybe <advertising_id_n> instead of <advertising_id_3>?

, file=sys.stderr)
sys.exit(1)


if len(sys.argv) <= 4:
_usage()

base_url = sys.argv[1]
api_key = sys.argv[2]
client_secret = sys.argv[3]
advertising_ids = sys.argv[4:]

optout_status_client = OptOutStatusClient(base_url, api_key, client_secret)

response_json = json.loads(optout_status_client.get_optout_status(advertising_ids))
print(json.dumps(response_json, indent=2))
1 change: 1 addition & 0 deletions uid2_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@
from .identity_map_client import *
from .identity_map_input import *
from .identity_map_response import *
from .optout_status_client import *
41 changes: 41 additions & 0 deletions uid2_client/optout_status_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import base64
import datetime as dt
from datetime import timezone
import json

from uid2_client import auth_headers, make_v2_request, post, parse_v2_response


class OptOutStatusClient:
"""Client for interacting with UID Optout status

You will need to have the base URL of the endpoint and a client API key
and secret to consume web services.

Methods:
get_optout_status: Get Opt Out Status of advertising_ids
"""

def __init__(self, base_url, api_key, client_secret):
"""Create a new OptOutStatusClient client.

Args:
base_url (str): base URL for all requests to UID services (e.g. 'https://prod.uidapi.com')
api_key (str): api key for consuming the UID services
client_secret (str): client secret for consuming the UID services

Note:
Your authorization key will determine which UID services you are allowed to use.
"""
self._base_url = base_url
self._api_key = api_key
self._client_secret = base64.b64decode(client_secret)

def get_optout_status(self, advertising_ids):
request_payload = {
'advertising_ids': advertising_ids
}
req, nonce = make_v2_request(self._client_secret, dt.datetime.now(tz=timezone.utc),
json.dumps(request_payload).encode())
resp = post(self._base_url, '/v2/optout/status', headers=auth_headers(self._api_key), data=req)
return parse_v2_response(self._client_secret, resp.read(), nonce)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we decide to merge it, would you mind have some unit tests for this class?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NVM, we do not want to spend too much time on this. And it is a simple class. No unit tests is also good to me.