Added gocli output (untested)
This commit is contained in:
+1
-1
@@ -24,7 +24,7 @@ func Init() API {
|
||||
Perms: make(map[string]string),
|
||||
}
|
||||
|
||||
mux.HandleFunc("", func(w http.ResponseWriter, r *http.Request) {
|
||||
mux.HandleFunc("/someapi", func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case "POST":
|
||||
h_SomeAPI(w, r)
|
||||
|
||||
+2
-2
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
/*@API*/
|
||||
type ARequestStruct struct {
|
||||
A *string
|
||||
A *string `json:"a"`
|
||||
B int64
|
||||
C time.Time
|
||||
D *string
|
||||
@@ -23,7 +23,7 @@ type AResponseStruct struct {
|
||||
|
||||
/*
|
||||
@API
|
||||
PATH: /someapi
|
||||
@PATH: /someapi
|
||||
*/
|
||||
func SomeAPI(ctx context.Context, a *ARequestStruct) (*AResponseStruct, error) {
|
||||
return nil, nil
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package gocli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
var Basepath string = ""
|
||||
var Host string = ""
|
||||
var ExtraHeaders map[string]string = make(map[string]string)
|
||||
|
||||
func invoke(m string, path string, bodyo interface{}) (*json.Decoder, error) {
|
||||
|
||||
b := &bytes.Buffer{}
|
||||
err := json.NewEncoder(b).Encode(bodyo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
body := bytes.NewReader(b.Bytes())
|
||||
|
||||
req, err := http.NewRequest(m, Host+Basepath+path, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-type", "application/json")
|
||||
for k, v := range ExtraHeaders {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
cli := http.Client{}
|
||||
res, err := cli.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ret := json.NewDecoder(res.Body)
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func Some() (*struct{}, error) {
|
||||
dec, err := invoke("a", "/c/d", struct{}{})
|
||||
ret := &struct{}{}
|
||||
dec.Decode(ret)
|
||||
return ret, err
|
||||
|
||||
}
|
||||
|
||||
type ARequestStruct struct {
|
||||
A string `json:"a"`
|
||||
B int64 `json:"b"`
|
||||
C time.Time `json:"c"`
|
||||
D string `json:"d"`
|
||||
}
|
||||
|
||||
type AResponseStruct struct {
|
||||
A string `json:"a"`
|
||||
B int64 `json:"b"`
|
||||
C time.Time `json:"c"`
|
||||
D string `json:"d"`
|
||||
}
|
||||
|
||||
func SomeAPI(req *ARequestStruct) (res *AResponseStruct, err error) {
|
||||
dec, err := invoke("POST", "/someapi", req)
|
||||
ret := &AResponseStruct{}
|
||||
err = dec.Decode(ret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ret, err
|
||||
}
|
||||
+3
-3
@@ -82,10 +82,10 @@ async function InvokeOk(path: string, method: HTMLMethod, body?: any): Promise<b
|
||||
|
||||
//#region Types
|
||||
export interface ARequestStruct {
|
||||
c:Date
|
||||
d:string
|
||||
a:string
|
||||
b:number
|
||||
c:Date
|
||||
d:string
|
||||
}
|
||||
|
||||
export interface AResponseStruct {
|
||||
@@ -101,7 +101,7 @@ export interface AResponseStruct {
|
||||
/**
|
||||
SomeAPI*/
|
||||
export async function SomeAPI(req:ARequestStruct):Promise<AResponseStruct>{
|
||||
return InvokeJSON("","POST",req)
|
||||
return InvokeJSON("/someapi","POST",req)
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
Reference in New Issue
Block a user