beef331 / website

Code for the official Nim programming language website
https://nim-lang.org
18 stars 1 forks source link

Harpoon #25

Closed juancarlospaco closed 3 years ago

juancarlospaco commented 3 years ago

Name: HTTP Harpoon

Author: Juan

Posting:

GET and POST

Examples require to import harpoon and uri.

echo getContent(parseUri"http://httpbin.org/get")
echo postContent(parseUri"http://httpbin.org/post", "data here")

GET and POST from JsonNode to JsonNode

import std/json
let jsonData: JsonNode = %*{"key": "value", "other": 42}
doAssert getJson(parseUri"http://httpbin.org/get") is JsonNode
doAssert postJson(parseUri"http://httpbin.org/post", jsonData) is JsonNode

Download files

downloadFile(parseUri"http://httpbin.org/image/png", "temp.png")
downloadFile([(url: parseUri"http://httpbin.org/image/png", path: "temp.png"), 
              (url: parseUri"http://httpbin.org/image/jpg", path: "temp.jpg")])

HTTP Headers can be const

const header = newDefaultHeaders("body", "application/json", "application/json")
# Use std/sequtils to manipulate the header or something...

Async

import std/asyncdispatch

proc example() {.async.} =
  doAssert getContent("http://httpbin.org/get") is Future[string]
  doAssert deleteContent("http://httpbin.org/delete") is Future[string]
  doAssert putContent("http://httpbin.org/put", "data here") is Future[string]
  doAssert postContent("http://httpbin.org/post", "data here") is Future[string]
  doAssert patchContent("http://httpbin.org/patch", "data here") is Future[string]
  doAssert downloadFile("http://httpbin.org/image/png", "temp.png") is Future[void]

waitFor example()

PUT, DELETE, etc

echo deleteContent(parseUri"http://httpbin.org/delete")
echo putContent(parseUri"http://httpbin.org/put", "data here")
echo patchContent(parseUri"http://httpbin.org/patch", "data here")
beef331 commented 3 years ago

Due to only having a single submission this will be held until more projects, just for more quantity.