diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b6383a..d2e65b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Changelog All changes to this project will be documented in this file. +## [1.2.5] - 2022-07-05 +- Add SDK origin header + ## [1.2.4] - 2022-04-01 - Fix nullable fields in `status` endpoint result diff --git a/api_videos_test.go b/api_videos_test.go index cb14fc8..5ceed07 100644 --- a/api_videos_test.go +++ b/api_videos_test.go @@ -421,26 +421,45 @@ func skipIfNoApiKey(t *testing.T) { func createRealClient() *Client { return (&Builder{ - baseURL: os.Getenv("BASE_URI"), - uploadChunkSize: minChunkSize, - apiKey: os.Getenv("API_KEY"), - applicationName: "client-integration-tests", + baseURL: os.Getenv("BASE_URI"), + uploadChunkSize: minChunkSize, + apiKey: os.Getenv("API_KEY"), + applicationName: "client-integration-tests", + applicationVersion: "0", }).Build() } -func TestVideos_BadApplicationName(t *testing.T) { +func TestVideos_BadApplicationNameOrVersion(t *testing.T) { cl := ClientBuilder("a").ApplicationName("bad application name").Build() _, err := cl.Videos.Create(VideoCreationPayload{Title: "Upload stream GO"}) - if err == nil { + if err == nil || err.Error() != "Invalid name value. Allowed characters: A-Z, a-z, 0-9, '-', '_'. Max length: 50." { t.Errorf("Application name validation should failed when invalid characters are used") } - _, err2 := cl.Videos.Create(VideoCreationPayload{Title: "012345678901234567890123456789012345678901234567891"}) + cl2 := ClientBuilder("a").ApplicationName("applicationname").ApplicationVersion("0 1").Build() + _, err2 := cl2.Videos.Create(VideoCreationPayload{Title: "test"}) - if err2 == nil { - t.Errorf("Application name validation should failed when too long") + if err == nil || err2.Error() != "Invalid version value. The version should match the xxx[.yyy][.zzz] pattern." { + t.Errorf("Application version validation should failed when invalid characters are used") + } +} + +func TestVideos_BadSdkNameOrVersion(t *testing.T) { + cl := ClientBuilder("a").SdkName("bad sdk name").Build() + + _, err := cl.Videos.Create(VideoCreationPayload{Title: "Upload stream GO"}) + + if err == nil || err.Error() != "Invalid name value. Allowed characters: A-Z, a-z, 0-9, '-', '_'. Max length: 50." { + t.Errorf("Sdk name validation should failed when invalid characters are used") + } + + cl2 := ClientBuilder("a").SdkName("applicationname").SdkVersion("0 1").Build() + _, err2 := cl2.Videos.Create(VideoCreationPayload{Title: "test"}) + + if err == nil || err2.Error() != "Invalid version value. The version should match the xxx[.yyy][.zzz] pattern." { + t.Errorf("Sdk version validation should failed when invalid characters are used") } } diff --git a/client.go b/client.go index 31cd7a2..a89a63d 100644 --- a/client.go +++ b/client.go @@ -27,6 +27,8 @@ type Client struct { Token *Token applicationName string applicationVersion string + sdkName string + sdkVersion string Authentication AuthenticationServiceI Captions CaptionsServiceI @@ -88,6 +90,8 @@ type Builder struct { httpClient Doer applicationName string applicationVersion string + sdkName string + sdkVersion string } func (cb *Builder) BaseURL(url string) *Builder { @@ -110,6 +114,16 @@ func (cb *Builder) ApplicationVersion(applicationVersion string) *Builder { return cb } +func (cb *Builder) SdkName(sdkName string) *Builder { + cb.sdkName = sdkName + return cb +} + +func (cb *Builder) SdkVersion(sdkVersion string) *Builder { + cb.sdkVersion = sdkVersion + return cb +} + func (cb *Builder) UploadChunkSize(size int64) *Builder { if size < minChunkSize { cb.uploadChunkSize = minChunkSize @@ -159,6 +173,8 @@ func (cb *Builder) Build() *Client { chunkSize: cb.uploadChunkSize, applicationName: cb.applicationName, applicationVersion: cb.applicationVersion, + sdkName: cb.sdkName, + sdkVersion: cb.sdkVersion, } c.Authentication = &AuthenticationService{client: c} @@ -223,7 +239,7 @@ func (c *Client) prepareRequest( req.Header.Set("Accept", "application/json") req.Header.Set("Content-Type", "application/json") - originAppHeaderValue, err := getOriginAppHeaderValue(c.applicationName, c.applicationVersion) + originAppHeaderValue, err := getOriginHeaderValue(c.applicationName, c.applicationVersion) if err != nil { return nil, err } @@ -231,7 +247,15 @@ func (c *Client) prepareRequest( req.Header.Set("AV-Origin-App", originAppHeaderValue) } - req.Header.Set("AV-Origin-Client", "go:1.2.4") + originSdkHeaderValue, err := getOriginHeaderValue(c.sdkName, c.sdkVersion) + if err != nil { + return nil, err + } + if originSdkHeaderValue != "" { + req.Header.Set("AV-Origin-Sdk", originSdkHeaderValue) + } + + req.Header.Set("AV-Origin-Client", "go:1.2.5") for headerName := range headerParams { req.Header.Set(headerName, headerParams[headerName]) @@ -247,27 +271,21 @@ func (c *Client) prepareRequest( return req, nil } -func getOriginAppHeaderValue(applicationName string, applicationVersion string) (string, error) { - if applicationName == "" { - if applicationVersion != "" { - return "", errors.New("applicationName is mandatory when applicationVersion is set.") - } +func getOriginHeaderValue(name string, version string) (string, error) { + if name == "" && version == "" { return "", nil } var re = regexp.MustCompile(`(?m)^[\w-]{1,50}$`) - if !re.MatchString(applicationName) { - return "", errors.New("Invalid applicationName value. Allowed characters: A-Z, a-z, 0-9, '-', '_'. Max length: 50.") + if !re.MatchString(name) { + return "", errors.New("Invalid name value. Allowed characters: A-Z, a-z, 0-9, '-', '_'. Max length: 50.") } - if applicationVersion != "" { - var reVersion = regexp.MustCompile(`(?m)^\d{1,3}(\.\d{1,3}(\.\d{1,3})?)?$`) - if !reVersion.MatchString(applicationVersion) { - return "", errors.New("Invalid applicationVersion value. The version should match the xxx[.yyy][.zzz] pattern.") - } - - return applicationName + ":" + applicationVersion, nil + var reVersion = regexp.MustCompile(`(?m)^\d{1,3}(\.\d{1,3}(\.\d{1,3})?)?$`) + if !reVersion.MatchString(version) { + return "", errors.New("Invalid version value. The version should match the xxx[.yyy][.zzz] pattern.") } - return applicationName, nil + + return name + ":" + version, nil } func (c *Client) prepareProgressiveUploadRequest( @@ -495,7 +513,7 @@ func (c *Client) auth(req *http.Request) (*http.Request, error) { req.Header.Set("Accept", "application/json") req.Header.Set("Content-Type", "application/json") - originAppHeaderValue, err := getOriginAppHeaderValue(c.applicationName, c.applicationVersion) + originAppHeaderValue, err := getOriginHeaderValue(c.applicationName, c.applicationVersion) if err != nil { return nil, err } @@ -504,6 +522,16 @@ func (c *Client) auth(req *http.Request) (*http.Request, error) { } req.Header.Set("AV-Origin-Client", "go:1.2.4") + originSdkHeaderValue, err := getOriginHeaderValue(c.sdkName, c.sdkVersion) + if err != nil { + return nil, err + } + if originSdkHeaderValue != "" { + req.Header.Set("AV-Origin-Sdk", originSdkHeaderValue) + } + + req.Header.Set("AV-Origin-Client", "go:1.2.5") + resp, err := c.httpClient.Do(req) if err != nil {