Skip to content

A Go library for integrating with Agora's REST API. This open-source project provides wrappers and implementations for Agora's official REST API, making it easier for developers to integrate Agora's server-side services into their Go applications.

License

Notifications You must be signed in to change notification settings

AgoraIO-Community/agora-rest-client-go

Repository files navigation

Agora REST Client for Go

GitHub License Go Reference Go Actions gitee-sync GitHub go.mod Go version GitHub GitHub Issues or Pull Requests

agora-rest-client-go是用Go语言编写的一个开源项目,专门为 Agora REST API设计。它包含了 Agora 官方提供的REST API接口的包装和内部实现,可以帮助开发者更加方便的集成服务端Agora REST API。

注意: 该SDK经过一些测试以确保其基本功能正常运作。然而,由于软件开发的复杂性,我们无法保证它是完全没有缺陷的。

该SDK目前可能存在一些潜在的BUG或不稳定性。我们鼓励社区的开发者和用户积极参与,共同改进这个项目。

特性

  • 封装了Agora REST API的请求和响应处理,简化与Agora REST API 的通信流程
  • 当遇到 DNS 解析失败、网络错误或者请求超时等问题的时候,提供了自动切换最佳域名的能力,以保障请求 REST API 服务的可用性
  • 提供了易于使用的API,可轻松地实现调用 Agora REST API 的常见功能,如开启云录制、停止云录制等
  • 基于Go语言,具有高效性、并发性和可扩展性

支持的服务

环境准备

安装

使用以下命令从 GitHub 安装依赖:

go get -u github.com/AgoraIO-Community/agora-rest-client-go

使用示例

以调用云录制服务为例:

package main

import (
	"context"
	"log"
	"time"

	"github.com/AgoraIO-Community/agora-rest-client-go/agora"
	"github.com/AgoraIO-Community/agora-rest-client-go/agora/auth"
	"github.com/AgoraIO-Community/agora-rest-client-go/agora/domain"
	agoraLogger "github.com/AgoraIO-Community/agora-rest-client-go/agora/log"
	"github.com/AgoraIO-Community/agora-rest-client-go/services/cloudrecording"
	cloudRecordingAPI "github.com/AgoraIO-Community/agora-rest-client-go/services/cloudrecording/api"
	"github.com/AgoraIO-Community/agora-rest-client-go/services/cloudrecording/scenario/mixrecording"
)

const (
	appId    = "<your appId>"
	cname    = "<your cname>"
	uid      = "<your uid>"
	username = "<the username of basic auth credential>"
	password = "<the password of basic auth credential>"
	token    = "<your token>"
)

var storageConfig = &cloudRecordingAPI.StorageConfig{
	Vendor:    0,
	Region:    0,
	Bucket:    "",
	AccessKey: "",
	SecretKey: "",
	FileNamePrefix: []string{
		"recordings",
	},
}

func main() {
	// Initialize Agora Config
	config := &agora.Config{
		AppID:      appId,
		Credential: auth.NewBasicAuthCredential(username, password),
		// Specify the region where the server is located. Options include CN, EU, AP, US.
		// The client will automatically switch to use the best domain based on the configured region.
		DomainArea: domain.CN,
		// Specify the log output level. Options include DebugLevel, InfoLevel, WarningLevel, ErrLevel.
		// To disable log output, set logger to DiscardLogger.
		Logger: agoraLogger.NewDefaultLogger(agoraLogger.DebugLevel),
	}

	// Initialize the cloud recording service client
	cloudRecordingClient, err := cloudrecording.NewClient(config)
	if err != nil {
		log.Fatal(err)
	}

	// Call the Acquire API of the cloud recording service client
	acquireResp, err := cloudRecordingClient.MixRecording().
		Acquire(context.TODO(), cname, uid, &mixrecording.AcquireMixRecodingClientRequest{})
	// Handle non-business errors
	if err != nil {
		log.Fatal(err)
	}

	// Handle business response
	if acquireResp.IsSuccess() {
		log.Printf("acquire success:%+v\n", acquireResp)
	} else {
		log.Fatalf("acquire failed:%+v\n", acquireResp)
	}

	// Call the Start API of the cloud recording service client
	resourceId := acquireResp.SuccessRes.ResourceId
	startResp, err := cloudRecordingClient.MixRecording().
		Start(context.TODO(), resourceId, cname, uid, &mixrecording.StartMixRecordingClientRequest{
			Token: token,
			RecordingConfig: &cloudRecordingAPI.RecordingConfig{
				ChannelType:  1,
				StreamTypes:  2,
				MaxIdleTime:  30,
				AudioProfile: 2,
				TranscodingConfig: &cloudRecordingAPI.TranscodingConfig{
					Width:            640,
					Height:           640,
					FPS:              15,
					BitRate:          800,
					MixedVideoLayout: 0,
					BackgroundColor:  "#000000",
				},
				SubscribeAudioUIDs: []string{
					"#allstream#",
				},
				SubscribeVideoUIDs: []string{
					"#allstream#",
				},
			},
			RecordingFileConfig: &cloudRecordingAPI.RecordingFileConfig{
				AvFileType: []string{
					"hls",
					"mp4",
				},
			},
			StorageConfig: storageConfig,
		})
	// Handle non-business errors
	if err != nil {
		log.Fatal(err)
	}

	// Handle business response
	if startResp.IsSuccess() {
		log.Printf("start success:%+v\n", startResp)
	} else {
		log.Fatalf("start failed:%+v\n", startResp)
	}

	sid := startResp.SuccessResponse.Sid
	// Query
	for i := 0; i < 6; i++ {
		queryResp, err := cloudRecordingClient.MixRecording().
			QueryHLSAndMP4(context.TODO(), resourceId, sid)
		// Handle non-business errors
		if err != nil {
			log.Fatal(err)
		}

		// Handle business response
		if queryResp.IsSuccess() {
			log.Printf("query success:%+v\n", queryResp)
		} else {
			log.Printf("query failed:%+v\n", queryResp)
		}
		time.Sleep(time.Second * 10)
	}

	// Call the Stop API of the cloud recording service client
	stopResp, err := cloudRecordingClient.MixRecording().
		Stop(context.TODO(), resourceId, sid, cname, uid, true)
	// Handle non-business errors
	if err != nil {
		log.Fatal(err)
	}

	// Handle business response
	if stopResp.IsSuccess() {
		log.Printf("stop success:%+v\n", stopResp)
	} else {
		log.Printf("stop failed:%+v\n", stopResp)
	}
}

更多的示例可在Example 查看

集成遇到困难,该如何联系声网获取协助

方案1:如果您已经在使用声网服务或者在对接中,可以直接联系对接的销售或服务

方案2:发送邮件给 [email protected] 咨询

方案3:扫码加入我们的微信交流群提问


贡献

本项目欢迎并接受贡献。如果您在使用中遇到问题或有改进建议,请提出issue或向我们提交Pull Request。

SemVer 版本规范

本项目使用语义化版本号规范 (SemVer) 来管理版本。格式为 MAJOR.MINOR.PATCH。

  • MAJOR 版本号表示不向后兼容的重大更改。
  • MINOR 版本号表示向后兼容的新功能或增强。
  • PATCH 版本号表示向后兼容的错误修复和维护。 有关详细信息,请参阅 语义化版本 规范。

参考

许可证

该项目使用MIT许可证,详细信息请参阅LICENSE文件。

About

A Go library for integrating with Agora's REST API. This open-source project provides wrappers and implementations for Agora's official REST API, making it easier for developers to integrate Agora's server-side services into their Go applications.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •