
使用這些基本 REST API 最佳實踐構建出色的 API
3Scale 提供了一個管理 API 端點來獲取與每個用戶關聯的應用程序上下文。應用程序上下文包含與單個用戶相關的詳細信息,包括他們與 3Scale API 的交互,例如first_traffic_at
,first_daily_traffic_at
以及其他個人身份信息數據,例如 – user_id
、user_account_id
、service_id
、plan
信息和其他詳細信息。通過訪問這些詳細信息,可以輕松地將請求與特定用戶關聯起來。
根據我們使用的身份驗證方法,我們調用管理端點來獲取應用程序上下文。使用標準 API 密鑰身份驗證方法時,我們通過調用此端點來獲取應用程序上下文 –
curl -v -X GET "https://#{domain}/admin/api/applications.xml?access_token=#{ADMIN_ACCESS_TOKEN}&user_key=#{user_key}"
在使用應用程序標識符和密鑰對身份驗證方法時,我們通過調用此端點來獲取應用程序上下文 –
curl -v -X GET "https://#{domain}/admin/api/applications.xml?access_token=#{ADMIN_ACCESS_TOKEN}&app_id=#{app_id}&app_key=#{app_key}"
Moesif 有一個Luarocks 中可用的插件捕獲 API 請求和響應并記錄到 Moesif,以便通過 3Scale 輕松檢查和實時調試 API 流量。該插件在本地捕獲指標并對其進行排隊,這使得該插件能夠將指標數據發(fā)送到 Moesif 收集網絡,而不會影響您的應用。
推薦通過 Luarocks 安裝 Moesif:
luarocks install --server=http://luarocks.org/manifests/moesif lua-resty-moesif
3Scale 為最終用戶提供了靈活性,使其可以在調用 API 時通過HTTP_Headers
或傳遞身份驗證憑據query_parameters
。Moesif 會在標頭和查詢參數中查找憑據并獲取該特定用戶的應用程序上下文。Moesif 提供了一個配置選項來設置字段名稱,該名稱與配置 API 身份驗證設置時使用的名稱相同。默認情況下,3Scale 使用user_key
標準 API 密鑰和app_id
App_Idapp_key
和 App_Key 對身份驗證方法。
管理端點將應用程序上下文作為 XML 實體返回。Moesif 提供了一個配置選項,用于設置 3Scale 應用程序 XML 實體中的字段名稱,該字段名稱將用于識別用戶和公司(帳戶)。默認情況下,用戶和公司的字段名稱為id
和user_account_id
,但其他有效示例包括user_key
和service_id
。
-- Function to parse 3Scale XML entity
-- @param user_id_name
The 3scale field name from 3scale's application XML entity used to identify the user. Default id
.
-- @param company_id_name
The 3scale field name from 3scale's application XML entity used to identify the company (account). Default user_account_id
.
-- @param debug
A flag to print logs
function parseXML(user_id_name, company_id_name, debug)
-- config_response is the response from an api call to fetch application context which is a XML entity
local response_body = config_response:match("(%<.*>)")
if response_body ~= nil then
local xobject = xml.eval(response_body)
local xapplication = xobject:find("application")
if xapplication ~= nil then
local xtable = {}
for k, v in pairs(xapplication) do
if v ~= nil and type(v) == "table" then
xtable[v:tag()] = k
end
end
local key = xapplication[xtable[user_id_name]]
if key ~= nil then
if debug then
ngx.log(ngx.DEBUG, "Successfully fetched the userId ")
end
-- Set the UserId
local user_id = key[1]
else
if debug then
ngx.log(ngx.DEBUG, "The user_id_name provided by the user does not exist ")
end
end
local companyKey = xapplication[xtable[company_id_name]]
if companyKey ~= nil then
if debug then
ngx.log(ngx.DEBUG, "[moesif] Successfully fetched the companyId (accountId) ")
end
-- Set the CompanyId (AccountId)
local company_id = companyKey[1]
else
if debug then
ngx.log(ngx.DEBUG, "[moesif] The company_id_name provided by the user does not exist ")
end
end
else
if debug then
ngx.log(ngx.DEBUG, "Application tag does not exist ")
end
end
else
if debug then
ngx.log(ngx.DEBUG, "Xml response body does not exist ")
end
end
end
通過這種方式,插件會將每個事件或動作鏈接到單個客戶,并且可以通過查看多個事件來發(fā)現行為趨勢,以識別產品問題,例如用戶為什么停止使用您的 API 或他們最常使用哪些功能或端點。