-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the Lark auth with some testing file to run the Lark Sheets…
… API
- Loading branch information
Showing
13 changed files
with
760 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
test: | ||
go test -tags=gofreedb_test -v -count=1 ./... | ||
|
||
test-race: | ||
go test -tags=gofreedb_test -v -race -count=1 ./... | ||
|
||
test-race-repeated: | ||
go test -tags=gofreedb_test -v -race -count=10 ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Generating Mocks | ||
|
||
- Use `go generate ./...` to generate all mocks. | ||
- Alternatively, you can just run the `mockgen` per `go:generate` line. | ||
|
||
Note that we add `-build_constraint=gofreedb_test` to the `mockgen` command to all our mock files. | ||
- We cannot use `_test.go` for the mock files because we want the mock files to be as | ||
close as possible to the source code files. | ||
- If we use `_test.go` in the original package, test files in other packages | ||
cannot import the mock files. | ||
- If we use `_mock.go` only, these files will be included in the main build for production use cases, | ||
unnecessarily including the mock files. | ||
- As the mock files are only used for testing purposes, we use the `-build_constraint` flag. This ensures | ||
we don't include these mock files on production build (unless users intentionally include such | ||
build constraints). | ||
- The `go test` script we are using already includes this build tag. | ||
|
||
> If you are using Goland, you may need to add `gofreedb_test` into the `custom build tags` option in Goland. | ||
> Otherwise, you may find the mock functions as "not found". |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package auth | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
const accessTokenURL = "https://open.larksuite.com/open-apis/auth/v3/tenant_access_token/internal" | ||
|
||
type accessTokenResponse struct { | ||
Code int `json:"code"` | ||
Msg string `json:"msg"` | ||
TenantAccessToken string `json:"tenant_access_token"` | ||
} | ||
|
||
type api struct { | ||
client *http.Client | ||
|
||
accessTokenURL string | ||
} | ||
|
||
func (api *api) TenantAccessToken( | ||
ctx context.Context, | ||
appID string, | ||
appSecret string, | ||
) (string, error) { | ||
body := map[string]string{ | ||
"app_id": appID, | ||
"app_secret": appSecret, | ||
} | ||
|
||
bodyJSON, err := json.Marshal(body) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
req, err := http.NewRequestWithContext( | ||
ctx, | ||
http.MethodPost, | ||
api.accessTokenURL, | ||
bytes.NewBuffer(bodyJSON), | ||
) | ||
if err != nil { | ||
return "", err | ||
} | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
resp, err := api.client.Do(req) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return "", fmt.Errorf("failed to get access token: %v", resp.Status) | ||
} | ||
|
||
bodyBytes, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
result := accessTokenResponse{} | ||
if err = json.Unmarshal(bodyBytes, &result); err != nil { | ||
return "", fmt.Errorf("failed to parse access token response: %v", err) | ||
} | ||
if result.Code != 0 { | ||
return "", fmt.Errorf("error fetching token: %v", result.Msg) | ||
} | ||
return result.TenantAccessToken, nil | ||
} | ||
|
||
func newAPI() *api { | ||
return &api{ | ||
client: http.DefaultClient, | ||
accessTokenURL: accessTokenURL, | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.