-1

How would I parse the current system date as a parameter in the URL as a string.

I want the current system date as 'start' and 'end' date to be the last 7 days from the system date.

Here's my code

headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer 123456',
}

params = {
   'mailbox' : '192659',
   'types' : 'email',
   'officeHours' : 'true',
   'start' : '2019-09-21T00:00:00Z',
   'end' : '2019-09-22T00:00:00Z',

}

response = requests.get('https://api.helpscout.net/', headers=headers, params=params)

print(response.json())

Thanks!

Arron
  • 25
  • 9

1 Answers1

1

You can do this using datetime.timedelta:

from datetime import datetime
from datetime import timedelta

headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer 123456',
}

current_date=datetime.now()
end_date=current_date+timedelta(days=7)

current_date=str(current_date).split('.')[0]+'Z'
end_date=str(end_date).split('.')[0]+'Z'

params = {
   'mailbox' : '192659',
   'types' : 'email',
   'officeHours' : 'true',
   'start' : current_date,
   'end' : end_date,

}

response = requests.get('https://api.helpscout.net/', headers=headers, params=params)

print(response.json())

Hope this helps! I did this using Python3.

Made changes to the code according to your requirement.

Joe
  • 6,758
  • 2
  • 26
  • 47
Apurva Singh
  • 411
  • 3
  • 14
  • Hey thanks for that! The only issue Is that I need that date converting to ISO 8601 format like this 2019-09-21T00:00:00Z Can I change this? – Arron Sep 23 '19 at 11:55
  • @Arron Hi, I made changes to the code for your requirement. I hope this is what you were looking for. :) – Apurva Singh Sep 23 '19 at 16:40
  • Hi Apurva, nearly there but I get this response. "Invalid date - expected ISO 8601 format (yyyy-MM-dd'T'HH:mm:ss'Z')", 'rejectedValue': '2019-09-24T11:45:04.787088' Thanks for your help – Arron Sep 24 '19 at 10:46
  • @Arron Something from this should get you the expected format. https://stackoverflow.com/questions/2150739/iso-time-iso-8601-in-python – Apurva Singh Sep 24 '19 at 10:54
  • @Arron Please try the code now. I have made changes to the current_date and end_date string according to the error message format they are expecting. Hoping this works. I tried running the code on my system but I am getting an invalid token error. – Apurva Singh Sep 24 '19 at 12:24