@@ -13,6 +13,7 @@ import (
1313 "time"
1414)
1515
16+ // API holds information used to communicate with the RT-CV api
1617type API struct {
1718 authHeaderValue string
1819 serverLocation string
@@ -21,20 +22,19 @@ type API struct {
2122 MockOptions MockOptions
2223}
2324
25+ // MockOptions represends options for the RT-CV mocking mode
2426type MockOptions struct {
2527 Secrets map [string ]json.RawMessage `json:"secrets"`
2628}
2729
30+ // NewAPI creates a new instance of the API
2831func NewAPI () * API {
2932 return & API {
30- authHeaderValue : "" ,
31- serverLocation : "" ,
32- Cache : map [string ]time.Time {},
33- MockMode : false ,
34- MockOptions : MockOptions {},
33+ Cache : map [string ]time.Time {},
3534 }
3635}
3736
37+ // SetCredentials sets the api credentials so we can make fetch requests to RT-CV
3838func (a * API ) SetCredentials (serverLocation , apiKeyID , apiKey string , runAsMockWithOpts * MockOptions ) error {
3939 a .MockMode = runAsMockWithOpts != nil
4040 if a .MockMode {
@@ -54,21 +54,24 @@ func (a *API) SetCredentials(serverLocation, apiKeyID, apiKey string, runAsMockW
5454 if apiKey == "" {
5555 return errors .New ("api_key cannot be empty" )
5656 }
57- hashedApiKey := sha512 .Sum512 ([]byte (apiKey ))
58- hashedApiKeyStr := hex .EncodeToString (hashedApiKey [:])
59- a .authHeaderValue = "Basic " + apiKeyID + ":" + hashedApiKeyStr
57+ hashedAPIKey := sha512 .Sum512 ([]byte (apiKey ))
58+ hashedAPIKeyStr := hex .EncodeToString (hashedAPIKey [:])
59+ a .authHeaderValue = "Basic " + apiKeyID + ":" + hashedAPIKeyStr
6060
6161 return nil
6262}
6363
64+ // Get makes a get request to RT-CV
6465func (a * API ) Get (path string , unmarshalResInto interface {}) error {
6566 return a .DoRequest ("GET" , path , nil , unmarshalResInto )
6667}
6768
69+ // Post makes a post request to RT-CV
6870func (a * API ) Post (path string , body interface {}, unmarshalResInto interface {}) error {
6971 return a .DoRequest ("POST" , path , body , unmarshalResInto )
7072}
7173
74+ // DoRequest makes a http request to RT-CV
7275func (a * API ) DoRequest (method , path string , body , unmarshalResInto interface {}) error {
7376 var reqBody io.ReadCloser
7477 if body != nil {
@@ -115,12 +118,16 @@ func (a *API) DoRequest(method, path string, body, unmarshalResInto interface{})
115118 return nil
116119}
117120
121+ // NoCredentials returns true if the SetCredentials method was not yet called and we aren't in mock mode
118122func (a * API ) NoCredentials () bool {
119123 return a .authHeaderValue == "" && ! a .MockMode
120124}
121125
126+ // ErrMissingCredentials is returned when the SetCredentials method was not yet called
127+ // while we're trying to execute an action that requires them
122128var ErrMissingCredentials = errors .New ("missing credentials, call set_credentials before this method" )
123129
130+ // GetSecret returns a secret from RT-CV
124131func (a * API ) GetSecret (key , encryptionKey string , result interface {}) error {
125132 if a .MockMode {
126133 if a .MockOptions .Secrets == nil {
@@ -138,6 +145,7 @@ func (a *API) GetSecret(key, encryptionKey string, result interface{}) error {
138145 return a .Get (fmt .Sprintf ("/api/v1/secrets/myKey/%s/%s" , key , encryptionKey ), result )
139146}
140147
148+ // GetUsersSecret returns strictly defined users secret
141149func (a * API ) GetUsersSecret (key , encryptionKey string ) ([]UserSecret , error ) {
142150 if a .NoCredentials () {
143151 return []UserSecret {}, ErrMissingCredentials
@@ -151,6 +159,7 @@ func (a *API) GetUsersSecret(key, encryptionKey string) ([]UserSecret, error) {
151159 return result , err
152160}
153161
162+ // GetUserSecret returns strictly defined user secret
154163func (a * API ) GetUserSecret (key , encryptionKey string ) (UserSecret , error ) {
155164 if a .NoCredentials () {
156165 return UserSecret {}, ErrMissingCredentials
@@ -164,6 +173,7 @@ func (a *API) GetUserSecret(key, encryptionKey string) (UserSecret, error) {
164173 return result , err
165174}
166175
176+ // CacheEntryExists returns true if the cache entry exists and is not expired
167177func (a * API ) CacheEntryExists (referenceNr string ) bool {
168178 cacheEntryInsertionTime , cacheEntryExists := a .Cache [referenceNr ]
169179 if cacheEntryExists {
@@ -178,6 +188,7 @@ func (a *API) CacheEntryExists(referenceNr string) bool {
178188 return cacheEntryExists
179189}
180190
191+ // UserSecret represents the json layout of an user secret
181192type UserSecret struct {
182193 Username string `json:"username"`
183194 Password string `json:"password"`
0 commit comments