Skip to content

Commit b813a22

Browse files
committed
edit methods
1 parent b0d3395 commit b813a22

File tree

1 file changed

+179
-60
lines changed

1 file changed

+179
-60
lines changed

parsons/newmode/newmode.py

Lines changed: 179 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
API_URL_V1 = "https://engage.newmode.net/api/"
1111
API_URL_V2 = "https://base.newmode.net/api/"
12-
API_AUTH_URL = "https://base.newmode.net/oauth/token"
12+
API_AUTH_URL = "https://base.newmode.net/oauth/token/"
13+
API_CAMPAIGNS_V2_URL = "https://base.newmode.net/"
1314

1415

1516
class Newmode(object):
@@ -37,7 +38,7 @@ def __init__(
3738
api_version="v1.0",
3839
):
3940
self.api_version = check_env.check("NEWMODE_API_VERSION", api_version)
40-
41+
# TODO: delete when v1 depreciated
4142
if "v1" in self.api_version:
4243
logger.warning(
4344
"Newmode API v1 will no longer be supported starting Feburary 2025."
@@ -58,15 +59,6 @@ def __init__(
5859
"NEWMODE_API_CLIENT_SECRET", client_secret
5960
)
6061
self.headers = {"content-type": "application/x-www-form-urlencoded"}
61-
self.client = OAuth2APIConnector(
62-
uri=self.base_url,
63-
auto_refresh_url=API_AUTH_URL,
64-
client_id=self.client_id,
65-
client_secret=self.__client_secret,
66-
headers=self.headers,
67-
token_url=API_AUTH_URL,
68-
grant_type="client_credentials",
69-
)
7062

7163
def convert_to_table(self, data):
7264
"""Internal method to create a Parsons table from a data element."""
@@ -79,10 +71,10 @@ def convert_to_table(self, data):
7971

8072
return table
8173

74+
# TODO: delete when v1 deprecated
8275
def base_request(
8376
self, method, url, requires_csrf=True, data=None, json=None, params={}
8477
):
85-
8678
if requires_csrf:
8779
csrf = self.get_csrf_token()
8880
self.headers["X-CSRF-Token"] = csrf
@@ -93,9 +85,24 @@ def base_request(
9385
elif method == "PATCH":
9486
response = self.client.patch_request(url=url, params=params)
9587
elif method == "POST":
96-
response = self.client.post_request(url=url, params=params, json=json)
88+
response = self.client.post_request(
89+
url=url, params=params, json=json, data=data
90+
)
9791
return response
9892

93+
def base_request_v2(self, method, url, data=None, json=None, params={}):
94+
response = None
95+
if method == "GET":
96+
response = self.client.get_request(url=url, params=params)
97+
elif method == "PATCH":
98+
response = self.client.patch_request(url=url, params=params)
99+
elif method == "POST":
100+
response = self.client.post_request(
101+
url=url, params=params, json=json, data=data
102+
)
103+
return response
104+
105+
# TODO: delete when v1 deprecated
99106
def converted_request(
100107
self,
101108
endpoint,
@@ -123,6 +130,42 @@ def converted_request(
123130
else:
124131
return response
125132

133+
def converted_request_v2(
134+
self,
135+
endpoint,
136+
method,
137+
supports_version=True,
138+
data=None,
139+
json=None,
140+
params={},
141+
convert_to_table=True,
142+
):
143+
self.client = OAuth2APIConnector(
144+
uri=self.base_url,
145+
auto_refresh_url=API_AUTH_URL,
146+
client_id=self.client_id,
147+
client_secret=self.__client_secret,
148+
headers=self.headers,
149+
token_url=API_AUTH_URL,
150+
grant_type="client_credentials",
151+
)
152+
153+
url = f"{self.api_version}/{endpoint}" if supports_version else endpoint
154+
response = self.base_request_v2(
155+
method=method,
156+
url=url,
157+
json=json,
158+
data=data,
159+
params=params,
160+
)
161+
if not response:
162+
logging.warning(f"Empty result returned from endpoint: {endpoint}")
163+
if convert_to_table:
164+
return self.convert_to_table(response)
165+
else:
166+
return response
167+
168+
# TODO:delete when v1 deprecated
126169
def get_csrf_token(self, max_retries=10):
127170
"""
128171
Retrieve a CSRF token for making API requests
@@ -154,6 +197,37 @@ def get_csrf_token(self, max_retries=10):
154197
)
155198
time.sleep(attempt + 1)
156199

200+
# def get_csrf_token_v2(self, max_retries=10):
201+
# """
202+
# Retrieve a CSRF token for making API requests
203+
# `Args:`
204+
# max_retries: int
205+
# The maximum number of attempts to get the CSRF token.
206+
# `Returns:`
207+
# The CSRF token.
208+
# """
209+
# endpoint = "/user/login?_format=json"
210+
# for attempt in range(max_retries):
211+
# # try:
212+
# response = self.converted_request_v2(
213+
# endpoint=endpoint,
214+
# method="POST",
215+
# supports_version=False,
216+
# convert_to_table=False,
217+
# )
218+
# print(response)
219+
# return response
220+
# except Exception as e:
221+
# if attempt >= max_retries:
222+
# logger.error(
223+
# (f"Error getting CSRF Token after {max_retries} retries")
224+
# )
225+
# raise e
226+
# logger.warning(
227+
# f"Retry {attempt} at getting CSRF Token failed. Retrying. Error: {e}"
228+
# )
229+
# time.sleep(attempt + 1)
230+
157231
def get_tools(self, params={}):
158232
"""
159233
V1 only
@@ -246,33 +320,26 @@ def run_action(self, tool_id, payload, params={}):
246320

247321
def get_campaigns(self, params={}):
248322
"""
249-
V1 & V2
323+
V1
250324
Retrieve all campaigns
251-
In v2, a campaign is equivalent to Tools or Actions in V1.
252325
`Args:`
253326
params: dict
254327
Query parameters to include in the request.
255328
`Returns:`
256329
Parsons Table containing campaigns data.
257330
"""
258-
if "v1" in self.api_version:
259-
endpoint = "campaign"
260-
requires_csrf = True
261-
else:
262-
self.api_version = "jsonapi"
263-
endpoint = "action/action"
264-
requires_csrf = False
331+
endpoint = "campaign"
332+
requires_csrf = True
265333
response = self.converted_request(
266334
endpoint=endpoint, method="GET", params=params, requires_csrf=requires_csrf
267335
)
268336
return response
269337

270338
def get_campaign(self, campaign_id, params={}):
271339
"""
272-
V1 & V2
340+
V1
273341
Retrieve a specific campaign by ID.
274342
275-
In v2, a campaign is equivalent to Tools or Actions in V1.
276343
`Args:`
277344
campaign_id: str
278345
The ID of the campaign to retrieve.
@@ -281,12 +348,8 @@ def get_campaign(self, campaign_id, params={}):
281348
`Returns:`
282349
Parsons Table containing campaign data.
283350
"""
284-
if "v1" in self.api_version:
285-
endpoint = f"campaign/{campaign_id}"
286-
requires_csrf = True
287-
else:
288-
endpoint = f"/campaign/{campaign_id}/form"
289-
requires_csrf = False
351+
endpoint = f"campaign/{campaign_id}"
352+
requires_csrf = True
290353
response = self.converted_request(
291354
endpoint=endpoint, method="GET", params=params, requires_csrf=requires_csrf
292355
)
@@ -421,6 +484,56 @@ def get_outreach(self, outreach_id, params={}):
421484
)
422485
return response
423486

487+
def get_campaign_v2(self, campaign_id, params={}):
488+
"""
489+
V2
490+
Retrieve a specific campaign by ID.
491+
492+
In v2, a campaign is equivalent to Tools or Actions in V1.
493+
`Args:`
494+
campaign_id: str
495+
The ID of the campaign to retrieve.
496+
params: dict
497+
Query parameters to include in the request.
498+
`Returns:`
499+
Parsons Table containing campaign data.
500+
"""
501+
endpoint = f"/campaign/{campaign_id}/form"
502+
response = self.converted_request_v2(
503+
endpoint=endpoint,
504+
method="GET",
505+
params=params,
506+
)
507+
return response
508+
509+
def get_campaigns_v2(self, organization_id, params={}):
510+
"""
511+
V2
512+
Retrieve all campaigns
513+
In v2, a campaign is equivalent to Tools or Actions in V1.
514+
`Args:`
515+
organization_id: str
516+
ID of organization
517+
params: dict
518+
Query parameters to include in the request.
519+
`Returns:`
520+
Parsons Table containing campaigns data.
521+
"""
522+
self.base_url = API_CAMPAIGNS_V2_URL
523+
self.api_version = "jsonapi"
524+
self.headers = {
525+
"content-type": "application/vnd.api+json",
526+
"accept": "application/vnd.api+json",
527+
"authorization": "Bearer 1234567890",
528+
}
529+
endpoint = f"action/action?filter[field_org_id]={organization_id}"
530+
response = self.converted_request_v2(
531+
endpoint=endpoint,
532+
method="GET",
533+
params=params,
534+
)
535+
return response
536+
424537
def get_recipient(
425538
self,
426539
campaign_id,
@@ -466,40 +579,42 @@ def get_recipient(
466579
for key, value in address_params.items()
467580
if value
468581
}
469-
response = self.converted_request(
582+
response = self.converted_request_v2(
470583
endpoint=f"campaign/{campaign_id}/target",
471584
method="GET",
472585
params=params,
473-
requires_csrf=False,
474586
)
475587
return response
476588

477-
def run_submit(self, campaign_id, json=None, data=None, params={}):
478-
"""
479-
V2 only
480-
Pass a submission from a supporter to a campaign
481-
that ultimately fills in a petition,
482-
sends an email or triggers a phone call
483-
depending on your campaign type
484-
485-
`Args:`
486-
campaign_id: str
487-
The ID of the campaign to retrieve.
488-
params: dict
489-
Query parameters to include in the request.
490-
`Returns:`
491-
Parsons Table containing submit data.
492-
"""
493-
response = self.converted_request(
494-
endpoint=f"campaign/{campaign_id}/submit",
495-
method="POST",
496-
data=data,
497-
json=json,
498-
params=params,
499-
requires_csrf=False,
500-
convert_to_table=False,
501-
)
502-
return response
589+
# TODO: add run_submit method
590+
# def run_submit(self, campaign_id, json=None, data=None, params={}):
591+
# """
592+
# V2 only
593+
# Pass a submission from a supporter to a campaign
594+
# that ultimately fills in a petition,
595+
# sends an email or triggers a phone call
596+
# depending on your campaign type
597+
598+
# `Args:`
599+
# campaign_id: str
600+
# The ID of the campaign to retrieve.
601+
# params: dict
602+
# Query parameters to include in the request.
603+
# `Returns:`
604+
# Parsons Table containing submit data.
605+
# """
606+
# # json["action_id"] = campaign_id
607+
# self.api_version = 2.0
608+
# response = self.converted_request(
609+
# endpoint=f"action/{campaign_id}/submit/?_format=json",
610+
# method="POST",
611+
# data=data,
612+
# json=json,
613+
# params=params,
614+
# requires_csrf=False,
615+
# convert_to_table=False,
616+
# )
617+
# return response
503618

504619
def get_submissions(self, params={}):
505620
"""
@@ -514,7 +629,11 @@ def get_submissions(self, params={}):
514629
`Returns:`
515630
Parsons Table containing submit data.
516631
"""
517-
response = self.converted_request(
518-
endpoint="submission", method="GET", params=params, requires_csrf=False
632+
# self.base_url = API_AUTH_URL
633+
# print(self.base_url)
634+
# response=self.converted_request_v2(endpoint="/user/login?_format=json", method="POST", supports_version=False)
635+
# self.token = self.token['access_token']
636+
response = self.converted_request_v2(
637+
endpoint="submission", method="GET", params=params
519638
)
520639
return response

0 commit comments

Comments
 (0)