From ccee022afd89673f8a24969dac3f25ab4ee170a3 Mon Sep 17 00:00:00 2001 From: Ceelog Date: Mon, 11 Jan 2021 14:32:13 +0800 Subject: [PATCH] fix: material.Get --- .../apis/material/example_material_test.go | 18 ----- corporation/apis/material/material.go | 31 +++++++- corporation/apis/material/material_test.go | 74 ------------------- corporation/cmd/build.go | 13 ++++ corporation/cmd/tpl/material.Get.tpl | 16 ++++ corporation/cmd/tpl/material.Jssdk.tpl | 14 ++++ 6 files changed, 70 insertions(+), 96 deletions(-) create mode 100644 corporation/cmd/tpl/material.Get.tpl create mode 100644 corporation/cmd/tpl/material.Jssdk.tpl diff --git a/corporation/apis/material/example_material_test.go b/corporation/apis/material/example_material_test.go index c5df8b8..73a5558 100644 --- a/corporation/apis/material/example_material_test.go +++ b/corporation/apis/material/example_material_test.go @@ -40,21 +40,3 @@ func ExampleUploadImg() { fmt.Println(resp, err) } - -func ExampleGet() { - var ctx *corporation.App - - params := url.Values{} - resp, err := material.Get(ctx, params) - - fmt.Println(resp, err) -} - -func ExampleJssdk() { - var ctx *corporation.App - - params := url.Values{} - resp, err := material.Jssdk(ctx, params) - - fmt.Println(resp, err) -} diff --git a/corporation/apis/material/material.go b/corporation/apis/material/material.go index 2141e67..31c73eb 100644 --- a/corporation/apis/material/material.go +++ b/corporation/apis/material/material.go @@ -18,6 +18,7 @@ package material import ( "io" "mime/multipart" + "net/http" "net/url" "os" "path" @@ -107,8 +108,20 @@ See: https://work.weixin.qq.com/api/doc/90000/90135/90254 GET https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID */ -func Get(ctx *corporation.App, params url.Values) (resp []byte, err error) { - return ctx.Client.HTTPGet(apiGet + "?" + params.Encode()) +func Get(ctx *corporation.App, params url.Values, header http.Header) (resp *http.Response, err error) { + accessToken, err := ctx.AccessToken.GetAccessTokenHandler(ctx) + if err != nil { + return + } + + req, err := http.NewRequest(http.MethodGet, corporation.WXServerUrl+apiGet+"?access_token="+accessToken+"&"+params.Encode(), nil) + if err != nil { + return + } + + req.Header = header + + return http.DefaultClient.Do(req) } /* @@ -120,6 +133,16 @@ See: https://work.weixin.qq.com/api/doc/90000/90135/90255 GET https://qyapi.weixin.qq.com/cgi-bin/media/get/jssdk?access_token=ACCESS_TOKEN&media_id=MEDIA_ID */ -func Jssdk(ctx *corporation.App, params url.Values) (resp []byte, err error) { - return ctx.Client.HTTPGet(apiJssdk + "?" + params.Encode()) +func Jssdk(ctx *corporation.App, params url.Values) (resp *http.Response, err error) { + accessToken, err := ctx.AccessToken.GetAccessTokenHandler(ctx) + if err != nil { + return + } + + req, err := http.NewRequest(http.MethodGet, corporation.WXServerUrl+apiJssdk+"?access_token="+accessToken+"&"+params.Encode(), nil) + if err != nil { + return + } + + return http.DefaultClient.Do(req) } diff --git a/corporation/apis/material/material_test.go b/corporation/apis/material/material_test.go index 9c4ea39..2b786e3 100644 --- a/corporation/apis/material/material_test.go +++ b/corporation/apis/material/material_test.go @@ -103,77 +103,3 @@ func TestUploadImg(t *testing.T) { }) } } -func TestGet(t *testing.T) { - mockResp := map[string][]byte{ - "case1": []byte("{\"errcode\":0,\"errmsg\":\"ok\"}"), - } - var resp []byte - test.MockSvrHandler.HandleFunc(apiGet, func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte(resp)) - }) - - type args struct { - ctx *corporation.App - - params url.Values - } - tests := []struct { - name string - args args - wantResp []byte - wantErr bool - }{ - {name: "case1", args: args{ctx: test.MockApp}, wantResp: mockResp["case1"], wantErr: false}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - resp = mockResp[tt.name] - gotResp, err := Get(tt.args.ctx, tt.args.params) - //fmt.Println(string(gotResp), err) - if (err != nil) != tt.wantErr { - t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(gotResp, tt.wantResp) { - t.Errorf("Get() gotResp = %v, want %v", gotResp, tt.wantResp) - } - }) - } -} -func TestJssdk(t *testing.T) { - mockResp := map[string][]byte{ - "case1": []byte("{\"errcode\":0,\"errmsg\":\"ok\"}"), - } - var resp []byte - test.MockSvrHandler.HandleFunc(apiJssdk, func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte(resp)) - }) - - type args struct { - ctx *corporation.App - - params url.Values - } - tests := []struct { - name string - args args - wantResp []byte - wantErr bool - }{ - {name: "case1", args: args{ctx: test.MockApp}, wantResp: mockResp["case1"], wantErr: false}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - resp = mockResp[tt.name] - gotResp, err := Jssdk(tt.args.ctx, tt.args.params) - //fmt.Println(string(gotResp), err) - if (err != nil) != tt.wantErr { - t.Errorf("Jssdk() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(gotResp, tt.wantResp) { - t.Errorf("Jssdk() gotResp = %v, want %v", gotResp, tt.wantResp) - } - }) - } -} diff --git a/corporation/cmd/build.go b/corporation/cmd/build.go index 4cf5208..ac7fef3 100644 --- a/corporation/cmd/build.go +++ b/corporation/cmd/build.go @@ -140,6 +140,15 @@ func build(group ApiGroup) { _FUNC_NAME_ = api.FuncName } + isTplApi := false + if (group.Package == "material" && api.FuncName == "Get") || group.Package == "material" && api.FuncName == "Jssdk" { + file, err := ioutil.ReadFile("tpl/" + group.Package + "." + api.FuncName + ".tpl") + if err != nil { + continue + } + tpl = commentTpl + string(file) + isTplApi = true + } tpl = strings.ReplaceAll(tpl, "_TITLE_", api.Name) tpl = strings.ReplaceAll(tpl, "_DESCRIPTION_", api.Description) tpl = strings.ReplaceAll(tpl, "_REQUEST_", api.Request) @@ -161,6 +170,10 @@ func build(group ApiGroup) { consts = append(consts, tpl) + if isTplApi { // 不用 test case + continue + } + // TestFunc _TEST_ARGS_STRUCT_ := "" switch { diff --git a/corporation/cmd/tpl/material.Get.tpl b/corporation/cmd/tpl/material.Get.tpl new file mode 100644 index 0000000..f4ca5f9 --- /dev/null +++ b/corporation/cmd/tpl/material.Get.tpl @@ -0,0 +1,16 @@ + +func Get(ctx *corporation.App, params url.Values, header http.Header) (resp *http.Response, err error) { + accessToken, err := ctx.AccessToken.GetAccessTokenHandler(ctx) + if err != nil { + return + } + + req, err := http.NewRequest(http.MethodGet, corporation.WXServerUrl+apiGet+"?access_token="+accessToken+"&"+params.Encode(), nil) + if err != nil { + return + } + + req.Header = header + + return http.DefaultClient.Do(req) +} diff --git a/corporation/cmd/tpl/material.Jssdk.tpl b/corporation/cmd/tpl/material.Jssdk.tpl new file mode 100644 index 0000000..711cee4 --- /dev/null +++ b/corporation/cmd/tpl/material.Jssdk.tpl @@ -0,0 +1,14 @@ + +func Jssdk(ctx *corporation.App, params url.Values) (resp *http.Response, err error) { + accessToken, err := ctx.AccessToken.GetAccessTokenHandler(ctx) + if err != nil { + return + } + + req, err := http.NewRequest(http.MethodGet, corporation.WXServerUrl+apiJssdk+"?access_token="+accessToken+"&"+params.Encode(), nil) + if err != nil { + return + } + + return http.DefaultClient.Do(req) +} \ No newline at end of file