Skip to content

Commit

Permalink
fix python bindings, improve openapi
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethosa committed Oct 27, 2024
1 parent 75e27d7 commit db96bbb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion happyx.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

description = "Macro-oriented asynchronous web-framework written with ♥"
author = "HapticX"
version = "4.6.3"
version = "4.6.4"
license = "MIT"
srcDir = "src"
installExt = @["nim"]
Expand Down
2 changes: 1 addition & 1 deletion src/happyx/core/constants.nim
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const
# Framework version
HpxMajor* = 4
HpxMinor* = 6
HpxPatch* = 3
HpxPatch* = 4
HpxVersion* = $HpxMajor & "." & $HpxMinor & "." & $HpxPatch


Expand Down
22 changes: 21 additions & 1 deletion src/happyx/ssr/docs/autodocs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ proc procApiDocs*(docsData: NimNode): NimNode =
"newApiDocObject",
newCall("@", bracket(newLit"GET")),
ident"documentation",
newLit"",
newDotExpr(ident"routeData", ident"path"),
newDotExpr(ident"routeData", ident"pathParams"),
newDotExpr(ident"routeData", ident"requestModels"),
Expand All @@ -225,6 +226,7 @@ proc procApiDocs*(docsData: NimNode): NimNode =
"newApiDocObject",
newDotExpr(ident"route", ident"httpMethod"),
ident"documentation",
newLit"",
newDotExpr(ident"routeData", ident"path"),
newDotExpr(ident"routeData", ident"pathParams"),
newDotExpr(ident"routeData", ident"requestModels"),
Expand Down Expand Up @@ -373,11 +375,13 @@ proc openApiDocs*(docsData: NimNode): NimNode =
"responses": {
"200": {
"description": "",
"content": {}
"content": {},
"headers": {},
}
}
}

# Parse all status codes
for m in route.src.findAll(re2"\bstatusCode\b\s*=\s*(\d+)(\s*#+\s*([^\n]+))?"):
let
statusCode = route.src[m.group(0)]
Expand All @@ -389,6 +393,22 @@ proc openApiDocs*(docsData: NimNode): NimNode =
"content": {}
}

# Parse all outHeaders
for m in route.src.findAll(re2"""\boutHeaders\b\["([^\"\]]+)"\]\s*=\s*(.*#+\s*((\d+):\s*)?([^\n]+))?"""):
let
header = route.src[m.group(0)]
statusCode = route.src[m.group(3)]
description = route.src[m.group(4)]
if statusCode == "" or statusCode notin pathData["responses"]:
for code in pathData["responses"].keys():
pathData["responses"][code]["headers"][header] = %*{
"description": description
}
else:
pathData["responses"][statusCode]["headers"][header] = %*{
"description": description
}

# echo route.srcd

# Params
Expand Down
6 changes: 4 additions & 2 deletions tests/testc15.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ type
OptionsEncoding* = enum
encodingA, encodingB


model TestModel:
lang: Language

Expand Down Expand Up @@ -62,8 +61,11 @@ serve "127.0.0.1", 5000:
return {"response": m.x}

get "/teststatuscodes/{i:int}":
## you can test here status codes
outHeaders["Server"] = "HappyX " & HpxVersion ## Here just a server
if i mod 2 == 0:
statusCode = 403 ## i is not even
outHeaders["Reason"] = "bye world" ## 403: error reason
statusCode = 403 ## i is not even
return i
return i

Expand Down

0 comments on commit db96bbb

Please sign in to comment.