Skip to content

Commit

Permalink
Merge pull request #3 from DEXPRO-Solutions-GmbH/feat/put-store
Browse files Browse the repository at this point in the history
Add function to create store
  • Loading branch information
fabiante authored Dec 12, 2023
2 parents ef80df9 + 08f4cb7 commit 1e5ab62
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
11 changes: 9 additions & 2 deletions store_client_test.go → client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (
"gopkg.in/resty.v1"
)

var DefaultClient *easclient.StoreClient
var (
DefaultClient *easclient.StoreClient
DefaultServerClient *easclient.ServerClient
)

// init is run before any tests are executed.
// it loads the environment variables from .env and creates
Expand All @@ -25,8 +28,12 @@ func init() {
client := resty.New()
client.SetHostURL(fmt.Sprintf("http://%s/eas/archives/%s", os.Getenv("EAS_HOST"), os.Getenv("EAS_STORE")))
client.SetBasicAuth(os.Getenv("EAS_USER"), os.Getenv("EAS_PASSWORD"))

DefaultClient = easclient.NewStoreClient(client)

serverClient := resty.New()
serverClient.SetHostURL(fmt.Sprintf("http://%s/eas/archives", os.Getenv("EAS_HOST")))
serverClient.SetBasicAuth(os.Getenv("EAS_USER"), os.Getenv("EAS_PASSWORD"))
DefaultServerClient = easclient.NewServerClient(serverClient)
}

func testPrelude(t *testing.T) {
Expand Down
40 changes: 40 additions & 0 deletions r_put_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package easclient

import (
"context"
"fmt"
)

type ConfigurationParameter struct {
Name string `json:"name"`
Value string `json:"value"`
}

type ConfigurationTemplate struct {
Name string `json:"name"`
Parameters []ConfigurationParameter `json:"parameters"`
}

type PutStoreRequest struct {
ConfigurationTemplate `json:"configurationTemplate"`
}

func (c *ServerClient) PutStore(ctx context.Context, storeName string, request *PutStoreRequest) error {
req, err := newRequest(ctx, c.c)
if err != nil {
return err
}

req.SetBody(request)

res, err := req.Put("/" + storeName)
if err != nil {
return err
}

if status := res.StatusCode(); status != 201 {
return fmt.Errorf("unexpected response status %v", status)
}

return nil
}
33 changes: 33 additions & 0 deletions r_put_store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package easclient_test

import (
"context"
"testing"

"github.com/DEXPRO-Solutions-GmbH/easclient"
"github.com/stretchr/testify/require"
)

func TestStoreClient_PutStore(t *testing.T) {
testPrelude(t)

ctx := context.Background()
user := easclient.NewUserClaims("[email protected]")
ctx = user.SetOnContext(ctx)

storeName := "random-store"

err := DefaultServerClient.PutStore(ctx, storeName, &easclient.PutStoreRequest{
ConfigurationTemplate: easclient.ConfigurationTemplate{
Name: "default",
Parameters: []easclient.ConfigurationParameter{
{
Name: "STORE_NAME",
Value: storeName,
},
},
},
})

require.NoError(t, err)
}
11 changes: 11 additions & 0 deletions server_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package easclient

import "gopkg.in/resty.v1"

type ServerClient struct {
c *resty.Client
}

func NewServerClient(c *resty.Client) *ServerClient {
return &ServerClient{c: c}
}
6 changes: 5 additions & 1 deletion store_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ func NewStoreClient(c *resty.Client) *StoreClient {
}

func (c *StoreClient) newRequest(ctx context.Context) (*resty.Request, error) {
return newRequest(ctx, c.c)
}

func newRequest(ctx context.Context, c *resty.Client) (*resty.Request, error) {
claims := UserClaimsFromContext(ctx)
if claims == nil {
return nil, errors.New("missing user claims in context object")
}

req := c.c.NewRequest()
req := c.NewRequest()
req.SetContext(ctx)
req.SetHeader("Accept", "application/json")
claims.SetOnHeader(req.Header)
Expand Down

0 comments on commit 1e5ab62

Please sign in to comment.