Module:USASpending-rcpt-displaytitle

From Defense Knowledge

Documentation for this module may be created at Module:USASpending-rcpt-displaytitle/doc

local p = {}

function p.main(frame)

    -- Get pagename
    local pageName = frame.args.page or mw.title.getCurrentTitle().text

    -- Fetch data from the API
    local data = mw.ext.externaldata.getWebData {
        url = 'https://api.usaspending.gov/api/v2/recipient/' .. pageName .. '/?format=json',
        format = 'JSON',
        ['use jsonpath'] = true,
        data = {
            name = '$.name',
            uei = '$.uei',
        },
        ['cache seconds'] = 86400
    }

    local name = data.name or ''
    local uei = data.uei or ''

    local output = {}
    table.insert(output, uei)
    table.insert(output, frame:preprocess('{{DISPLAYTITLE:' .. name .. '}}'))
    table.insert(output, frame:preprocess('{{DEFAULTSORT:' .. name .. '|noerror}}'))
    
    return table.concat(output, '\n')    
end

return p