Skip to content

Commit

Permalink
fix: material.Get
Browse files Browse the repository at this point in the history
  • Loading branch information
Ceelog committed Jan 11, 2021
1 parent c016d14 commit ccee022
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 96 deletions.
18 changes: 0 additions & 18 deletions corporation/apis/material/example_material_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
31 changes: 27 additions & 4 deletions corporation/apis/material/material.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package material
import (
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path"
Expand Down Expand Up @@ -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)
}

/*
Expand All @@ -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)
}
74 changes: 0 additions & 74 deletions corporation/apis/material/material_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
13 changes: 13 additions & 0 deletions corporation/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -161,6 +170,10 @@ func build(group ApiGroup) {

consts = append(consts, tpl)

if isTplApi { // 不用 test case
continue
}

// TestFunc
_TEST_ARGS_STRUCT_ := ""
switch {
Expand Down
16 changes: 16 additions & 0 deletions corporation/cmd/tpl/material.Get.tpl
Original file line number Diff line number Diff line change
@@ -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)
}
14 changes: 14 additions & 0 deletions corporation/cmd/tpl/material.Jssdk.tpl
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit ccee022

Please sign in to comment.