Combine two python scripts
Budget: $10 – $30 USD
Script 1 makes a REST API call to get the bearer token. Script 2 makes a REST API call to promote an api to higher environments.
The task is to take the response of script 1 and keep it in the header (22 'authorization': 'Bearer insertthebearertokenhere',) of script 2.
ALSO, in script 2, value of originApiId, <Org-ID>, <Env-ID> should be put in either a function or an argument so this entire process of promoting an API can be automated using python script(9 "originApiId": inserttheapiidhere,) & (4url =
"https://anypoint.mulesoft.com/apimanager/api/v1/organizations/<Org-ID>/environments/<Env-ID>/apis")
Script 1 :
import requests
2
3url = "xxx"
4
5payload='client_id=insertclientidhereinstead&client_secret=insertclientsecrethereinstead&grant_type=client_credentials'
6headers = {
7 'Content-Type': 'application/x-www-form-urlencoded'
8}
9
10response = requests.request("POST", url, headers=headers, data=payload)
11
12print(response.text)
Script 2 :
import requests
2import json
3
4url = "https://anypoint.mulesoft.com/apimanager/api/v1/organizations/<Org-ID>/environments/<Env-ID>/apis"
5
6payload = json.dumps({
7 "instanceLabel": "insertthelabelnamehere",
8 "promote": {
9 "originApiId": inserttheapiidhere,
10 "policies": {
11 "allEntities": True
12 },
13 "tiers": {
14 "allEntities": True
15 },
16 "alerts": {
17 "allEntities": True
18 }
19 }
20})
21headers = {
22 'authorization': 'Bearer insertthebearertokenhere',
23 'content-type': 'application/json'
24}
25
26response = requests.request("POST", url, headers=headers, data=payload)
27
28print(response.text)
The task is to take the response of script 1 and keep it in the header (22 'authorization': 'Bearer insertthebearertokenhere',) of script 2.
ALSO, in script 2, value of originApiId, <Org-ID>, <Env-ID> should be put in either a function or an argument so this entire process of promoting an API can be automated using python script(9 "originApiId": inserttheapiidhere,) & (4url =
"https://anypoint.mulesoft.com/apimanager/api/v1/organizations/<Org-ID>/environments/<Env-ID>/apis")
Script 1 :
import requests
2
3url = "xxx"
4
5payload='client_id=insertclientidhereinstead&client_secret=insertclientsecrethereinstead&grant_type=client_credentials'
6headers = {
7 'Content-Type': 'application/x-www-form-urlencoded'
8}
9
10response = requests.request("POST", url, headers=headers, data=payload)
11
12print(response.text)
Script 2 :
import requests
2import json
3
4url = "https://anypoint.mulesoft.com/apimanager/api/v1/organizations/<Org-ID>/environments/<Env-ID>/apis"
5
6payload = json.dumps({
7 "instanceLabel": "insertthelabelnamehere",
8 "promote": {
9 "originApiId": inserttheapiidhere,
10 "policies": {
11 "allEntities": True
12 },
13 "tiers": {
14 "allEntities": True
15 },
16 "alerts": {
17 "allEntities": True
18 }
19 }
20})
21headers = {
22 'authorization': 'Bearer insertthebearertokenhere',
23 'content-type': 'application/json'
24}
25
26response = requests.request("POST", url, headers=headers, data=payload)
27
28print(response.text)