-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository.go
89 lines (70 loc) · 1.97 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package repository
import (
"encoding/json"
"net/url"
"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
"github.com/cloudfoundry/cli/cf/v3/models"
"github.com/cloudfoundry/go-ccapi/v3/client"
)
//go:generate counterfeiter . Repository
type Repository interface {
GetApplications() ([]models.V3Application, error)
GetProcesses(path string) ([]models.V3Process, error)
GetRoutes(path string) ([]models.V3Route, error)
}
type repository struct {
client client.Client
config coreconfig.ReadWriter
}
func NewRepository(config coreconfig.ReadWriter, client client.Client) Repository {
return &repository{
client: client,
config: config,
}
}
func (r *repository) handleUpdatedTokens() {
if r.client.TokensUpdated() {
accessToken, refreshToken := r.client.GetUpdatedTokens()
r.config.SetAccessToken(accessToken)
r.config.SetRefreshToken(refreshToken)
}
}
func (r *repository) GetApplications() ([]models.V3Application, error) {
jsonResponse, err := r.client.GetApplications(url.Values{})
if err != nil {
return []models.V3Application{}, err
}
r.handleUpdatedTokens()
applications := []models.V3Application{}
err = json.Unmarshal(jsonResponse, &applications)
if err != nil {
return []models.V3Application{}, err
}
return applications, nil
}
func (r *repository) GetProcesses(path string) ([]models.V3Process, error) {
jsonResponse, err := r.client.GetResources(path, 0)
if err != nil {
return []models.V3Process{}, err
}
r.handleUpdatedTokens()
processes := []models.V3Process{}
err = json.Unmarshal(jsonResponse, &processes)
if err != nil {
return []models.V3Process{}, err
}
return processes, nil
}
func (r *repository) GetRoutes(path string) ([]models.V3Route, error) {
jsonResponse, err := r.client.GetResources(path, 0)
if err != nil {
return []models.V3Route{}, err
}
r.handleUpdatedTokens()
routes := []models.V3Route{}
err = json.Unmarshal(jsonResponse, &routes)
if err != nil {
return []models.V3Route{}, err
}
return routes, nil
}