Skip to content

Commit

Permalink
Merge pull request #18 from larksuite/new_service
Browse files Browse the repository at this point in the history
context GetHeader
add ai service
add im service
  • Loading branch information
Mingqiang Zhao authored Apr 12, 2021
2 parents 6ac53bd + a898b8f commit b618c5d
Show file tree
Hide file tree
Showing 35 changed files with 3,871 additions and 927 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ modules.txt
*_test.go
*.json
*.sh
test.go
19 changes: 11 additions & 8 deletions README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -518,14 +518,17 @@ readCloser, err := tools.DownloadFileToStream(ctx context.Context, url string)

---

|业务域|版本|路径|代码示例|
|---|---|---|----|
|[用户身份验证](https://open.feishu.cn/document/ukTMukTMukTM/uETOwYjLxkDM24SM5AjN)|v1|[service/authen](service/authen)|[sample/api/authen.go](sample/api/authen.go)|
|[图片](https://open.feishu.cn/document/ukTMukTMukTM/uEDO04SM4QjLxgDN)|v4|[service/image](service/image)|[sample/api/image.go](sample/api/image.go)|
|[通讯录](https://open.feishu.cn/document/ukTMukTMukTM/uETNz4SM1MjLxUzM/v3/introduction)|v3|[service/contact](service/contact)|[sample/api/contact.go](sample/api/contact.go)|
|[日历](https://open.feishu.cn/document/ukTMukTMukTM/uETM3YjLxEzN24SMxcjN)|v4|[service/calendar](service/calendar)|[sample/api/calendar.go](sample/api/calendar.go)|
|[视频会议](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/videoconference/guide)|v1|[service/vc](service/vc)|[sample/api/vc.go](sample/api/vc.go)|
|[云空间文件](https://open.feishu.cn/document/ukTMukTMukTM/uUjM5YjL1ITO24SNykjN)|v1|[service/drive](service/drive)|[sample/api/drive.go](sample/api/drive.go)|
|业务域|版本|路径|API示例|Event示例|
|---|---|---|----|---|
|[用户身份验证](https://open.feishu.cn/document/ukTMukTMukTM/uETOwYjLxkDM24SM5AjN)|v1|[service/authen](service/authen)|[sample/api/authen.go](sample/api/authen.go)||
|[通讯录](https://open.feishu.cn/document/ukTMukTMukTM/uETNz4SM1MjLxUzM/v3/introduction)|v3|[service/contact](service/contact)|[sample/api/contact.go](sample/api/contact.go)|[sample/event/contact.go](sample/event/contact.go)|
|[日历](https://open.feishu.cn/document/ukTMukTMukTM/uETM3YjLxEzN24SMxcjN)|v4|[service/calendar](service/calendar)|[sample/api/calendar.go](sample/api/calendar.go)||
|[视频会议](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/videoconference/guide)|v1|[service/vc](service/vc)|[sample/api/vc.go](sample/api/vc.go)||
|[云空间文件](https://open.feishu.cn/document/ukTMukTMukTM/uUjM5YjL1ITO24SNykjN)|v1|[service/drive](service/drive)|[sample/api/drive.go](sample/api/drive.go)||
|[消息&群组](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/create)|v1|[service/im](service/im)|[sample/api/im.go](sample/api/im.go)|[sample/event/im.go](sample/event/im.go)|
|[AI能力-光学字符识别(OCR)](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/ai/optical_char_recognition-v1/image/basic_recognize)|v1|[service/optical_char_recognition](service/optical_char_recognition)|[sample/api/optical_char_recognition.go](sample/api/optical_char_recognition.go)||
|[AI能力-语音识别(ASR)](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/ai/speech_to_text-v1/speech/stream_recognize)|v1|[service/speech_to_text](service/speech_to_text)|[sample/api/speech_to_text.go](sample/api/speech_to_text.go)||
|[AI能力-机器翻译(MT)](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/ai/translation-v1/text/translate)|v1|[service/translation](service/translation)|[sample/api/translation.go](sample/api/translation.go)||

## License

Expand Down
3 changes: 2 additions & 1 deletion api/core/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/larksuite/oapi-sdk-go/core"
"github.com/larksuite/oapi-sdk-go/core/config"
coreconst "github.com/larksuite/oapi-sdk-go/core/constants"
"github.com/larksuite/oapi-sdk-go/core/model"
"io"
"io/ioutil"
"mime/multipart"
Expand Down Expand Up @@ -89,7 +90,7 @@ func (hs *Handlers) send(ctx *core.Context, req *request.Request) {
req.Err = err
return
}
ctx.SetRequestID(resp.Header.Get(coreconst.HTTPHeaderKeyLogID), resp.Header.Get(coreconst.HTTPHeaderKeyRequestID))
ctx.Set(coreconst.HTTPHeader, model.NewOapiHeader(resp.Header))
ctx.Set(coreconst.HTTPKeyStatusCode, resp.StatusCode)
req.HTTPResponse = resp
defer hs.retry(ctx, req)
Expand Down
6 changes: 1 addition & 5 deletions card/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,14 @@ type Handlers struct {

func initFunc(ctx *core.Context, httpCard *model.HTTPCard) {
request := httpCard.Request
ctx.Set(constants.HTTPHeader, request.Header)
header := &model.Header{
Timestamp: request.Header.GetFirstValue(model.LarkRequestTimestamp),
Nonce: request.Header.GetFirstValue(model.LarkRequestRequestNonce),
Signature: request.Header.GetFirstValue(model.LarkSignature),
RefreshToken: request.Header.GetFirstValue(model.LarkRefreshToken),
}
httpCard.Header = header
ctx.Set(model.LarkRequestTimestamp, header.Timestamp)
ctx.Set(model.LarkRequestRequestNonce, header.Nonce)
ctx.Set(model.LarkSignature, header.Signature)
ctx.Set(model.LarkRefreshToken, header.RefreshToken)
ctx.SetRequestID(request.Header.GetFirstValue(constants.HTTPHeaderKeyLogID), request.Header.GetFirstValue(constants.HTTPHeaderKeyRequestID))
config.ByCtx(ctx).GetLogger().Debug(ctx, fmt.Sprintf("[init] card: %s", request.Body))
httpCard.Input = []byte(request.Body)
}
Expand Down
1 change: 1 addition & 0 deletions core/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const ContentTypeJson = "application/json"
const DefaultContentType = ContentTypeJson + "; charset=utf-8"

const (
HTTPHeader = "HTTP-Header"
HTTPHeaderKeyRequestID = "X-Request-Id"
HTTPHeaderKeyLogID = "X-Log-Id"
HTTPKeyStatusCode = "http_status_code"
Expand Down
16 changes: 13 additions & 3 deletions core/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"context"
"github.com/larksuite/oapi-sdk-go/core/constants"
"github.com/larksuite/oapi-sdk-go/core/model"
"sync"
"time"
)
Expand Down Expand Up @@ -52,11 +53,20 @@ func (c *Context) Get(key string) (value interface{}, exists bool) {
return
}

func (c *Context) GetHeader() *model.OapiHeader {
if header, ok := c.Get(constants.HTTPHeader); ok {
return header.(*model.OapiHeader)
}
return model.NewOapiHeader(map[string][]string{})
}

func (c *Context) GetRequestID() string {
if requestID, ok := c.Get(constants.HTTPHeaderKeyRequestID); ok {
return requestID.(string)
header := c.GetHeader()
logID := header.GetFirstValue(constants.HTTPHeaderKeyLogID)
if logID != "" {
return logID
}
return ""
return header.GetFirstValue(constants.HTTPHeaderKeyRequestID)
}

func (c *Context) GetHTTPStatusCode() int {
Expand Down
2 changes: 1 addition & 1 deletion event/core/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Handle(ctx *core.Context, httpEvent *model.HTTPEvent) {

func unmarshalFunc(ctx *core.Context, httpEvent *model.HTTPEvent) {
request := httpEvent.Request
ctx.SetRequestID(request.Header.GetFirstValue(constants.HTTPHeaderKeyLogID), request.Header.GetFirstValue(constants.HTTPHeaderKeyRequestID))
ctx.Set(constants.HTTPHeader, request.Header)
conf := config.ByCtx(ctx)
conf.GetLogger().Debug(ctx, fmt.Sprintf("[unmarshal] event: %s", request.Body))
body := []byte(request.Body)
Expand Down
6 changes: 3 additions & 3 deletions sample/api/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ import (
var contactService = contact.NewService(configs.TestConfig(constants.DomainFeiShu))

func main() {
//testUserServiceList()
testDepartmentServiceList()
testUserServiceList()
//testDepartmentServiceList()
}
func testUserServiceList() {
coreCtx := core.WrapContext(context.Background())
reqCall := contactService.Users.List(coreCtx)
reqCall.SetDepartmentIdType("open_id")
reqCall.SetPageSize(20)
reqCall.SetDepartmentIdType("open_department_id")
reqCall.SetDepartmentId("od_XXXXXXXXX")
reqCall.SetDepartmentId("0")
reqCall.SetUserIdType("open_id")
result, err := reqCall.Do()
fmt.Printf("request_id:%s", coreCtx.GetRequestID())
Expand Down
98 changes: 98 additions & 0 deletions sample/api/im.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"bytes"
"context"
"fmt"
"github.com/larksuite/oapi-sdk-go/api/core/request"
"github.com/larksuite/oapi-sdk-go/api/core/response"
"github.com/larksuite/oapi-sdk-go/core"
"github.com/larksuite/oapi-sdk-go/core/constants"
"github.com/larksuite/oapi-sdk-go/core/tools"
"github.com/larksuite/oapi-sdk-go/sample/configs"
im "github.com/larksuite/oapi-sdk-go/service/im/v1"
"io/ioutil"
"os"
)

// for redis store and logrus
// configs.TestConfigWithLogrusAndRedisStore(constants.DomainFeiShu)
// configs.TestConfig("https://open.feishu.cn")
var imService = im.NewService(configs.TestConfig(constants.DomainFeiShu))

func main() {
//testMessageCreate()
//testFileCreate()
testFileRead()
}

func testMessageCreate() {
coreCtx := core.WrapContext(context.Background())
reqCall := imService.Messages.Create(coreCtx, &im.MessageCreateReqBody{
ReceiveId: "ou_a11d2bcc7d852afbcaf37e5b3ad01f7e",
Content: "{\"text\":\"<at user_id=\\\"ou_a11d2bcc7d852afbcaf37e5b3ad01f7e\\\">Tom</at> test content\"}",
MsgType: "text",
})
reqCall.SetReceiveIdType("open_id")
message, err := reqCall.Do()
fmt.Println(coreCtx.GetRequestID())
fmt.Println(coreCtx.GetHTTPStatusCode())
if err != nil {
fmt.Println(tools.Prettify(err))
e := err.(*response.Error)
fmt.Println(e.Code)
fmt.Println(e.Msg)
return
}
fmt.Println(tools.Prettify(message))
}

func testFileRead() {
coreCtx := core.WrapContext(context.Background())
reqCall := imService.Files.Get(coreCtx)
buf := &bytes.Buffer{}
reqCall.SetResponseStream(buf)
reqCall.SetFileKey("file_ec24f8ad-89ea-4bb5-a7e4-c5db35d2925g")
_, err := reqCall.Do()
fmt.Println(coreCtx.GetRequestID())
fmt.Println(coreCtx.GetHTTPStatusCode())
fmt.Println(coreCtx.GetHeader())
if err != nil {
fmt.Println(tools.Prettify(err))
e := err.(*response.Error)
fmt.Println(e.Code)
fmt.Println(e.Msg)
return
}
err = ioutil.WriteFile("test_download.pdf", buf.Bytes(), os.ModePerm)
if err != nil {
fmt.Println(err)
return
}
}

func testFileCreate() {
coreCtx := core.WrapContext(context.Background())
reqCall := imService.Files.Create(coreCtx)
f, err := os.Open("test.pdf")
if err != nil {
fmt.Println(err)
return
}
file := request.NewFile().SetContentStream(f)
// request.NewFile().SetContent([]byte)
reqCall.SetFile(file)
reqCall.SetFileName("test-测试.pdf")
reqCall.SetFileType("pdf")
message, err := reqCall.Do()
fmt.Println(coreCtx.GetRequestID())
fmt.Println(coreCtx.GetHTTPStatusCode())
if err != nil {
fmt.Println(tools.Prettify(err))
e := err.(*response.Error)
fmt.Println(e.Code)
fmt.Println(e.Msg)
return
}
fmt.Println(tools.Prettify(message))
}
37 changes: 37 additions & 0 deletions sample/api/optical_char_recognition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"context"
"fmt"
"github.com/larksuite/oapi-sdk-go/api/core/response"
"github.com/larksuite/oapi-sdk-go/core"
"github.com/larksuite/oapi-sdk-go/core/constants"
"github.com/larksuite/oapi-sdk-go/core/tools"
"github.com/larksuite/oapi-sdk-go/sample/configs"
optical_char_recognition "github.com/larksuite/oapi-sdk-go/service/optical_char_recognition/v1"
)

// for redis store and logrus
// configs.TestConfigWithLogrusAndRedisStore(constants.DomainFeiShu)
// configs.TestConfig("https://open.feishu.cn")
var opticalCharRecognitionService = optical_char_recognition.NewService(configs.TestConfig(constants.DomainFeiShu))

func main() {
testImageBasicRecognize()
}

func testImageBasicRecognize() {
coreCtx := core.WrapContext(context.Background())
reqCall := opticalCharRecognitionService.Images.BasicRecognize(coreCtx, &optical_char_recognition.ImageBasicRecognizeReqBody{
Image: "base64 image",
})
result, err := reqCall.Do()
fmt.Printf("request_id:%s\n", coreCtx.GetRequestID())
fmt.Printf("http status code:%d", coreCtx.GetHTTPStatusCode())
if err != nil {
e := err.(*response.Error)
fmt.Printf(tools.Prettify(e))
return
}
fmt.Printf("reault:%s", tools.Prettify(result))
}
45 changes: 45 additions & 0 deletions sample/api/speech_to_text.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"context"
"fmt"
"github.com/larksuite/oapi-sdk-go/api/core/response"
"github.com/larksuite/oapi-sdk-go/core"
"github.com/larksuite/oapi-sdk-go/core/constants"
"github.com/larksuite/oapi-sdk-go/core/tools"
"github.com/larksuite/oapi-sdk-go/sample/configs"
speech_to_text "github.com/larksuite/oapi-sdk-go/service/speech_to_text/v1"
)

// for redis store and logrus
// configs.TestConfigWithLogrusAndRedisStore(constants.DomainFeiShu)
// configs.TestConfig("https://open.feishu.cn")
var speechToTextService = speech_to_text.NewService(configs.TestConfig(constants.DomainFeiShu))

func main() {
testSpeechFileRecognize()
}

func testSpeechFileRecognize() {
coreCtx := core.WrapContext(context.Background())
reqCall := speechToTextService.Speechs.FileRecognize(coreCtx, &speech_to_text.SpeechFileRecognizeReqBody{
Speech: &speech_to_text.Speech{
Speech: "base64 后的音频内容",
},
Config: &speech_to_text.FileConfig{
FileId: "qwe12dd34567890w",
Format: "pcm",
EngineType: "16k_auto",
},
})
result, err := reqCall.Do()
fmt.Printf("header:%s\n", coreCtx.GetHeader())
fmt.Printf("request_id:%s\n", coreCtx.GetRequestID())
fmt.Printf("http status code:%d", coreCtx.GetHTTPStatusCode())
if err != nil {
e := err.(*response.Error)
fmt.Printf(tools.Prettify(e))
return
}
fmt.Printf("reault:%s", tools.Prettify(result))
}
45 changes: 45 additions & 0 deletions sample/api/translation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"context"
"fmt"
"github.com/larksuite/oapi-sdk-go/api/core/response"
"github.com/larksuite/oapi-sdk-go/core"
"github.com/larksuite/oapi-sdk-go/core/constants"
"github.com/larksuite/oapi-sdk-go/core/tools"
"github.com/larksuite/oapi-sdk-go/sample/configs"
translation "github.com/larksuite/oapi-sdk-go/service/translation/v1"
)

// for redis store and logrus
// configs.TestConfigWithLogrusAndRedisStore(constants.DomainFeiShu)
// configs.TestConfig("https://open.feishu.cn")
var translationService = translation.NewService(configs.TestConfig(constants.DomainFeiShu))

func main() {
testTextDetect()
}

func testTextDetect() {
coreCtx := core.WrapContext(context.Background())
reqCall := translationService.Texts.Translate(coreCtx, &translation.TextTranslateReqBody{
SourceLanguage: "zh",
Text: "测试",
TargetLanguage: "en",
Glossary: []*translation.Term{
{
From: "测",
To: "test",
},
},
})
result, err := reqCall.Do()
fmt.Printf("request_id:%s\n", coreCtx.GetRequestID())
fmt.Printf("http status code:%d", coreCtx.GetHTTPStatusCode())
if err != nil {
e := err.(*response.Error)
fmt.Printf(tools.Prettify(e))
return
}
fmt.Printf("reault:%s", tools.Prettify(result))
}
10 changes: 4 additions & 6 deletions sample/event/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package main

import (
"fmt"
"github.com/larksuite/oapi-sdk-go/core/constants"
"github.com/larksuite/oapi-sdk-go/sample/configs"
"path"

"github.com/gin-gonic/gin"
"github.com/larksuite/oapi-sdk-go/core"
"github.com/larksuite/oapi-sdk-go/core/constants"
"github.com/larksuite/oapi-sdk-go/core/tools"
eventginserver "github.com/larksuite/oapi-sdk-go/event/http/gin"
"github.com/larksuite/oapi-sdk-go/sample/configs"
contact "github.com/larksuite/oapi-sdk-go/service/contact/v3"
)

Expand All @@ -32,14 +30,14 @@ func main() {
return nil
})

contact.SetDepartmentDeletedEventHandler(conf, func(ctx *core.Context, event *contact.DepartmentDeletedEvent) error {
contact.SetUserUpdatedEventHandler(conf, func(ctx *core.Context, event *contact.UserUpdatedEvent) error {
fmt.Println(ctx.GetRequestID())
fmt.Println(tools.Prettify(event))
return nil
})

g := gin.Default()
eventginserver.Register(path.Join("/", conf.GetAppSettings().AppID, "webhook/event"), conf, g)
eventginserver.Register("/webhook/event", conf, g)
err := g.Run(":8089")
if err != nil {
fmt.Println(err)
Expand Down
Loading

0 comments on commit b618c5d

Please sign in to comment.