應(yīng)用上下文

3Scale 提供了一個(gè)管理 API 端點(diǎn)來(lái)獲取與每個(gè)用戶關(guān)聯(lián)的應(yīng)用程序上下文。應(yīng)用程序上下文包含與單個(gè)用戶相關(guān)的詳細(xì)信息,包括他們與 3Scale API 的交互,例如first_traffic_at,first_daily_traffic_at以及其他個(gè)人身份信息數(shù)據(jù),例如 – user_iduser_account_id、service_id、plan信息和其他詳細(xì)信息。通過(guò)訪問(wèn)這些詳細(xì)信息,可以輕松地將請(qǐng)求與特定用戶關(guān)聯(lián)起來(lái)。

根據(jù)我們使用的身份驗(yàn)證方法,我們調(diào)用管理端點(diǎn)來(lái)獲取應(yīng)用程序上下文。使用標(biāo)準(zhǔn) API 密鑰身份驗(yàn)證方法時(shí),我們通過(guò)調(diào)用此端點(diǎn)來(lái)獲取應(yīng)用程序上下文 –

curl -v -X GET "https://#{domain}/admin/api/applications.xml?access_token=#{ADMIN_ACCESS_TOKEN}&user_key=#{user_key}"

在使用應(yīng)用程序標(biāo)識(shí)符和密鑰對(duì)身份驗(yàn)證方法時(shí),我們通過(guò)調(diào)用此端點(diǎn)來(lái)獲取應(yīng)用程序上下文 –

curl -v -X GET "https://#{domain}/admin/api/applications.xml?access_token=#{ADMIN_ACCESS_TOKEN}&app_id=#{app_id}&app_key=#{app_key}"

使用 3Scale 設(shè)置 Moesif API 分析

Moesif 有一個(gè)Luarocks 中可用的插件捕獲 API 請(qǐng)求和響應(yīng)并記錄到 Moesif,以便通過(guò) 3Scale 輕松檢查和實(shí)時(shí)調(diào)試 API 流量。該插件在本地捕獲指標(biāo)并對(duì)其進(jìn)行排隊(duì),這使得該插件能夠?qū)⒅笜?biāo)數(shù)據(jù)發(fā)送到 Moesif 收集網(wǎng)絡(luò),而不會(huì)影響您的應(yīng)用。

推薦通過(guò) Luarocks 安裝 Moesif:

luarocks install --server=http://luarocks.org/manifests/moesif lua-resty-moesif

身份驗(yàn)證憑證位置

3Scale 為最終用戶提供了靈活性,使其可以在調(diào)用 API 時(shí)通過(guò)HTTP_Headers或傳遞身份驗(yàn)證憑據(jù)query_parameters。Moesif 會(huì)在標(biāo)頭和查詢參數(shù)中查找憑據(jù)并獲取該特定用戶的應(yīng)用程序上下文。Moesif 提供了一個(gè)配置選項(xiàng)來(lái)設(shè)置字段名稱,該名稱與配置 API 身份驗(yàn)證設(shè)置時(shí)使用的名稱相同。默認(rèn)情況下,3Scale 使用user_key標(biāo)準(zhǔn) API 密鑰和app_idApp_Idapp_key和 App_Key 對(duì)身份驗(yàn)證方法。

識(shí)別用戶和公司(賬戶)

管理端點(diǎn)將應(yīng)用程序上下文作為 XML 實(shí)體返回。Moesif 提供了一個(gè)配置選項(xiàng),用于設(shè)置 3Scale 應(yīng)用程序 XML 實(shí)體中的字段名稱,該字段名稱將用于識(shí)別用戶和公司(帳戶)。默認(rèn)情況下,用戶和公司的字段名稱為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

結(jié)論

通過(guò)這種方式,插件會(huì)將每個(gè)事件或動(dòng)作鏈接到單個(gè)客戶,并且可以通過(guò)查看多個(gè)事件來(lái)發(fā)現(xiàn)行為趨勢(shì),以識(shí)別產(chǎn)品問(wèn)題,例如用戶為什么停止使用您的 API 或他們最常使用哪些功能或端點(diǎn)。

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

上一篇:

如何使用API:初學(xué)者的分步教程

下一篇:

使用這些基本 REST API 最佳實(shí)踐構(gòu)建出色的 API
#你可能也喜歡這些API文章!

我們有何不同?

API服務(wù)商零注冊(cè)

多API并行試用

數(shù)據(jù)驅(qū)動(dòng)選型,提升決策效率

查看全部API→
??

熱門(mén)場(chǎng)景實(shí)測(cè),選對(duì)API

#AI文本生成大模型API

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

25個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)

#AI深度推理大模型API

對(duì)比大模型API的邏輯推理準(zhǔn)確性、分析深度、可視化建議合理性

10個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)