應用上下文

3Scale 提供了一個管理 API 端點來獲取與每個用戶關聯的應用程序上下文。應用程序上下文包含與單個用戶相關的詳細信息,包括他們與 3Scale API 的交互,例如first_traffic_atfirst_daily_traffic_at以及其他個人身份信息數據,例如 – user_iduser_account_idservice_idplan信息和其他詳細信息。通過訪問這些詳細信息,可以輕松地將請求與特定用戶關聯起來。

根據我們使用的身份驗證方法,我們調用管理端點來獲取應用程序上下文。使用標準 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}"

使用 3Scale 設置 Moesif API 分析

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_idApp_Idapp_key和 App_Key 對身份驗證方法。

識別用戶和公司(賬戶)

管理端點將應用程序上下文作為 XML 實體返回。Moesif 提供了一個配置選項,用于設置 3Scale 應用程序 XML 實體中的字段名稱,該字段名稱將用于識別用戶和公司(帳戶)。默認情況下,用戶和公司的字段名稱為iduser_account_id,但其他有效示例包括user_keyservice_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 或他們最常使用哪些功能或端點。

原文地址:https://www.moesif.com/blog/technical/3scale/How-to-Get-Customer-and-Application-Context-When-Logging-API-Calls-for-3scale-API-Gateway/

上一篇:

如何使用API:初學者的分步教程

下一篇:

使用這些基本 REST API 最佳實踐構建出色的 API
#你可能也喜歡這些API文章!

我們有何不同?

API服務商零注冊

多API并行試用

數據驅動選型,提升決策效率

查看全部API→
??

熱門場景實測,選對API

#AI文本生成大模型API

對比大模型API的內容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉化潛力

25個渠道
一鍵對比試用API 限時免費

#AI深度推理大模型API

對比大模型API的邏輯推理準確性、分析深度、可視化建議合理性

10個渠道
一鍵對比試用API 限時免費