<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>https://wiki.statsape.com/index.php?action=history&amp;feed=atom&amp;title=%E6%A8%A1%E5%9D%97%3ACommon%2FApi</id>
	<title>模块:Common/Api - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.statsape.com/index.php?action=history&amp;feed=atom&amp;title=%E6%A8%A1%E5%9D%97%3ACommon%2FApi"/>
	<link rel="alternate" type="text/html" href="https://wiki.statsape.com/index.php?title=%E6%A8%A1%E5%9D%97:Common/Api&amp;action=history"/>
	<updated>2026-11-10T17:44:45Z</updated>
	<subtitle>本wiki上该页面的版本历史</subtitle>
	<generator>MediaWiki 1.39.6</generator>
	<entry>
		<id>https://wiki.statsape.com/index.php?title=%E6%A8%A1%E5%9D%97:Common/Api&amp;diff=3794&amp;oldid=prev</id>
		<title>Zeroclanzhang：​创建页面，内容为“local common = {}  --- Checks if Api Request was successful and if the Response is valid --- @param response table --- @param errorOnData boolean --- @param errorOnData boolean --- @return boolean function common.checkResponseStructure( response, errorOnStatus, errorOnData )     if response[ &#039;status_code&#039; ] ~= nil and response[ &#039;status_code&#039; ] ~= 200 then         if errorOnStatus == nil or errorOnStatus == true then             error( &#039;API request returns the e…”</title>
		<link rel="alternate" type="text/html" href="https://wiki.statsape.com/index.php?title=%E6%A8%A1%E5%9D%97:Common/Api&amp;diff=3794&amp;oldid=prev"/>
		<updated>2023-12-13T08:26:26Z</updated>

		<summary type="html">&lt;p&gt;创建页面，内容为“local common = {}  --- Checks if Api Request was successful and if the Response is valid --- @param response table --- @param errorOnData boolean --- @param errorOnData boolean --- @return boolean function common.checkResponseStructure( response, errorOnStatus, errorOnData )     if response[ &amp;#039;status_code&amp;#039; ] ~= nil and response[ &amp;#039;status_code&amp;#039; ] ~= 200 then         if errorOnStatus == nil or errorOnStatus == true then             error( &amp;#039;API request returns the e…”&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;local common = {}&lt;br /&gt;
&lt;br /&gt;
--- Checks if Api Request was successful and if the Response is valid&lt;br /&gt;
--- @param response table&lt;br /&gt;
--- @param errorOnData boolean&lt;br /&gt;
--- @param errorOnData boolean&lt;br /&gt;
--- @return boolean&lt;br /&gt;
function common.checkResponseStructure( response, errorOnStatus, errorOnData )&lt;br /&gt;
    if response[ &amp;#039;status_code&amp;#039; ] ~= nil and response[ &amp;#039;status_code&amp;#039; ] ~= 200 then&lt;br /&gt;
        if errorOnStatus == nil or errorOnStatus == true then&lt;br /&gt;
            error( &amp;#039;API request returns the error code &amp;#039; .. response[ &amp;#039;status_code&amp;#039; ] .. &amp;#039;(&amp;#039; .. response[ &amp;#039;message&amp;#039; ] .. &amp;#039;)&amp;#039;, 0 )&lt;br /&gt;
        end&lt;br /&gt;
        return false&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if response[ &amp;#039;data&amp;#039; ] == nil then&lt;br /&gt;
        if errorOnData == nil or errorOnData == true then&lt;br /&gt;
            error( &amp;#039;API data does not contain a &amp;quot;data&amp;quot; field&amp;#039;, 0 )&lt;br /&gt;
        end&lt;br /&gt;
        return false&lt;br /&gt;
    end&lt;br /&gt;
    return true&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--- Sets the table to return nil for unknown keys instead of erroring out&lt;br /&gt;
--- For deep nested tables use apiData:get( &amp;#039;table1.table2.table3&amp;#039; ) etc.&lt;br /&gt;
--- @param apiData table - The json decoded data from the api&lt;br /&gt;
--- @return table&lt;br /&gt;
function common.makeAccessSafe( apiData )&lt;br /&gt;
	local function set( data )&lt;br /&gt;
		setmetatable( data, {&lt;br /&gt;
			__index = function(self, key)&lt;br /&gt;
				return nil&lt;br /&gt;
			end&lt;br /&gt;
		} )&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	local function iterSet( data )&lt;br /&gt;
		set( data )&lt;br /&gt;
&lt;br /&gt;
		for _, v in pairs( data ) do&lt;br /&gt;
			if type( v ) == &amp;#039;table&amp;#039; then&lt;br /&gt;
				iterSet( v )&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	iterSet( apiData )&lt;br /&gt;
&lt;br /&gt;
	apiData.get = function( self, key )&lt;br /&gt;
		local parts = mw.text.split( key, &amp;#039;.&amp;#039;, true )&lt;br /&gt;
&lt;br /&gt;
		local val = self&lt;br /&gt;
		for _, part in ipairs( parts ) do&lt;br /&gt;
			local success, conv = pcall( tonumber, part, 10 )&lt;br /&gt;
			if success and conv ~= nil then&lt;br /&gt;
				part = conv&lt;br /&gt;
			end&lt;br /&gt;
&lt;br /&gt;
			if val[ part ] == nil then&lt;br /&gt;
				return nil&lt;br /&gt;
			end&lt;br /&gt;
&lt;br /&gt;
			val = val[ part ]&lt;br /&gt;
		end&lt;br /&gt;
&lt;br /&gt;
		if val == nil or ( type( val ) == &amp;#039;table&amp;#039; and #val == 0 ) or type( val ) == &amp;#039;function&amp;#039; then&lt;br /&gt;
			return nil&lt;br /&gt;
		end&lt;br /&gt;
&lt;br /&gt;
		return val&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	return apiData&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return common&lt;/div&gt;</summary>
		<author><name>Zeroclanzhang</name></author>
	</entry>
</feed>