-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tags to the client and testing
- Loading branch information
1 parent
81b4873
commit 3e908d3
Showing
6 changed files
with
519 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package tags | ||
|
||
import "fmt" | ||
|
||
type AllTags struct { | ||
Tags *[]*Tag `json:"tags,omitempty"` | ||
} | ||
|
||
type Tag struct { | ||
Key *string `json:"key,omitempty"` | ||
Value *string `json:"value,omitempty"` | ||
} | ||
|
||
type NotFound struct { | ||
subId int | ||
dbId int | ||
} | ||
|
||
func (f *NotFound) Error() string { | ||
return fmt.Sprintf("database %d in subscription %d not found", f.dbId, f.subId) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package tags | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/RedisLabs/rediscloud-go-api/internal" | ||
) | ||
|
||
type HttpClient interface { | ||
Get(ctx context.Context, name, path string, responseBody interface{}) error | ||
Put(ctx context.Context, name, path string, requestBody interface{}, responseBody interface{}) error | ||
} | ||
|
||
type API struct { | ||
client HttpClient | ||
} | ||
|
||
func NewAPI(client HttpClient) *API { | ||
return &API{client: client} | ||
} | ||
|
||
func (a *API) Get(ctx context.Context, subscription int, database int) (*AllTags, error) { | ||
message := fmt.Sprintf("get tags for database %d in subscription %d", subscription, database) | ||
address := fmt.Sprintf("/subscriptions/%d/databases/%d/tags", subscription, database) | ||
tags, err := a.get(ctx, message, address) | ||
if err != nil { | ||
return nil, wrap404Error(subscription, database, err) | ||
} | ||
return tags, nil | ||
} | ||
|
||
func (a *API) GetFixed(ctx context.Context, subscription int, database int) (*AllTags, error) { | ||
message := fmt.Sprintf("get tags for fixed database %d in subscription %d", subscription, database) | ||
address := fmt.Sprintf("fixed/subscriptions/%d/databases/%d/tags", subscription, database) | ||
tags, err := a.get(ctx, message, address) | ||
if err != nil { | ||
return nil, wrap404Error(subscription, database, err) | ||
} | ||
return tags, nil | ||
} | ||
|
||
func (a *API) Put(ctx context.Context, subscription int, database int, tags AllTags) error { | ||
message := fmt.Sprintf("update tags for database %d in subscription %d", subscription, database) | ||
address := fmt.Sprintf("/subscriptions/%d/databases/%d/tags", subscription, database) | ||
err := a.put(ctx, message, address, tags) | ||
if err != nil { | ||
return wrap404Error(subscription, database, err) | ||
} | ||
return nil | ||
} | ||
|
||
func (a *API) PutFixed(ctx context.Context, subscription int, database int, tags AllTags) error { | ||
message := fmt.Sprintf("update tags for fixed database %d in subscription %d", subscription, database) | ||
address := fmt.Sprintf("fixed/subscriptions/%d/databases/%d/tags", subscription, database) | ||
err := a.put(ctx, message, address, tags) | ||
if err != nil { | ||
return wrap404Error(subscription, database, err) | ||
} | ||
return nil | ||
} | ||
|
||
func (a *API) get(ctx context.Context, message string, address string) (*AllTags, error) { | ||
var tags AllTags | ||
err := a.client.Get(ctx, message, address, &tags) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &tags, nil | ||
} | ||
|
||
func (a *API) put(ctx context.Context, message string, address string, tags AllTags) error { | ||
var tagsResponse AllTags | ||
return a.client.Put(ctx, message, address, tags, &tagsResponse) | ||
} | ||
|
||
func wrap404Error(subId int, dbId int, err error) error { | ||
if v, ok := err.(*internal.HTTPError); ok && v.StatusCode == http.StatusNotFound { | ||
return &NotFound{subId: subId, dbId: dbId} | ||
} | ||
return err | ||
} |
Oops, something went wrong.