Skip to content

Commit

Permalink
Allow to transform router to test .http file for vs code rest client
Browse files Browse the repository at this point in the history
  • Loading branch information
e-marchand committed Jun 19, 2020
1 parent d8fd55c commit 86bcac3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
30 changes: 18 additions & 12 deletions Project/Sources/Classes/Context.4dm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Class constructor
C_TEXT:C284($1;$2;$3;$4;$5;$6)
This:C1470.relativeReference:=$1 // https://tools.ietf.org/html/rfc3986#section-4.2
This:C1470.relativeReference:=$1// https://tools.ietf.org/html/rfc3986#section-4.2
If (Position:C15("?";This:C1470.relativeReference)>0)
This:C1470.path:=Substring:C12(This:C1470.relativeReference;1;Position:C15("?";This:C1470.relativeReference)-1)
Else
Expand Down Expand Up @@ -28,17 +28,17 @@ See WEB GET VARIABLES.
*/
Function variables
C_OBJECT:C1216($0)
$0:=WebGetVariables
$0:=WebGetVariables

/*
Return the http headers variables as object.
See WEB GET HTTP HEADER.
*/
Function headers
// alt: parse raw http if defined?
// alt: parse raw http if defined?
C_OBJECT:C1216($0)
If (This:C1470._headers=Null:C1517)
This:C1470._headers:=WebGetHTTPHeaders
This:C1470._headers:=WebGetHTTPHeaders
End if
$0:=This:C1470._headers

Expand All @@ -47,7 +47,7 @@ Function hasHeader
C_BOOLEAN:C305($0)
C_OBJECT:C1216($headers)
$headers:=This:C1470.headers()
$0:=($headers[$1]#Null:C1517) | ($headers[Lowercase:C14($1)]#Null:C1517) // 4D seems to change case of headers....
$0:=($headers[$1]#Null:C1517) | ($headers[Lowercase:C14($1)]#Null:C1517)// 4D seems to change case of headers....

Function header
C_TEXT:C284($1;$2)
Expand All @@ -56,16 +56,16 @@ Function header
$headers:=This:C1470.headers()
$0:=$headers[$1]
Case of
: ($0=Null:C1517) // must not occur if string?
: ($0=Null:C1517)// must not occur if string?
$0:=$headers[Lowercase:C14($1)]
: (Length:C16($0)=0)
$0:=$headers[Lowercase:C14($1)]
End case

// Checks if the specified content types are acceptable, based on the request’s Accept HTTP header field. The method
// Checks if the specified content types are acceptable, based on the request’s Accept HTTP header field. The method
Function _accepts
C_BOOLEAN:C305($0)
$0:=True:C214 // TODO
$0:=True:C214// TODO

Function bodyText
C_TEXT:C284($0;$request)
Expand All @@ -89,7 +89,7 @@ Function bodyPart
C_BLOB:C604($vPartContentBlob)
C_LONGINT:C283($cpt)
$0:=New collection:C1472()
For ($cpt;1;WEB Get body part count:C1211) //for each part
For ($cpt;1;WEB Get body part count:C1211)//for each part
WEB GET BODY PART:C1212($cpt;$vPartContentBlob;$vPartName;$vPartMimeType;$vPartFileName)
$0.push(New object:C1471("name";$vPartName;"content";$vPartContentBlob;"mimeType";$vPartMimeType;"filename";$vPartFileName))
End for
Expand All @@ -99,7 +99,7 @@ Function bodyTextPart
C_TEXT:C284($vPartName;$vPartMimeType;$vPartFileName;$vDestinationFolder;$vPartContentText)
C_LONGINT:C283($cpt)
$0:=New collection:C1472()
For ($cpt;1;WEB Get body part count:C1211) //for each part
For ($cpt;1;WEB Get body part count:C1211)//for each part
WEB GET BODY PART:C1212($cpt;$vPartContentText;$vPartName;$vPartMimeType;$vPartFileName)
$0.push(New object:C1471("name";$vPartName;"content";$vPartContentText;"mimeType";$vPartMimeType;"filename";$vPartFileName))
End for
Expand All @@ -122,7 +122,13 @@ Function secure

Function protocol
C_TEXT:C284($0)
$0:=Choose:C955(This:C1470.secure;"https";"http")
$0:=Choose:C955(This:C1470.secure();"https";"http")

Function port
C_LONGINT:C283($0)
C_OBJECT:C1216($info)
$info:=WEB Get server info:C1531
$0:=Choose:C955(This:C1470.secure();$info.options.webHTTPSPortID;$info.options.webPortID)

Function xhr
C_BOOLEAN:C305($0)
Expand All @@ -135,7 +141,7 @@ Function popClone
C_LONGINT:C283($pos)
$pos:=Position:C15("/";$path;2)
If ($pos>1)
$path:=Substring:C12($path;$pos) // remove first path element
$path:=Substring:C12($path;$pos)// remove first path element
End if

$0:=cs:C1710.Context.new($path;This:C1470.rawHTTP;This:C1470.client.ip;This:C1470.server.ip;This:C1470.basicAuth.username;This:C1470.basicAuth.password)
Expand Down
30 changes: 29 additions & 1 deletion Project/Sources/Classes/Router.4dm
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,33 @@ Function respond

Function restClientHTTP
C_TEXT:C284($0;$result)
$result:=JSON Stringify:C1217(This:C1470)// TODO convert to rest client format
C_OBJECT:C1216($1;$context)
$context:=$1

$result:="@hostname="+$context.server.ip+"\n"\
+"@port="+String:C10($context.port())+"\n"\
+"@baseURL="+$context.protocol()+"://{{hostname}}:{{port}}\n\n"

For each ($path;This:C1470.routes)
$result:=$result+This:C1470._restClientHTTPR(This:C1470.routes[$path])+"\n"
End for each
$0:=$result

Function _restClientHTTPR
C_OBJECT:C1216($1;$route)
C_TEXT:C284($0;$result)
$result:=""
$route:=$1
If ($route.methods#Null:C1517)
For each ($method;$route.methods)
$result:=$result+"###\n"
$result:=$result+$method+" {{baseURL}}"+$route.path+"\n"
End for each
Else
$done:=False:C215
For each ($path;$route) Until ($done)
$result:=$result+This:C1470._restClientHTTPR($route[$path])
$done:=Position:C15("__";$path)=1// if indexed by method do only one times, les .methods do the job?
End for each
End if
$0:=$result
2 changes: 1 addition & 1 deletion Project/Sources/Methods/_new router test.4dm
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ $router.get("/format";Formula:C1597($2.format($1;New object:C1471(\

$router.get("/textplain";"hey")

$router.get("/router.http";Formula:C1597($2.status(200).attachment("router.http").send($router.restClientHTTP())))
$router.get("/router.http";Formula:C1597($2.status(200).attachment("router.http").send($router.restClientHTTP($1))))

/*$childRooter:=tricho .router()
$childRooter.get("/hello";"Hello child")
Expand Down

0 comments on commit 86bcac3

Please sign in to comment.