Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1/n] Version 2 breaking changes #133

Merged
merged 8 commits into from
Dec 19, 2024
405 changes: 81 additions & 324 deletions apis.md.go

Large diffs are not rendered by default.

39 changes: 32 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *Workwx) WithApp(corpSecret string, agentID int64) *WorkwxApp {
return &app
}

func (c *WorkwxApp) composeQyapiURL(path string, req interface{}) (*url.URL, error) {
func (c *WorkwxApp) composeQyapiURL(path string, req any) (*url.URL, error) {
values := url.Values{}
if valuer, ok := req.(urlValuer); ok {
values = valuer.intoURLValues()
Expand All @@ -81,7 +81,7 @@ func (c *WorkwxApp) composeQyapiURL(path string, req interface{}) (*url.URL, err
return base, nil
}

func (c *WorkwxApp) composeQyapiURLWithToken(path string, req interface{}, withAccessToken bool) (*url.URL, error) {
func (c *WorkwxApp) composeQyapiURLWithToken(path string, req any, withAccessToken bool) (*url.URL, error) {
url, err := c.composeQyapiURL(path, req)
if err != nil {
return nil, err
Expand All @@ -103,7 +103,13 @@ func (c *WorkwxApp) composeQyapiURLWithToken(path string, req interface{}, withA
return url, nil
}

func (c *WorkwxApp) executeQyapiGet(path string, req urlValuer, respObj interface{}, withAccessToken bool) error {
func executeQyapiGet[T urlValuer, U tryIntoErr](
c *WorkwxApp,
path string,
req T,
respObj U,
withAccessToken bool,
) error {
url, err := c.composeQyapiURLWithToken(path, req, withAccessToken)
if err != nil {
return err
Expand All @@ -122,10 +128,20 @@ func (c *WorkwxApp) executeQyapiGet(path string, req urlValuer, respObj interfac
return makeRespUnmarshalErr(err)
}

if bizErr := respObj.TryIntoErr(); bizErr != nil {
return bizErr
}

return nil
}

func (c *WorkwxApp) executeQyapiJSONPost(path string, req bodyer, respObj interface{}, withAccessToken bool) error {
func executeQyapiJSONPost[T bodyer, U tryIntoErr](
c *WorkwxApp,
path string,
req T,
respObj U,
withAccessToken bool,
) error {
url, err := c.composeQyapiURLWithToken(path, req, withAccessToken)
if err != nil {
return err
Expand All @@ -149,13 +165,18 @@ func (c *WorkwxApp) executeQyapiJSONPost(path string, req bodyer, respObj interf
return makeRespUnmarshalErr(err)
}

if bizErr := respObj.TryIntoErr(); bizErr != nil {
return bizErr
}

return nil
}

func (c *WorkwxApp) executeQyapiMediaUpload(
func executeQyapiMediaUpload[T mediaUploader, U tryIntoErr](
c *WorkwxApp,
path string,
req mediaUploader,
respObj interface{},
req T,
respObj U,
withAccessToken bool,
) error {
url, err := c.composeQyapiURLWithToken(path, req, withAccessToken)
Expand Down Expand Up @@ -192,5 +213,9 @@ func (c *WorkwxApp) executeQyapiMediaUpload(
return makeRespUnmarshalErr(err)
}

if bizErr := respObj.TryIntoErr(); bizErr != nil {
return bizErr
}

return nil
}
Loading
Loading