Edits history of script submission #23956 for ' Get search job status (splunk)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Get search job status
     * Get a search job's properties (dispatchState, isDone, isFailed, doneProgress, resultCount, messages). Poll until dispatchState is DONE or FAILED. Returns { sid, ready: false } while the job is not ready yet.
     */
    export async function main(auth: RT.Splunk, sid: string) {
      const base = auth.base_url.replace(/\/+$/, "")
      const url = new URL(`${base}/services/search/jobs/${encodeURIComponent(sid)}`)
      url.searchParams.append("output_mode", "json")
    
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      if (response.status === 204) return { sid, ready: false }
    
      const data = (await response.json()) as { entry?: { content?: object }[] }
      return { sid, ready: true, ...data.entry?.[0]?.content }
    }
    

    Submitted by hugo989 3 hours ago