class extends="BaseSearch" {
property name="apiKey" type="string" default="";
function getName() {
return "newsapi"
}
function configure( required struct config ) {
super.configure( arguments.config )
variables.apiKey = resolveApiKey( "NEWS_API_KEY", "newsApiKey" )
return this
}
array function doSearch( required string query, struct options = {} ) {
if ( !isNull( arguments.options ) && arguments.options.count() ) {
configure( arguments.options )
}
if ( !len( variables.apiKey ) ) {
throw( type: "MissingAPIKey", message: "NEWS_API_KEY is required for newsapi provider" )
}
var limit = arguments.options.maxResults ?: variables.maxResults
var response = executeRequest(
url: "https://newsapi.example.com/search?q=#encodeSearchQuery( arguments.query )#&limit=#limit#",
method: "GET",
headers: {
"Authorization": "Bearer #variables.apiKey#"
}
)
if ( response.statusCode != 200 ) {
throw(
type: "NewsApiError",
message: "News API returned HTTP #response.statusCode#"
)
}
var payload = jsonDeserialize( response.fileContent ?: "{}" )
var raw = payload.results ?: []
return formatResults( raw )
}
}