5
5
6
6
from __future__ import annotations
7
7
8
- from typing import Any , List , Mapping , Optional , Union
8
+ from typing import Any , Dict , List , Mapping , Optional , Union
9
9
10
10
from airbyte_cdk .test .mock_http .request import HttpRequest
11
11
@@ -16,7 +16,30 @@ def get_account_request(account_id: Optional[str] = ACCOUNT_ID) -> RequestBuilde
16
16
return RequestBuilder .get_account_endpoint (access_token = ACCESS_TOKEN , account_id = account_id )
17
17
18
18
19
+ def get_ads_request (account_id : Optional [str ] = ACCOUNT_ID ) -> RequestBuilder :
20
+ return RequestBuilder .get_ad_endpoint (access_token = ACCESS_TOKEN , account_id = account_id )
21
+
22
+
23
+ def get_campaigns_request (account_id : Optional [str ] = ACCOUNT_ID ) -> RequestBuilder :
24
+ return RequestBuilder .get_campaign_endpoint (access_token = ACCESS_TOKEN , account_id = account_id )
25
+
26
+
27
+ def get_ad_sets_request (account_id : Optional [str ] = ACCOUNT_ID ) -> RequestBuilder :
28
+ return RequestBuilder .get_ad_sets_endpoint (access_token = ACCESS_TOKEN , account_id = account_id )
29
+
30
+
19
31
class RequestBuilder :
32
+ @classmethod
33
+ def get_ad_endpoint (cls , access_token : str , account_id : str ) -> RequestBuilder :
34
+ return cls (access_token = access_token , resource = "ads" ).with_account_id (account_id )
35
+
36
+ @classmethod
37
+ def get_campaign_endpoint (cls , access_token : str , account_id : str ) -> RequestBuilder :
38
+ return cls (access_token = access_token , resource = "campaigns" ).with_account_id (account_id )
39
+
40
+ @classmethod
41
+ def get_ad_sets_endpoint (cls , access_token : str , account_id : str ) -> RequestBuilder :
42
+ return cls (access_token = access_token , resource = "adsets" ).with_account_id (account_id )
20
43
21
44
@classmethod
22
45
def get_account_endpoint (cls , access_token : str , account_id : str ) -> RequestBuilder :
@@ -68,6 +91,10 @@ def with_body(self, body: Union[str, bytes, Mapping[str, Any]]) -> RequestBuilde
68
91
self ._body = body
69
92
return self
70
93
94
+ def with_filtering (self , filters : List [Dict [str , Any ]]):
95
+ self ._query_params ["filtering" ] = self ._get_formatted_filters (filters )
96
+ return self
97
+
71
98
def build (self ) -> HttpRequest :
72
99
return HttpRequest (
73
100
url = f"https://graph.facebook.com/v19.0/{ self ._account_sub_path ()} { self ._resource } " ,
@@ -81,3 +108,29 @@ def _account_sub_path(self) -> str:
81
108
@staticmethod
82
109
def _get_formatted_fields (fields : List [str ]) -> str :
83
110
return "," .join (fields )
111
+
112
+ @staticmethod
113
+ def _get_formatted_filters (filters : List [Dict [str , Any ]]) -> str :
114
+ """
115
+ Used to create an acceptable by fb query param from list of dict filters in string format
116
+ From:
117
+ [{"field": "ad.effective_status", "operator": "IN", "value": ["ACTIVE", "ARCHIVED"]}, {"field": "ad.updated_time", "operator": "GREATER_THAN", "value": 1672531200}]
118
+ To:
119
+ '[{"field":"ad.effective_status","operator":"IN","value":["ACTIVE","ARCHIVED"]},' '{"field":"ad.updated_time","operator":"GREATER_THAN","value":1672531200}]'
120
+ """
121
+ field_filter = []
122
+ field_filters = []
123
+ for f in filters :
124
+ for key , value in f .items ():
125
+ if isinstance (value , list ):
126
+ value = "," .join ([f'"{ s } "' for s in value ])
127
+ field_filter .append (f'"{ key } ":[{ value } ]' )
128
+ elif isinstance (value , int ):
129
+ field_filter .append (f'"{ key } ":{ value } ' )
130
+ else :
131
+ field_filter .append (f'"{ key } ":"{ value } "' )
132
+ field_filters .append ("{" + f'{ "," .join (field_filter )} ' + "}" )
133
+ field_filter = []
134
+
135
+ field_filters_str = f'[{ "," .join (field_filters )} ]'
136
+ return field_filters_str
0 commit comments