Skip to content

Commit

Permalink
Add files argument
Browse files Browse the repository at this point in the history
  • Loading branch information
mishankov committed Aug 19, 2024
1 parent 1205cdf commit 8a16932
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/yahttp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const defaultEncodeQueryParams = EncodeQueryParams(usePlus: false, omitEq: true,
proc request*(url: string, httpMethod: Method = Method.GET, headers: openArray[
RequestHeader] = [], query: openArray[QueryParam] = [],
encodeQueryParams: EncodeQueryParams = defaultEncodeQueryParams,
body: string = "", data: tuple[name, fileName, contentType, content: string] = ("", "", "", ""),
body: string = "", files: openArray[tuple[name, fileName, contentType, content: string]] = [],
auth: BasicAuth = ("", ""), timeout = -1, ignoreSsl = false, sslContext: SslContext = nil): Response =
## Genreal proc to make HTTP request with every HTTP method

Expand Down Expand Up @@ -131,9 +131,10 @@ proc request*(url: string, httpMethod: Method = Method.GET, headers: openArray[

# Make request

let response = if data.name != "":
let response = if files.len() > 0:
var multipartData = newMultipartData()
multipartData[data.name] = (data.fileName, data.contentType, data.content)
for file in files:
multipartData[file.name] = (file.fileName, file.contentType, file.content)
client.request(innerUrl, httpMethod = innerMethod, multipart = multipartData)
else:
client.request(innerUrl, httpMethod = innerMethod, body = body)
Expand Down
3 changes: 2 additions & 1 deletion src/yahttp/internal/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ macro http_method_gen*(name: untyped): untyped =
let comment = newCommentStmtNode(fmt"Proc for {methodUpper} HTTP method")
quote do:
proc `name`*(url: string, headers: openArray[RequestHeader] = [], query: openArray[
QueryParam] = [], encodeQueryParams: EncodeQueryParams = defaultEncodeQueryParams, body: string = "", auth: BasicAuth = ("", ""), timeout = -1,
QueryParam] = [], encodeQueryParams: EncodeQueryParams = defaultEncodeQueryParams, body: string = "", files: openArray[tuple[name, fileName, contentType, content: string]] = [], auth: BasicAuth = ("", ""), timeout = -1,
ignoreSsl = false, sslContext: SslContext = nil): Response =
`comment`
return request(
Expand All @@ -15,6 +15,7 @@ macro http_method_gen*(name: untyped): untyped =
headers = headers,
query = query,
body = body,
files = files,
auth = auth,
timeout = timeout,
ignoreSsl = ignoreSsl,
Expand Down

0 comments on commit 8a16932

Please sign in to comment.