Sentry API 使用筆記
Sentry API 使用筆記
本文為使用Sentry API的筆記, 并非搭建Sentry的筆記
官方文檔: ?https://docs.sentry.io/api/
官方社區(qū): ?https://forum.sentry.io/
官方提醒:(2017年5月) The current version of the web API is known as v0 and is considered to be in a draft phase. While we don’t expect public endpoints to change greatly, keep in mind that the API is still under development.
身份驗(yàn)證
sentry API 的身份驗(yàn)證參數(shù)通過(guò)請(qǐng)求頭傳遞,所有API都需要包含此請(qǐng)求頭。
requests.get(api_url, headers=header)
如果沒(méi)有提供此請(qǐng)求頭參數(shù)會(huì)返回錯(cuò)誤提示: {"detail": "Authentication credentials were not provided."} 。
headers 具體寫法為:
{'Authorization': 'Bearer TOKEN'}
其中TOKEN在項(xiàng)目設(shè)置中的API -- Auth Tokens 里設(shè)置. 如果TOKEN錯(cuò)誤會(huì)返回錯(cuò)誤提示: {"detail": "Invalid token"}
API 列表
Sentry 的API分為幾類, 具體每個(gè)API的含義可以直接看官方文檔. 本文以Events類別下的List a Project’s Events舉例說(shuō)明如何使用這些API.
官方說(shuō)明如下:
GET?/api/0/projects/{organization_slug}/{project_slug}/events/
Return?a?list?of?events?bound?to?a?project.
Note:?This?endpoint?is?experimental?and?may?be?removed?without?notice.
Path?Parameters:
organization_slug?(string)?–?the?slug?of?the?organization?the?groups?belong?to.
project_slug?(string)?–?the?slug?of?the?project?the?groups?belong?to.
Method:
GET
Path:
/api/0/projects/{organization_slug}/{project_slug}/events/即:
請(qǐng)求方式:get
請(qǐng)求地址:你的sentry地址/api/0/projects/{organization_slug}/{project_slug}/events/
其中organization_slug 是組織名, project_slug 是項(xiàng)目名。
注意:
+ 并非所有請(qǐng)求都是get方式,比如刪除相關(guān)API使用delete,更新相關(guān)API使用put。
+ 有些API還需要其他請(qǐng)求頭, 比如更新issue的API還需要Content-Type: application/json
處理返回結(jié)果
此處以List a Project’s Events? API 為例。
如果參數(shù)都沒(méi)問(wèn)題的話API會(huì)以字符串形式返回100個(gè)(如果有的話,不足100個(gè)則直接返回全部) events 組成的列表, 每個(gè)event 都是json 格式的。
直接使用json模塊載入返回結(jié)果:
json.loads(response.text)
這樣會(huì)得到一個(gè)列表, 每項(xiàng)均是一個(gè)dict, 每個(gè)dict包含了一個(gè)event的信息。
如果這個(gè)項(xiàng)目的event不止100條, sentry還會(huì)返回下一頁(yè)的url。 沒(méi)錯(cuò), sentry把100條event作為一頁(yè)請(qǐng)求返回給接口調(diào)用, 要獲得下一頁(yè)events的話需要從返回結(jié)果的響應(yīng)頭 response.headers 中獲取下一頁(yè)請(qǐng)求地址。
官方說(shuō)明如下:
HTTP/1.0?200?OK Date:?Sat,?14?Feb?2015?18:47:20?GMT Content-Type:?application/json Content-Language:?en Allow:?GET,?HEAD,?OPTIONS Link:; ??rel="previous";?results="false",; ??rel="next";?results="true"
其中Link中就包含了我們需要的下一頁(yè)地址(示例中為https://sentry.io/api/0/projects/1/groups/?&cursor=1420837533:0:0`)
link = response.headers.get('Link')
接下來(lái)用正則或者其他什么方式從字符串里提取下一頁(yè)地址再請(qǐng)求即可, 一直到無(wú)法提取到下一頁(yè)地址或下一頁(yè)地址返回結(jié)果為空。





