|
| 1 | +import json |
| 2 | + |
| 3 | +from ..core.rate_limit import rate_limiter |
| 4 | +from ..core.validation import ( |
| 5 | + KEY_REGEX, |
| 6 | + ORDER_BY_VALUES, |
| 7 | + SORT_BY_VALUES, |
| 8 | + validate_key, |
| 9 | + validate_limit, |
| 10 | + validate_page, |
| 11 | + validate_provider_keys, |
| 12 | + validate_reference, |
| 13 | + validate_response, |
| 14 | + validate_value, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +class ProfileMatching: |
| 19 | + def __init__(self, api): |
| 20 | + """Init.""" |
| 21 | + self.client = api |
| 22 | + |
| 23 | + @rate_limiter |
| 24 | + def list( |
| 25 | + self, |
| 26 | + source_key, |
| 27 | + profile_key=None, |
| 28 | + profile_reference=None, |
| 29 | + source_keys=None, |
| 30 | + page=1, |
| 31 | + limit=30, |
| 32 | + sort_by="created_at", |
| 33 | + order_by=None, |
| 34 | + created_at_min=None, |
| 35 | + created_at_max=None, |
| 36 | + **kwargs, |
| 37 | + ): |
| 38 | + """ |
| 39 | + Retrieve the matching information. |
| 40 | +
|
| 41 | + Args: |
| 42 | + profile_key: <string> |
| 43 | + profile_reference: <string> |
| 44 | + source_key: <string> |
| 45 | + source_keys: <list> |
| 46 | + source_keys |
| 47 | + limit: <int> (default to 30) |
| 48 | + number of fetched profiles/page |
| 49 | + page: <int> REQUIRED default to 1 |
| 50 | + number of the page associated to the pagination |
| 51 | + sort_by: <string> |
| 52 | + order_by: <string> |
| 53 | + created_at_min: <string> |
| 54 | + The minimum date of creation of the targeted Profiles. |
| 55 | + Format : "YYYY-MM-DD". |
| 56 | + created_at_max: <string> |
| 57 | + The maximum date of creation of the targeted Profiles. |
| 58 | + Format : "YYYY-MM-DD". |
| 59 | + Returns |
| 60 | + Applies the params to filter on Profiles in the targeted Sources and |
| 61 | + returns the response from the endpoint. |
| 62 | + Response examples : |
| 63 | + - Success response : |
| 64 | + { |
| 65 | + "code": 200, # response code |
| 66 | + "message": "Profile Matching results", # response message |
| 67 | + "meta" : {'page': 1, # current page |
| 68 | + 'maxPage': 5, # max page in the paginated response |
| 69 | + 'count': 2, # number of profiles in the current page |
| 70 | + 'total': 10}, # total number of profiles retrieved |
| 71 | + "data": { # list of profiles objects |
| 72 | + "predictions":[ |
| 73 | + [] |
| 74 | + ] |
| 75 | + "profiles":[ |
| 76 | + { |
| 77 | + "key": "xxx", |
| 78 | + "reference": "xxx", |
| 79 | + ... |
| 80 | + }, |
| 81 | + ... |
| 82 | + ] |
| 83 | + } |
| 84 | + } |
| 85 | + - Error response : (if the source_key is not valid) |
| 86 | + { |
| 87 | + "code": 400, |
| 88 | + "message": "Invalid parameters. Unable to find object: source" |
| 89 | + } |
| 90 | +
|
| 91 | + """ |
| 92 | + |
| 93 | + query_params = { |
| 94 | + "source_key": validate_key("Source", source_key, regex=KEY_REGEX), |
| 95 | + "profile_key": validate_key("Key", profile_key, regex=KEY_REGEX), |
| 96 | + "profile_reference": validate_reference(profile_reference), |
| 97 | + "source_keys": json.dumps(validate_provider_keys(source_keys)), |
| 98 | + "limit": validate_limit(limit), |
| 99 | + "page": validate_page(page), |
| 100 | + "sort_by": validate_value(sort_by, SORT_BY_VALUES, "sort by"), |
| 101 | + "order_by": validate_value(order_by, ORDER_BY_VALUES, "order by"), |
| 102 | + "created_at_min": created_at_min, # TODO validate dates format |
| 103 | + "created_at_max": created_at_max, # TODO validate dates format |
| 104 | + } |
| 105 | + |
| 106 | + params = {**query_params, **kwargs} |
| 107 | + response = self.client.get("profiles/matching", params) |
| 108 | + return validate_response(response) |
0 commit comments