Issue using API for creating Schedules automatically

Octavio Ornelas July 4, 2022

I have being tryng to create schedules automatically via python but I cannot, I have already been able to get the data but not to post, I am using the documentation which does not help much. When I try to post i receive the error 405, any suggestion?

1 answer

1 accepted

2 votes
Answer accepted
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 4, 2022

Hi @Octavio Ornelas ,

would you please share the code with us, so we can help you with it? Thank you.

Octavio Ornelas July 5, 2022

import requests
from requests.structures import CaseInsensitiveDict
from pandas import DataFrame, DateOffset, to_datetime

url = <url of the schedule>

headers = CaseInsensitiveDict()
headers["Authorization"] = "GenieKey <API KEY>"
headers["content-Type"] = "application/json"

resp = requests.post(url, headers = headers, json = <body os the schedule>)

print(resp.status_code)

 

BODY of the schedule

{'name': 'Factory Support - Space On-Call Schedule',
'description': 'On-call Schedule for Space Segment (Time Zone is EET)',
'timezone': 'Europe/Helsinki',
'enabled': True,
'ownerTeam': {'name': 'Factory Support - Space'},
'rotations' :{
'name': 'Week 30',
'startDate': '2022-07-25T15:00:00Z',
'endDate': '2022-07-31T07:00:00Z',
'type': 'weekly',
'length': 1,
'participants': [{'type': 'user',
'id': '60a0f278-cb62-4809-800d-ed5e7f483159'}],
'timeRestriction': {'type': 'weekday-and-time-of-day',
'restrictions': [{'startDay': 'monday',
'endDay': 'tuesday',
'startHour': 18,
'endHour': 10,
'startMin': 0,
'endMin': 0},
{'startDay': 'tuesday',
'endDay': 'wednesday',
'startHour': 18,
'endHour': 10,
'startMin': 0,
'endMin': 0},
{'startDay': 'wednesday',
'endDay': 'thursday',
'startHour': 18,
'endHour': 10,
'startMin': 0,
'endMin': 0},
{'startDay': 'thursday',
'endDay': 'friday',
'startHour': 18,
'endHour': 10,
'startMin': 0,
'endMin': 0},
{'startDay': 'friday',
'endDay': 'monday',
'startHour': 18,
'endHour': 10,
'startMin': 0,
'endMin': 0}]}}}

 

I get the response 405 I was thinking that maybe the body has an error but the format is exactly the same as the one shown in the documentation

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 5, 2022

Thank you @Octavio Ornelas . According to the documentation, the POST request needs to be send to https://api.opsgenie.com/v2/schedules, but you are using schedule URL, so maybe your URL is not right?

Octavio Ornelas July 5, 2022

I tried using this https://api.opsgenie.com/v2/schedules i got the 422 error 

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 5, 2022

According to the documentation error 422 means "Semantic errors in request body"

In JSON there should be double quotes instead of single quotes:

{  "name": "ScheduleName",  "description": "ScheduleDescription",  "timezone": "Europe/Kirov",  "enabled": true,  "ownerTeam": {  "name": "ops_team"  },  "rotations": [  {  "name": "First Rotation",  "startDate": "2017-02-06T05:00:00Z",  "endDate": "2017-02-23T06:00:00Z",  "type": "hourly",  "length": 6,  "participants": [  {  "type": "user",  "username": "user@opsgenie.com"  },  {  "type": "none"  },  {  "type": "user",  "id": "ac463592-dbd2-4ca3-alp1d-48fhf5j5c871"  }  ],  "timeRestriction": {  "type" : "weekday-and-time-of-day",  "restrictions" : [  {  "startDay" : "monday",  "startHour" : 8,  "startMin" : 0,  "endDay" : "tuesday",  "endHour" : 18,  "endMin" : 30  },  {  "startDay" : "wednesday",  "startHour" : 8,  "startMin" : 0,  "endDay" : "thursday",  "endHour" : 18,  "endMin" : 30  }  ]  }  }  ] }

Would you please try to change it?

Octavio Ornelas July 5, 2022

I changed the quotess and stil gives me the same response 422

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 5, 2022

@Octavio Ornelas Are you able to get also error message what's wrong with the request body, not only error code?

Octavio Ornelas July 5, 2022

it says --rotations is invalid which i assume means tht the dict rotations has a non expected value, this is the dict i am using

"rotations" :{
"name": "Week 30",
"startDate": "2022-07-25T15:00:00Z",
"endDate": "2022-07-31T07:00:00Z",
"type": "weekly",
"length": 1,
"participants": [{"type": "user",
"username": "<user_name"}],
"timeRestriction": {"type": "weekday-and-time-of-day",
"restrictions": [{"startDay": "monday",
"endDay": "tuesday",
"startHour": 18,
"endHour": 10,
"startMin": 0,
"endMin": 0},
{"startDay": "tuesday",
"endDay": "wednesday",
"startHour": 18,
"endHour": 10,
"startMin": 0,
"endMin": 0},
{"startDay": "wednesday",
"endDay": "thursday",
"startHour": 18,
"endHour": 10,
"startMin": 0,
"endMin": 0},
{"startDay": "thursday",
"endDay": "friday",
"startHour": 18,
"endHour": 10,
"startMin": 0,
"endMin": 0},
{"startDay": "friday",
"endDay": "monday",
"startHour": 18,
"endHour": 10,
"startMin": 0,
"endMin": 0}]}}}

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 6, 2022

Yes, it means something is wrong with this part of JSON, but I cannot tell what exactly. Please try to experiment a little bit with it to find it out.

Octavio Ornelas July 8, 2022

I have already found the bug but I have another issue when I am trying to update the schedule I cant do it I have the response 409 

<Response [405]>
{'message': "Request method 'POST' not supported", 'took': 0.001, 'requestId': 'c7a2aff3-3c27-4551-b248-e33c8bab846e'}

The code is the same iI just changed the id of the schedule to be updated in the url as the documentation says

url = "https://api.opsgenie.com/v2/schedules/1743f25-c16b-42e0-b017-3a8277410fda

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 10, 2022

Hi @Octavio Ornelas ,

according to the documentation you need to send PATCH request (not POST) to update the schedule.

Octavio Ornelas October 25, 2022

Hi! I was able to do it by using post, there was a mistake in the dictionary I was using, fortunately the bug was found 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
AUG Leaders

Atlassian Community Events