Passed
Pull Request — master (#29)
by eval
01:33
created

pinpoint/client_xapi_app.go   A

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 27
dl 0
loc 45
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A pinpoint.*Pinpoint.XExistsApp 0 14 4
B pinpoint.*Pinpoint.XGetAppIDByName 0 19 6
1
package pinpoint
2
3
import (
4
	"context"
5
6
	"github.com/evalphobia/aws-sdk-go-v2-wrapper/errors"
7
)
8
9
// XExistsApp checks if the application already exists or not.
10
func (svc *Pinpoint) XExistsApp(ctx context.Context, appID string) (bool, error) {
11
	_, err := svc.GetApp(ctx, GetAppRequest{
12
		ApplicationID: appID,
13
	})
14
	if err == nil {
15
		return true, nil
16
	}
17
18
	if e, ok := err.(errors.ErrorData); ok {
19
		if e.GetAWSErrCode() == "NotFoundException" {
20
			return false, nil
21
		}
22
	}
23
	return false, err
24
}
25
26
// XGetAppIDByName searches app by name and returns its ID.
27
func (svc *Pinpoint) XGetAppIDByName(ctx context.Context, appName string) (appID string, err error) {
28
	nextToken := ""
29
	for {
30
		resp, err := svc.GetApps(ctx, GetAppsRequest{
31
			PageSize: "1000",
32
			Token:    nextToken,
33
		})
34
		if err != nil {
35
			return "", err
36
		}
37
		for _, v := range resp.Item {
38
			if v.Name == appName {
39
				return v.ID, nil
40
			}
41
		}
42
		if resp.NextToken == "" {
43
			return "", nil
44
		}
45
		nextToken = resp.NextToken
46
	}
47
}
48