import { getApiUrl } from "./getApiUrl" ; import { getApiKey } from "./getApiKey" ; import { queryStringify } from "./queryStringify" ; export function searchPixabayImages ( term , page ) { const apiUrl = getApiUrl ( ) ; const apiKey = getApiKey ( ) ; const response = UrlFetchApp . fetch ( apiUrl + '/search-pixabay-images?' + queryStringify ( { q : term , image_type : "all" , page : page . toString ( ) , safesearch : "true" } ) , { method : "get" , headers : { Authorization : "Bearer " + apiKey } , muteHttpExceptions : true } ) ; if ( response . getResponseCode ( ) === 200 ) { const hits = JSON . parse ( response . getContentText ( ) ) . hits ; return hits . map ( ( hit ) : string => { return JSON . stringify ( hit ) ; } ) ; } else if ( response . getResponseCode ( ) === 401 ) { throw new Error ( "The API key is invalid or expired." ) ; } else { throw new Error ( response . getContentText ( ) ) ; } }