|
| 1 | +package lark |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "net/http/httputil" |
| 11 | +) |
| 12 | + |
| 13 | +// ExpandURL expands url path to full url |
| 14 | +func (bot Bot) ExpandURL(urlPath string) string { |
| 15 | + url := fmt.Sprintf("%s%s", bot.domain, urlPath) |
| 16 | + return url |
| 17 | +} |
| 18 | + |
| 19 | +func (bot Bot) httpErrorLog(ctx context.Context, prefix, text string, err error) { |
| 20 | + bot.logger.Log(ctx, LogLevelError, fmt.Sprintf("[%s] %s: %+v\n", prefix, text, err)) |
| 21 | +} |
| 22 | + |
| 23 | +// PerformAPIRequest performs API request |
| 24 | +func (bot Bot) PerformAPIRequest( |
| 25 | + ctx context.Context, |
| 26 | + method string, |
| 27 | + prefix, urlPath string, |
| 28 | + header http.Header, auth bool, |
| 29 | + body io.Reader, |
| 30 | + output interface{}, |
| 31 | +) error { |
| 32 | + var ( |
| 33 | + err error |
| 34 | + respBody io.ReadCloser |
| 35 | + url = bot.ExpandURL(urlPath) |
| 36 | + ) |
| 37 | + if header == nil { |
| 38 | + header = make(http.Header) |
| 39 | + } |
| 40 | + if auth { |
| 41 | + header.Add("Authorization", fmt.Sprintf("Bearer %s", bot.TenantAccessToken())) |
| 42 | + } |
| 43 | + if bot.useCustomClient { |
| 44 | + if bot.customClient == nil { |
| 45 | + return ErrCustomHTTPClientNotSet |
| 46 | + } |
| 47 | + respBody, err = bot.customClient.Do(ctx, method, url, header, body) |
| 48 | + if err != nil { |
| 49 | + bot.httpErrorLog(ctx, prefix, "call failed", err) |
| 50 | + return err |
| 51 | + } |
| 52 | + } else { |
| 53 | + req, err := http.NewRequestWithContext(ctx, method, url, body) |
| 54 | + if err != nil { |
| 55 | + bot.httpErrorLog(ctx, prefix, "init request failed", err) |
| 56 | + return err |
| 57 | + } |
| 58 | + req.Header = header |
| 59 | + resp, err := bot.client.Do(req) |
| 60 | + if err != nil { |
| 61 | + bot.httpErrorLog(ctx, prefix, "call failed", err) |
| 62 | + return err |
| 63 | + } |
| 64 | + if bot.debug { |
| 65 | + b, _ := httputil.DumpResponse(resp, true) |
| 66 | + bot.logger.Log(ctx, LogLevelDebug, string(b)) |
| 67 | + } |
| 68 | + respBody = resp.Body |
| 69 | + } |
| 70 | + defer respBody.Close() |
| 71 | + err = json.NewDecoder(respBody).Decode(&output) |
| 72 | + if err != nil { |
| 73 | + bot.httpErrorLog(ctx, prefix, "decode body failed", err) |
| 74 | + return err |
| 75 | + } |
| 76 | + return err |
| 77 | +} |
| 78 | + |
| 79 | +func (bot Bot) wrapAPIRequest(ctx context.Context, method, prefix, urlPath string, auth bool, params interface{}, output interface{}) error { |
| 80 | + buf := new(bytes.Buffer) |
| 81 | + err := json.NewEncoder(buf).Encode(params) |
| 82 | + if err != nil { |
| 83 | + bot.httpErrorLog(ctx, prefix, "encode JSON failed", err) |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + header := make(http.Header) |
| 88 | + header.Set("Content-Type", "application/json; charset=utf-8") |
| 89 | + err = bot.PerformAPIRequest(ctx, method, prefix, urlPath, header, auth, buf, output) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +// PostAPIRequest call Lark API |
| 97 | +func (bot Bot) PostAPIRequest(ctx context.Context, prefix, urlPath string, auth bool, params interface{}, output interface{}) error { |
| 98 | + return bot.wrapAPIRequest(ctx, http.MethodPost, prefix, urlPath, auth, params, output) |
| 99 | +} |
| 100 | + |
| 101 | +// GetAPIRequest call Lark API |
| 102 | +func (bot Bot) GetAPIRequest(ctx context.Context, prefix, urlPath string, auth bool, params interface{}, output interface{}) error { |
| 103 | + return bot.wrapAPIRequest(ctx, http.MethodGet, prefix, urlPath, auth, params, output) |
| 104 | +} |
| 105 | + |
| 106 | +// DeleteAPIRequest call Lark API |
| 107 | +func (bot Bot) DeleteAPIRequest(ctx context.Context, prefix, urlPath string, auth bool, params interface{}, output interface{}) error { |
| 108 | + return bot.wrapAPIRequest(ctx, http.MethodDelete, prefix, urlPath, auth, params, output) |
| 109 | +} |
| 110 | + |
| 111 | +// PutAPIRequest call Lark API |
| 112 | +func (bot Bot) PutAPIRequest(ctx context.Context, prefix, urlPath string, auth bool, params interface{}, output interface{}) error { |
| 113 | + return bot.wrapAPIRequest(ctx, http.MethodPut, prefix, urlPath, auth, params, output) |
| 114 | +} |
| 115 | + |
| 116 | +// PatchAPIRequest call Lark API |
| 117 | +func (bot Bot) PatchAPIRequest(ctx context.Context, prefix, urlPath string, auth bool, params interface{}, output interface{}) error { |
| 118 | + return bot.wrapAPIRequest(ctx, http.MethodPatch, prefix, urlPath, auth, params, output) |
| 119 | +} |
0 commit comments