@@ -6,9 +6,13 @@ import (
66 bytes "bytes"
77 context "context"
88 json "encoding/json"
9+ errors "errors"
910 generated "github.com/devimteam/microgen/example/generated"
11+ mux "github.com/gorilla/mux"
1012 ioutil "io/ioutil"
1113 http "net/http"
14+ path "path"
15+ strconv "strconv"
1216)
1317
1418func CommonHTTPRequestEncoder (_ context.Context , r * http.Request , request interface {}) error {
@@ -32,9 +36,28 @@ func DecodeHTTPUppercaseRequest(_ context.Context, r *http.Request) (interface{}
3236}
3337
3438func DecodeHTTPCountRequest (_ context.Context , r * http.Request ) (interface {}, error ) {
35- var req generated.CountRequest
36- err := json .NewDecoder (r .Body ).Decode (& req )
37- return & req , err
39+ var (
40+ _param string
41+ )
42+ var ok bool
43+ _vars := mux .Vars (r )
44+ _param , ok = _vars ["text" ]
45+ if ! ok {
46+ return nil , errors .New ("param text not found" )
47+ }
48+ text := _param
49+ _param , ok = _vars ["symbol" ]
50+ if ! ok {
51+ return nil , errors .New ("param symbol not found" )
52+ }
53+ symbol , err := strconv .ParseInt (_param , 10 , 64 )
54+ if err != nil {
55+ return nil , err
56+ }
57+ return & generated.CountRequest {
58+ Symbol : int (symbol ),
59+ Text : string (text ),
60+ }, nil
3861}
3962
4063func DecodeHTTPTestCaseRequest (_ context.Context , r * http.Request ) (interface {}, error ) {
@@ -62,14 +85,21 @@ func DecodeHTTPTestCaseResponse(_ context.Context, r *http.Response) (interface{
6285}
6386
6487func EncodeHTTPUppercaseRequest (ctx context.Context , r * http.Request , request interface {}) error {
88+ r .URL .Path = path .Join (r .URL .Path , "uppercase" )
6589 return CommonHTTPRequestEncoder (ctx , r , request )
6690}
6791
6892func EncodeHTTPCountRequest (ctx context.Context , r * http.Request , request interface {}) error {
69- return CommonHTTPRequestEncoder (ctx , r , request )
93+ req := request .(* generated.CountRequest )
94+ r .URL .Path = path .Join (r .URL .Path , "count" ,
95+ req .Text ,
96+ strconv .FormatInt (int64 (req .Symbol ), 10 ),
97+ )
98+ return nil
7099}
71100
72101func EncodeHTTPTestCaseRequest (ctx context.Context , r * http.Request , request interface {}) error {
102+ r .URL .Path = path .Join (r .URL .Path , "test-case" )
73103 return CommonHTTPRequestEncoder (ctx , r , request )
74104}
75105
0 commit comments