<!--- Shared Vertex API bearer token (used by vertex_api_process_job and call_vertex_api status poll) --->
<cfinclude template="vertex_api_config.cfm" />
<cfparam name="vertexAccessToken" default="" />
<cfparam name="vertexAccessTokenType" default="" />
<cfparam name="vertexTokenStatusCode" default="" />
<cfparam name="vertexTokenResponseBody" default="" />
<cfparam name="vertexTokenRequestBody" default="" />

<cfset vertexAccessToken = "" />
<cfset vertexAccessTokenType = "" />
<cfset vertexTokenURL = vertexApiBaseHost & "/token" />
<cfset vertexTokenFormParams = {
    "username" = "superman",
    "password" = "Hf5*!BK"
} />
<cfset vertexTokenRequestBody = "username=superman&password=" & urlEncodedFormat("Hf5*!BK") />

<cfhttp url="#vertexTokenURL#" method="post" timeout="60" result="vertexTokenResponse" throwonerror="false">
    <cfhttpparam type="header" name="accept" value="application/json" />
    <cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded" />
    <cfloop collection="#vertexTokenFormParams#" item="tokenKey">
        <cfhttpparam type="formField" name="#tokenKey#" value="#vertexTokenFormParams[tokenKey]#" />
    </cfloop>
</cfhttp>

<cfset vertexTokenStatusCode = vertexTokenResponse.StatusCode />
<cfset vertexTokenResponseBody = trim(vertexTokenResponse.FileContent) />

<cfif Left(vertexTokenStatusCode, 3) EQ "200" AND len(vertexTokenResponseBody) AND isJSON(vertexTokenResponseBody)>
    <cfset vertexTokenData = deserializeJSON(vertexTokenResponseBody) />
    <cfif structKeyExists(vertexTokenData, "access_token")>
        <cfset vertexAccessToken = trim(vertexTokenData.access_token) />
    <cfelseif structKeyExists(vertexTokenData, "accessToken")>
        <cfset vertexAccessToken = trim(vertexTokenData.accessToken) />
    </cfif>
    <cfif structKeyExists(vertexTokenData, "token_type")>
        <cfset vertexAccessTokenType = trim(vertexTokenData.token_type) />
    <cfelseif structKeyExists(vertexTokenData, "tokenType")>
        <cfset vertexAccessTokenType = trim(vertexTokenData.tokenType) />
    </cfif>
</cfif>

<cfif NOT len(vertexAccessTokenType)>
    <cfset vertexAccessTokenType = "Bearer" />
</cfif>
