|
| 1 | +# Agora REST Client for Go |
| 2 | +<p> |
| 3 | +<img alt="GitHub License" src="https://img.shields.io/github/license/AgoraIO-Community/agora-rest-client-go"> |
| 4 | +<a href="https://pkg.go.dev/github.com/AgoraIO-Community/agora-rest-client-go"><img src="https://pkg.go.dev/badge/github.com/AgoraIO-Community/agora-rest-client-go.svg" alt="Go Reference"></a> |
| 5 | +<a href="https://github.com/AgoraIO-Community/agora-rest-client-go/actions/workflows/go.yml"><img src="https://github.com/AgoraIO-Community/agora-rest-client-go/actions/workflows/go.yml/badge.svg" alt="Go Actions"></a> |
| 6 | +<a href="https://github.com/AgoraIO-Community/agora-rest-client-go/actions/workflows/gitee-sync.yml"><img alt="gitee-sync" src="https://github.com/AgoraIO-Community/agora-rest-client-go/actions/workflows/gitee-sync.yml/badge.svg?branch=main"></a> |
| 7 | +<img alt="GitHub go.mod Go version" src="https://img.shields.io/github/go-mod/go-version/AgoraIO-Community/agora-rest-client-go"> |
| 8 | +<img alt="GitHub" src="https://img.shields.io/github/v/release/AgoraIO-Community/agora-rest-client-go"> |
| 9 | +<img alt="GitHub Issues or Pull Requests" src="https://img.shields.io/github/issues-pr/AgoraIO-Community/agora-rest-client-go"> |
| 10 | +</p> |
| 11 | + |
| 12 | + [English](./README.md) | 简体中文 |
| 13 | + |
| 14 | +`agora-rest-client-go`是用Go语言编写的一个开源项目,专门为 Agora REST API设计。它包含了 Agora 官方提供的REST API接口的包装和内部实现,可以帮助开发者更加方便的集成服务端Agora REST API。 |
| 15 | + |
| 16 | +> [!IMPORTANT] |
| 17 | +该SDK经过一些测试以确保其基本功能正常运作。然而,由于软件开发的复杂性,我们无法保证它是完全没有缺陷的,我们鼓励社区的开发者和用户积极参与,共同改进这个项目。 |
| 18 | + |
| 19 | +## 特性 |
| 20 | +* 封装了Agora REST API的请求和响应处理,简化与Agora REST API 的通信流程 |
| 21 | +* 当遇到 DNS 解析失败、网络错误或者请求超时等问题的时候,提供了自动切换最佳域名的能力,以保障请求 REST API 服务的可用性 |
| 22 | +* 提供了易于使用的API,可轻松地实现调用 Agora REST API 的常见功能,如开启云录制、停止云录制等 |
| 23 | +* 基于Go语言,具有高效性、并发性和可扩展性 |
| 24 | + |
| 25 | +## 支持的服务 |
| 26 | +* [云端录制 Cloud Recording](./services/cloudrecording/README.md) |
| 27 | +* [云端转码 Cloud Transcoder](./services/cloudtranscoder/README.md) |
| 28 | +* [对话式 AI 引擎 Conversationsal AI Engine](./services/convoai/README_ZH.md) |
| 29 | + |
| 30 | +## 环境准备 |
| 31 | +* [Go 1.18 或以上版本](https://go.dev/) |
| 32 | +* 在声网 [Console 平台](https://console.shengwang.cn/)申请的 App ID 和 App Certificate |
| 33 | +* 在声网 [Console 平台](https://console.shengwang.cn/)的 Basic Auth 认证信息 |
| 34 | +* 在声网 [Console 平台](https://console.shengwang.cn/)开启相关的服务能力 |
| 35 | + |
| 36 | +## 安装 |
| 37 | +使用以下命令从 GitHub 安装依赖: |
| 38 | +```shell |
| 39 | +go get -u github.com/AgoraIO-Community/agora-rest-client-go |
| 40 | +``` |
| 41 | +## 使用示例 |
| 42 | +以调用云录制服务为例: |
| 43 | + |
| 44 | +```go |
| 45 | +package main |
| 46 | + |
| 47 | +import ( |
| 48 | + "context" |
| 49 | + "log" |
| 50 | + "time" |
| 51 | + |
| 52 | + "github.com/AgoraIO-Community/agora-rest-client-go/agora" |
| 53 | + "github.com/AgoraIO-Community/agora-rest-client-go/agora/auth" |
| 54 | + "github.com/AgoraIO-Community/agora-rest-client-go/agora/domain" |
| 55 | + agoraLogger "github.com/AgoraIO-Community/agora-rest-client-go/agora/log" |
| 56 | + "github.com/AgoraIO-Community/agora-rest-client-go/services/cloudrecording" |
| 57 | + cloudRecordingAPI "github.com/AgoraIO-Community/agora-rest-client-go/services/cloudrecording/api" |
| 58 | + "github.com/AgoraIO-Community/agora-rest-client-go/services/cloudrecording/scenario/mixrecording" |
| 59 | +) |
| 60 | + |
| 61 | +const ( |
| 62 | + appId = "<your appId>" |
| 63 | + cname = "<your cname>" |
| 64 | + uid = "<your uid>" |
| 65 | + username = "<the username of basic auth credential>" |
| 66 | + password = "<the password of basic auth credential>" |
| 67 | + token = "<your token>" |
| 68 | +) |
| 69 | + |
| 70 | +var storageConfig = &cloudRecordingAPI.StorageConfig{ |
| 71 | + Vendor: 0, |
| 72 | + Region: 0, |
| 73 | + Bucket: "", |
| 74 | + AccessKey: "", |
| 75 | + SecretKey: "", |
| 76 | + FileNamePrefix: []string{ |
| 77 | + "recordings", |
| 78 | + }, |
| 79 | +} |
| 80 | + |
| 81 | +func main() { |
| 82 | + // Initialize Agora Config |
| 83 | + config := &agora.Config{ |
| 84 | + AppID: appId, |
| 85 | + Credential: auth.NewBasicAuthCredential(username, password), |
| 86 | + // Specify the region where the server is located. Options include CN, EU, AP, US. |
| 87 | + // The client will automatically switch to use the best domain based on the configured region. |
| 88 | + DomainArea: domain.CN, |
| 89 | + // Specify the log output level. Options include DebugLevel, InfoLevel, WarningLevel, ErrLevel. |
| 90 | + // To disable log output, set logger to DiscardLogger. |
| 91 | + Logger: agoraLogger.NewDefaultLogger(agoraLogger.DebugLevel), |
| 92 | + } |
| 93 | + |
| 94 | + // Initialize the cloud recording service client |
| 95 | + cloudRecordingClient, err := cloudrecording.NewClient(config) |
| 96 | + if err != nil { |
| 97 | + log.Fatal(err) |
| 98 | + } |
| 99 | + |
| 100 | + // Call the Acquire API of the cloud recording service client |
| 101 | + acquireResp, err := cloudRecordingClient.MixRecording(). |
| 102 | + Acquire(context.TODO(), cname, uid, &mixrecording.AcquireMixRecodingClientRequest{}) |
| 103 | + // Handle non-business errors |
| 104 | + if err != nil { |
| 105 | + log.Fatal(err) |
| 106 | + } |
| 107 | + |
| 108 | + // Handle business response |
| 109 | + if acquireResp.IsSuccess() { |
| 110 | + log.Printf("acquire success:%+v\n", acquireResp) |
| 111 | + } else { |
| 112 | + log.Fatalf("acquire failed:%+v\n", acquireResp) |
| 113 | + } |
| 114 | + |
| 115 | + // Call the Start API of the cloud recording service client |
| 116 | + resourceId := acquireResp.SuccessRes.ResourceId |
| 117 | + startResp, err := cloudRecordingClient.MixRecording(). |
| 118 | + Start(context.TODO(), resourceId, cname, uid, &mixrecording.StartMixRecordingClientRequest{ |
| 119 | + Token: token, |
| 120 | + RecordingConfig: &cloudRecordingAPI.RecordingConfig{ |
| 121 | + ChannelType: 1, |
| 122 | + StreamTypes: 2, |
| 123 | + MaxIdleTime: 30, |
| 124 | + AudioProfile: 2, |
| 125 | + TranscodingConfig: &cloudRecordingAPI.TranscodingConfig{ |
| 126 | + Width: 640, |
| 127 | + Height: 640, |
| 128 | + FPS: 15, |
| 129 | + BitRate: 800, |
| 130 | + MixedVideoLayout: 0, |
| 131 | + BackgroundColor: "#000000", |
| 132 | + }, |
| 133 | + SubscribeAudioUIDs: []string{ |
| 134 | + "#allstream#", |
| 135 | + }, |
| 136 | + SubscribeVideoUIDs: []string{ |
| 137 | + "#allstream#", |
| 138 | + }, |
| 139 | + }, |
| 140 | + RecordingFileConfig: &cloudRecordingAPI.RecordingFileConfig{ |
| 141 | + AvFileType: []string{ |
| 142 | + "hls", |
| 143 | + "mp4", |
| 144 | + }, |
| 145 | + }, |
| 146 | + StorageConfig: storageConfig, |
| 147 | + }) |
| 148 | + // Handle non-business errors |
| 149 | + if err != nil { |
| 150 | + log.Fatal(err) |
| 151 | + } |
| 152 | + |
| 153 | + // Handle business response |
| 154 | + if startResp.IsSuccess() { |
| 155 | + log.Printf("start success:%+v\n", startResp) |
| 156 | + } else { |
| 157 | + log.Fatalf("start failed:%+v\n", startResp) |
| 158 | + } |
| 159 | + |
| 160 | + sid := startResp.SuccessResponse.Sid |
| 161 | + // Query |
| 162 | + for i := 0; i < 6; i++ { |
| 163 | + queryResp, err := cloudRecordingClient.MixRecording(). |
| 164 | + QueryHLSAndMP4(context.TODO(), resourceId, sid) |
| 165 | + // Handle non-business errors |
| 166 | + if err != nil { |
| 167 | + log.Fatal(err) |
| 168 | + } |
| 169 | + |
| 170 | + // Handle business response |
| 171 | + if queryResp.IsSuccess() { |
| 172 | + log.Printf("query success:%+v\n", queryResp) |
| 173 | + } else { |
| 174 | + log.Printf("query failed:%+v\n", queryResp) |
| 175 | + } |
| 176 | + time.Sleep(time.Second * 10) |
| 177 | + } |
| 178 | + |
| 179 | + // Call the Stop API of the cloud recording service client |
| 180 | + stopResp, err := cloudRecordingClient.MixRecording(). |
| 181 | + Stop(context.TODO(), resourceId, sid, cname, uid, true) |
| 182 | + // Handle non-business errors |
| 183 | + if err != nil { |
| 184 | + log.Fatal(err) |
| 185 | + } |
| 186 | + |
| 187 | + // Handle business response |
| 188 | + if stopResp.IsSuccess() { |
| 189 | + log.Printf("stop success:%+v\n", stopResp) |
| 190 | + } else { |
| 191 | + log.Printf("stop failed:%+v\n", stopResp) |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +``` |
| 196 | +更多的示例可在[Example](./examples) 查看 |
| 197 | + |
| 198 | +## 集成遇到困难,该如何联系声网获取协助 |
| 199 | + |
| 200 | +> 方案1:如果您已经在使用声网服务或者在对接中,可以直接联系对接的销售或服务 |
| 201 | +> |
| 202 | + |
| 203 | +> |
| 204 | +> 方案3:扫码加入我们的微信交流群提问 |
| 205 | +> |
| 206 | +> <img src="https://download.agora.io/demo/release/SDHY_QA.jpg" width="360" height="360"> |
| 207 | +--- |
| 208 | + |
| 209 | +## 贡献 |
| 210 | +本项目欢迎并接受贡献。如果您在使用中遇到问题或有改进建议,请提出issue或向我们提交Pull Request。 |
| 211 | + |
| 212 | +# SemVer 版本规范 |
| 213 | +本项目使用语义化版本号规范 (SemVer) 来管理版本。格式为 MAJOR.MINOR.PATCH。 |
| 214 | + |
| 215 | +* MAJOR 版本号表示不向后兼容的重大更改。 |
| 216 | +* MINOR 版本号表示向后兼容的新功能或增强。 |
| 217 | +* PATCH 版本号表示向后兼容的错误修复和维护。 |
| 218 | +有关详细信息,请参阅 [语义化版本](https://semver.org/lang/zh-CN/) 规范。 |
| 219 | + |
| 220 | +## 参考 |
| 221 | +* [Agora API 文档](https://doc.shengwang.cn/) |
| 222 | + |
| 223 | +## 许可证 |
| 224 | +该项目使用MIT许可证,详细信息请参阅LICENSE文件。 |
0 commit comments