Skip to content

Commit

Permalink
TopicDetail and TopicDelete definitions (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellemaxwell committed Nov 18, 2022
1 parent 4bb48cf commit 8ed3c5e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/tenant/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type TenantClient interface {

TopicList(context.Context, *PageQuery) (*TopicPage, error)
TopicCreate(context.Context, *Topic) (*Topic, error)
TopicDetail(ctx context.Context, id string) (*Topic, error)
TopicDelete(ctx context.Context, id string) error

ProjectAPIKeyList(ctx context.Context, id string, in *PageQuery) (*ProjectAPIKeyPage, error)
ProjectAPIKeyCreate(ctx context.Context, id string, in *APIKey) (*APIKey, error)
Expand Down
37 changes: 37 additions & 0 deletions pkg/tenant/api/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,43 @@ func (s *APIv1) TopicCreate(ctx context.Context, in *Topic) (out *Topic, err err
return out, err
}

func (s *APIv1) TopicDetail(ctx context.Context, id string) (out *Topic, err error) {
if id == "" {
return nil, ErrTopicIDRequired
}

path := fmt.Sprintf("/v1/topics/%s", id)

// Make the HTTP request
var req *http.Request
if req, err = s.NewRequest(ctx, http.MethodGet, path, nil, nil); err != nil {
return nil, err
}

if _, err = s.Do(req, &out, true); err != nil {
return nil, err
}
return out, nil
}

func (s *APIv1) TopicDelete(ctx context.Context, id string) (err error) {
if id == "" {
return ErrTopicIDRequired
}

path := fmt.Sprintf("/v1/topics/%s", id)

// Make the HTTP request
var req *http.Request
if req, err = s.NewRequest(ctx, http.MethodDelete, path, nil, nil); err != nil {
return err
}
if _, err = s.Do(req, nil, true); err != nil {
return err
}
return nil
}

func (s *APIv1) ProjectAPIKeyList(ctx context.Context, id string, in *PageQuery) (out *ProjectAPIKeyPage, err error) {
if id == "" {
return nil, ErrProjectIDRequired
Expand Down
48 changes: 48 additions & 0 deletions pkg/tenant/api/v1/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,54 @@ func TestTopicCreate(t *testing.T) {
require.Equal(t, fixture, out, "unexpected response returned")
}

func TestTopicDetail(t *testing.T) {
fixture := &api.Topic{
ID: "001",
Name: "topic01",
}

// Creates a test server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, http.MethodGet, r.Method)
require.Equal(t, "/v1/topics/topic001", r.URL.Path)

w.Header().Add("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(fixture)
}))
defer ts.Close()

// Creates a client to execute tests against the test server
client, err := api.New(ts.URL)
require.NoError(t, err, "could not create api client")

out, err := client.TopicDetail(context.Background(), "topic001")
require.NoError(t, err, "could not execute api request")
require.Equal(t, fixture, out, "unexpected response error")
}

func TestTopicDelete(t *testing.T) {
fixture := &api.Reply{}

// Creates a test server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, http.MethodDelete, r.Method)
require.Equal(t, "/v1/topics/topic001", r.URL.Path)

w.Header().Add("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(fixture)
}))
defer ts.Close()

// Creates a client to execute tests against the test server
client, err := api.New(ts.URL)
require.NoError(t, err, "could not create api client")

err = client.TopicDelete(context.TODO(), "topic001")
require.NoError(t, err, "could not execute api request")
}

func TestProjectAPIKeyList(t *testing.T) {
fixture := &api.ProjectAPIKeyPage{
ProjectID: "001",
Expand Down
1 change: 1 addition & 0 deletions pkg/tenant/api/v1/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
ErrTenantIDRequired = errors.New("tenant id is required for this endpoint")
ErrMemberIDRequired = errors.New("member id is required for this endpoint")
ErrProjectIDRequired = errors.New("project id is required for this endpoint")
ErrTopicIDRequired = errors.New("topic id is required for this endpoint")
)

// Constructs a new response for an error or returns unsuccessful.
Expand Down

0 comments on commit 8ed3c5e

Please sign in to comment.