1

我正在使用 Pagerduty python api 客户端 - pdpyras 下面是代码块

session = APISession(api_token)
changedate = os.popen("date -u -d '24 hours ago' +'%FT%T%.2Z'").read()
print(changedate)
changeurl = "change_events?include[]=integration&since=" + changedate
print(changeurl)
change_dump = session.rget(changeurl)
print(change_dump[0])

理想情况下,这应该让我从 pagerduty 的 change-events 页面获得第一个事件。但是,由于在 changeurl 传递给 session.rget 时添加了垃圾字符,它会失败

请参阅以下输出

2021-03-26T09:47:53.2Z

change_events?include[]=integration&since=2021-03-26T09:47:53.2Z

Traceback (most recent call last):
  File "dailyincidents.py", line 28, in <module>
    change_dump = session.rget(changeurl)
  File "/Users/saha/.pyenv/versions/3.7.7/lib/python3.7/site-packages/pdpyras.py", line 190, in call
    return method(self, url, **kw)
  File "/Users/saha/.pyenv/versions/3.7.7/lib/python3.7/site-packages/pdpyras.py", line 143, in call
    r = raise_on_error(method(self, path, **pass_kw))
  File "/Users/saha/.pyenv/versions/3.7.7/lib/python3.7/site-packages/pdpyras.py", line 84, in raise_on_error
    ), response=r
pdpyras.PDClientError: GET /change_events?include%5B%5D=integration&since=2021-03-26T09:47:53.2Z%0A: API responded with non-success status (400)

这里的问题是您在错误“include%5B%5D”的最后一行看到的垃圾字符,最后是“%0A”。因为如果我直接运行下面的代码块,我可以成功提取细节。

change_dump = session.rget("change_events?include[]=integration&since=2021-03-26T09:47:53.2Z")

仅当字符串被替换时才会出现此问题,它需要这些垃圾字符。不知道它是如何被挑选出来的,以及如何解决这个问题。任何指针?

编辑

我能够部分摆脱垃圾字符。但是,最后还有一个,我不知道该怎么做。以下是我所做的更改。

parameters = "include[]=integration&since=" + changedate
change_dump = session.rget("change_events", params=parameters)

现在,错误如下

pdpyras.PDClientError: GET /change_events?include[]=integration&since=2021-03-26T14:14:08.2Z%0A: API 以非成功状态响应 (400)

如您所见,现在 url 中的最后 3 个字符 %0A: 导致了问题。请问有什么指点吗?

4

1 回答 1

1

以下格式应该适合您。

parameters = "include[]=integration&since=" + changedate
change_dump = session.rget("change_events",params=parameters.rstrip('\n').replace("\n", ""))
于 2021-03-30T10:13:17.583 回答