replaces curl with chronos http session

This commit is contained in:
benbierens 2024-08-30 09:44:55 +02:00
parent 0badfe563e
commit c9a090a80c
No known key found for this signature in database
GPG Key ID: 877D2C2E09A22F3A
1 changed files with 19 additions and 10 deletions

View File

@ -1,10 +1,12 @@
import std/os
import std/streams
import pkg/chronicles
import pkg/chronos
import pkg/ethers
import pkg/questionable
import pkg/questionable/results
import pkg/zip/zipfiles
import pkg/chronos/apps/http/httpclient
import ../../codex/contracts/marketplace
## TODO: chronicles is still "Log message not delivered: [Chronicles] A writer was not configured for a dynamic log output device"
@ -13,7 +15,7 @@ defaultChroniclesStream.outputs[0].writer =
proc (logLevel: LogLevel, msg: LogOutputStr) {.gcsafe.} =
echo msg
proc printHelp() =
proc printHelp() =
info "Usage: ./cirdl [circuitPath] [rpcEndpoint] [marketplaceAddress]"
info " circuitPath: path where circuit files will be placed."
info " rpcEndpoint: URL of web3 RPC endpoint."
@ -31,19 +33,26 @@ proc getCircuitHash(rpcEndpoint: string, marketplaceAddress: string): Future[?!s
proc formatUrl(hash: string): string =
"https://circuit.codex.storage/proving-key/" & hash
proc downloadZipfile(url: string, filepath: string): ?!void =
proc retrieveUrl(uri: string): Future[seq[byte]] {.async.} =
let httpSession = HttpSessionRef.new()
try:
# Nim's default webclient does not support SSL on all platforms.
# Not without shipping additional binaries and cert-files... :(
# So we're using curl for now.
var rc = execShellCmd("curl -o " & filepath & " " & url)
if not rc == 0:
return failure("Download of '" & url & "' failed with return code: " & $rc)
let resp = await httpSession.fetch(parseUri(uri))
return resp.data
finally:
await noCancel(httpSession.closeWait())
proc downloadZipfile(url: string, filepath: string): Future[?!void] {.async.} =
try:
let file = await retrieveUrl(url)
var s = newFileStream(filepath, fmWrite)
for b in file:
s.write(b)
s.close()
except Exception as exc:
return failure(exc.msg)
success()
proc unzip(zipfile:string, targetPath: string): ?!void =
proc unzip(zipfile:string, targetPath: string): ?!void =
var z: ZipArchive
if not z.open(zipfile):
return failure("Unable to open zip file: " & zipfile)
@ -74,7 +83,7 @@ proc main() {.async.} =
debug "Got circuithash", circuitHash
let url = formatUrl(circuitHash)
if dlErr =? downloadZipfile(url, zipfile).errorOption:
if dlErr =? (await downloadZipfile(url, zipfile)).errorOption:
error "Failed to download circuit file", msg = dlErr.msg
return
debug "Download completed"