import httpclient, json, os, strutils, times import ../../main/nim/strawbosspkg/core import ../../main/nim/strawbosspkg/configuration proc newAuthenticatedHttpClient*(apiBase, uname, pwd: string): HttpClient = result = newHttpClient() let authResp = result.post(apiBase & "/auth-token", $(%*{"username": uname, "password": pwd})) assert authResp.status.startsWith("200") result.headers = newHttpHeaders({"Authorization": "Bearer " & parseJson(authResp.body).getStr}) proc waitForBuild*(client: HttpClient, apiBase, projectName, runId: string, expectedState = BuildState.complete, failedState = BuildState.failed, timeout = 10): Run = let startTime = epochTime() var run: Run #echo "Waiting for '" & $expectedState & "' from run:\n\t" & # apiBase & "/project/" & projectName & "/run/" & runId while true: var curElapsed = epochTime() - startTime #echo "Checking (" & $curElapsed & " has passed)." if curElapsed > toFloat(timeout): raise newException(Exception, "Timeout exceeded waiting for build.") let resp = client.get(apiBase & "/project/" & projectName & "/run/" & runId) #echo "Received resp:\n\n" & $resp.status & "\n\n" & $resp.body if not resp.status.startsWith("200"): raise newException(IOError, "Unable to retrieve status. Received response: " & resp.body) run = parseRun(parseJson(resp.body)) if run.status.state == failedState: raise newException(IOError, "Run transitioned to failed state '" & $failedState & "'") if run.status.state == expectedState: return run sleep(200)