Skip to content

Commit

Permalink
Merge pull request #53 from apivideo/feature/origin_sdk_header
Browse files Browse the repository at this point in the history
feat(ios): add origin sdk field support
  • Loading branch information
bot-api-video authored Jul 5, 2022
2 parents 0b0cf0c + e5571d0 commit 778a175
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
37 changes: 28 additions & 9 deletions api_videos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
64 changes: 46 additions & 18 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Client struct {
Token *Token
applicationName string
applicationVersion string
sdkName string
sdkVersion string

Authentication AuthenticationServiceI
Captions CaptionsServiceI
Expand Down Expand Up @@ -88,6 +90,8 @@ type Builder struct {
httpClient Doer
applicationName string
applicationVersion string
sdkName string
sdkVersion string
}

func (cb *Builder) BaseURL(url string) *Builder {
Expand All @@ -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
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -223,15 +239,23 @@ 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
}
if originAppHeaderValue != "" {
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])
Expand All @@ -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(
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down

0 comments on commit 778a175

Please sign in to comment.