Skip to content

Commit

Permalink
Merge pull request #7 from hashicorp-demoapp/add-product-functionality
Browse files Browse the repository at this point in the history
Make public API compatible with redesigned frontend
  • Loading branch information
im2nguyen authored Feb 14, 2022
2 parents d23c3f8 + ffd2f38 commit a576419
Show file tree
Hide file tree
Showing 27 changed files with 1,844 additions and 340 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=v0.0.4
VERSION=v0.0.7
REPOSITORY=hashicorpdemoapp/public-api

.PHONY: auth
Expand Down
18 changes: 9 additions & 9 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ func Middleware(auth *authn.Client) func(http.Handler) http.Handler {
ctx := r.Context()
authorization := r.Header.Get("Authorization")
token := strings.TrimPrefix(authorization, "Bearer ")
decodedToken, err := auth.SubjectFrom(token)
if err != nil {
next.ServeHTTP(w, r)
return
}
// decodedToken, err := auth.SubjectFrom(token)
// if err != nil {
// next.ServeHTTP(w, r)
// return
// }

ctx = context.WithValue(ctx, userCtxKey, decodedToken)
ctx = context.WithValue(ctx, userCtxKey, token)
r = r.WithContext(ctx)
next.ServeHTTP(w, r)
})
}
}

// IsAuthenticated returns wether or not the user is authenticated.
// GetAuthHeader returns the auth header.
// REQUIRES Middleware to have run.
func IsAuthenticated(ctx context.Context) bool {
return ctx.Value(userCtxKey) != nil
func GetAuthHeader(ctx context.Context) string {
return ctx.Value(userCtxKey).(string)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/HdrHistogram/hdrhistogram-go v1.1.2 // indirect
github.com/gorilla/mux v1.8.0
github.com/hashicorp-demoapp/go-hckit v0.0.2
github.com/hashicorp-demoapp/hashicups-client-go v0.0.0-20220214055102-bd8bf734c61c // indirect
github.com/hashicorp-demoapp/product-api-go v0.0.12
github.com/hashicorp/go-hclog v0.14.1
github.com/keratin/authn-go v1.1.0
Expand Down
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,24 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t
github.com/hashicorp-demoapp/go-hckit v0.0.1/go.mod h1:FwfwIzjNELljJiAqnah7UHPzTcUKKtvxDH9OFmS429g=
github.com/hashicorp-demoapp/go-hckit v0.0.2 h1:mQ02OCFzxlCv0pv9zeAZDwAp7esCRYn99FhJ+QuOCE0=
github.com/hashicorp-demoapp/go-hckit v0.0.2/go.mod h1:mhJOg3le42ccvVkMx1+uhflTHZK0bZ/rBiUbKYAYJnQ=
github.com/hashicorp-demoapp/hashicups-client-go v0.0.0-20210721190446-1df90c457bd4 h1:Rt8GW22FYXFLaY9cnfBfPSg9kAngQbagHm31wNoSbBU=
github.com/hashicorp-demoapp/hashicups-client-go v0.0.0-20210721190446-1df90c457bd4/go.mod h1:fJF8CZhWlImByx49t7RZvuoxskStDwqIWi5/GOSJqGI=
github.com/hashicorp-demoapp/hashicups-client-go v0.0.0-20220208203135-614ee775d01c h1:dCTj/4XRUbrAEENxHnT2GVohlrFz3MFNSxDLMzJ0kUY=
github.com/hashicorp-demoapp/hashicups-client-go v0.0.0-20220208203135-614ee775d01c/go.mod h1:fJF8CZhWlImByx49t7RZvuoxskStDwqIWi5/GOSJqGI=
github.com/hashicorp-demoapp/hashicups-client-go v0.0.0-20220208203428-b72abc3e0894 h1:jDGmNlJGrJCumHssD1Z7pTwHDYZ91xHMetwQltY2WR4=
github.com/hashicorp-demoapp/hashicups-client-go v0.0.0-20220208203428-b72abc3e0894/go.mod h1:fJF8CZhWlImByx49t7RZvuoxskStDwqIWi5/GOSJqGI=
github.com/hashicorp-demoapp/hashicups-client-go v0.0.0-20220211051608-4ba79fab01a9 h1:xYyLQLWCStYTI+NtYTO/aGPHLmPvyk9KWYzGhTk0HpQ=
github.com/hashicorp-demoapp/hashicups-client-go v0.0.0-20220211051608-4ba79fab01a9/go.mod h1:fJF8CZhWlImByx49t7RZvuoxskStDwqIWi5/GOSJqGI=
github.com/hashicorp-demoapp/hashicups-client-go v0.0.0-20220211052040-21835c69ed44 h1:4rS2ePH6mvp/fW9lal9rAAR9Zz0aOUua77Xw1JNii6Q=
github.com/hashicorp-demoapp/hashicups-client-go v0.0.0-20220211052040-21835c69ed44/go.mod h1:fJF8CZhWlImByx49t7RZvuoxskStDwqIWi5/GOSJqGI=
github.com/hashicorp-demoapp/hashicups-client-go v0.0.0-20220211172642-60f047bae1f4 h1:Z4OFB/SwU3ei3QhKZkRkghuXNvTObvCllWB6D+JExhw=
github.com/hashicorp-demoapp/hashicups-client-go v0.0.0-20220211172642-60f047bae1f4/go.mod h1:fJF8CZhWlImByx49t7RZvuoxskStDwqIWi5/GOSJqGI=
github.com/hashicorp-demoapp/hashicups-client-go v0.0.0-20220211213457-9e52722e25b1 h1:02T2BaoVkctOcoyeNgUuHqeJW57KYT1Idg1frrOVqck=
github.com/hashicorp-demoapp/hashicups-client-go v0.0.0-20220211213457-9e52722e25b1/go.mod h1:fJF8CZhWlImByx49t7RZvuoxskStDwqIWi5/GOSJqGI=
github.com/hashicorp-demoapp/hashicups-client-go v0.0.0-20220212064447-a6eeb2e9fc33 h1:ZMEAOIn5fGdUy94aJjUK1IN8khwhLQUhjvgTpW1Okro=
github.com/hashicorp-demoapp/hashicups-client-go v0.0.0-20220212064447-a6eeb2e9fc33/go.mod h1:fJF8CZhWlImByx49t7RZvuoxskStDwqIWi5/GOSJqGI=
github.com/hashicorp-demoapp/hashicups-client-go v0.0.0-20220214055102-bd8bf734c61c h1:GYXwdLpP+C204jWcLJ5yCEf9Hxu6+BbQ3HacwT/ZFLM=
github.com/hashicorp-demoapp/hashicups-client-go v0.0.0-20220214055102-bd8bf734c61c/go.mod h1:fJF8CZhWlImByx49t7RZvuoxskStDwqIWi5/GOSJqGI=
github.com/hashicorp-demoapp/product-api-go v0.0.12 h1:16wyzf/eHekzUQ9cljDd51K9OTl4wiv78Pghz97Z3QU=
github.com/hashicorp-demoapp/product-api-go v0.0.12/go.mod h1:OpIaKtmQfK6RGHOuZqTn/6XxqVOhXg0dlTaQyWuLtqY=
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
Expand Down
13 changes: 8 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/handler"
"github.com/hashicorp-demoapp/go-hckit"
"github.com/hashicorp-demoapp/product-api-go/client"
"github.com/hashicorp-demoapp/hashicups-client-go"
"github.com/hashicorp-demoapp/public-api/auth"
"github.com/hashicorp-demoapp/public-api/models"
"github.com/hashicorp-demoapp/public-api/payments"
Expand Down Expand Up @@ -92,7 +92,10 @@ func main() {
}).Handler)

// create the client to the products-api
productsClient := client.NewHTTP(*productAddress)
productsClient, err := hashicups.NewClient(productAddress, nil, nil)
if err != nil {
log.Fatal(err)
}

// create the client for the payments-api
paymentClient := payments.NewHTTP(*paymentAddress)
Expand All @@ -104,9 +107,9 @@ func main() {

// Check if the user is authenticated.
c.Directives.IsAuthenticated = func(ctx context.Context, obj interface{}, next graphql.Resolver) (interface{}, error) {
isAuthenticated := auth.IsAuthenticated(ctx)
if !isAuthenticated {
return nil, fmt.Errorf("Access denied")
authHeader := auth.GetAuthHeader(ctx)
if authHeader == "" {
return nil, fmt.Errorf("Authorization header missing")
}

return next(ctx)
Expand Down
11 changes: 7 additions & 4 deletions models/coffee.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package models
import (
"strconv"

productsapi "github.com/hashicorp-demoapp/product-api-go/data/model"
"github.com/hashicorp-demoapp/hashicups-client-go"
)

// CoffeeFromProductsAPI is an adaptor function which converts the products api model
// into the local model
func CoffeeFromProductsAPI(cof []productsapi.Coffee) ([]*Coffee, error) {
func CoffeeFromProductsAPI(cof []hashicups.Coffee) ([]*Coffee, error) {
pc := make([]*Coffee, 0)

for i, co := range cof {
Expand All @@ -18,15 +18,18 @@ func CoffeeFromProductsAPI(cof []productsapi.Coffee) ([]*Coffee, error) {
Name: &cof[i].Name,
Price: &price,
Teaser: &cof[i].Teaser,
Collection: &cof[i].Collection,
Origin: &cof[i].Origin,
Color: &cof[i].Color,
Description: &cof[i].Description,
Image: &cof[i].Image,
}

// add the ingredients
ins := make([]*Ingredient, 0)
for _, i := range co.Ingredients {
for _, i := range co.Ingredient {
ins = append(ins, &Ingredient{
ID: strconv.Itoa(i.IngredientID),
ID: strconv.Itoa(i.ID),
})
}

Expand Down
46 changes: 26 additions & 20 deletions models/coffee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,56 @@ import (
"strconv"
"testing"

productsapi "github.com/hashicorp-demoapp/product-api-go/data/model"
"github.com/hashicorp-demoapp/hashicups-client-go"
"github.com/stretchr/testify/assert"
)

func TestConvertsFromProductsAPI(t *testing.T) {
c, err := CoffeeFromProductsAPI(apiModel)
func TestCoffeeConvertsFromProductsAPI(t *testing.T) {
c, err := CoffeeFromProductsAPI(apiCoffeeModel)
assert.NoError(t, err)

assert.Len(t, c, 2)
assert.Equal(t, strconv.Itoa(apiModel[0].ID), c[0].ID)
assert.Equal(t, apiModel[0].Name, *c[0].Name)
assert.Equal(t, apiModel[0].Teaser, *c[0].Teaser)
assert.Equal(t, apiModel[0].Description, *c[0].Description)
assert.Equal(t, float64(apiModel[0].Price), *c[0].Price)
assert.Equal(t, strconv.Itoa(apiCoffeeModel[0].ID), c[0].ID)
assert.Equal(t, apiCoffeeModel[0].Name, *c[0].Name)
assert.Equal(t, apiCoffeeModel[0].Teaser, *c[0].Teaser)
assert.Equal(t, apiCoffeeModel[0].Description, *c[0].Description)
assert.Equal(t, float64(apiCoffeeModel[0].Price), *c[0].Price)

assert.Len(t, c[0].Ingredients, 2)
assert.Equal(t, strconv.Itoa(apiModel[0].Ingredients[0].IngredientID), c[0].Ingredients[0].ID)
assert.Equal(t, strconv.Itoa(apiCoffeeModel[0].Ingredient[0].ID), c[0].Ingredients[0].ID)
}

var apiModel = []productsapi.Coffee{
productsapi.Coffee{
var apiCoffeeModel = []hashicups.Coffee{
{
ID: 123,
Name: "Latte",
Teaser: "This is a teaser",
Collection: "Origins",
Origin: "Summer 2014",
Color: "#FFF",
Description: "This is the description",
Price: 220,
Ingredients: []productsapi.CoffeeIngredients{
productsapi.CoffeeIngredients{
IngredientID: 1,
Ingredient: []hashicups.CoffeeIngredient{
{
ID: 1,
},
productsapi.CoffeeIngredients{
IngredientID: 2,
{
ID: 2,
},
},
},
productsapi.Coffee{
{
ID: 125,
Name: "Espresso",
Teaser: "This is a teaser",
Collection: "Foundations",
Origin: "Fall 2015",
Color: "#000",
Description: "This is the description",
Price: 200,
Ingredients: []productsapi.CoffeeIngredients{
productsapi.CoffeeIngredients{
IngredientID: 1,
Ingredient: []hashicups.CoffeeIngredient{
{
ID: 1,
},
},
},
Expand Down
25 changes: 25 additions & 0 deletions models/ingredient.go
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
package models

import (
"strconv"

"github.com/hashicorp-demoapp/hashicups-client-go"
)

// IngredientsFromProductAPI is an adaptor function which converts the
// Ingredients API model into the local model
func IngredientsFromProductAPI(pIngs []hashicups.Ingredient) ([]*Ingredient, error) {
ings := make([]*Ingredient, 0)

for i, pIng := range pIngs {
ing := &Ingredient{
ID: strconv.Itoa(pIng.ID),
Name: &pIngs[i].Name,
Quantity: &pIngs[i].Quantity,
Unit: &pIngs[i].Unit,
}

ings = append(ings, ing)
}

return ings, nil
}
35 changes: 35 additions & 0 deletions models/ingredient_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package models

import (
"strconv"
"testing"

"github.com/hashicorp-demoapp/hashicups-client-go"
"github.com/stretchr/testify/assert"
)

func TestIngredientConvertsFromProductsAPI(t *testing.T) {
i, err := IngredientsFromProductAPI(apiIngredientModel)
assert.NoError(t, err)

assert.Len(t, i, 2)
assert.Equal(t, strconv.Itoa(apiIngredientModel[0].ID), i[0].ID)
assert.Equal(t, apiIngredientModel[0].Name, *i[0].Name)
assert.Equal(t, apiIngredientModel[0].Quantity, *i[0].Quantity)
assert.Equal(t, apiIngredientModel[0].Unit, *i[0].Unit)
}

var apiIngredientModel = []hashicups.Ingredient{
{
ID: 1,
Name: "Espresso",
Quantity: 40,
Unit: "ml",
},
{
ID: 1,
Name: "Semi Skimmed Milk",
Quantity: 20,
Unit: "ml",
},
}
34 changes: 34 additions & 0 deletions models/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a576419

Please sign in to comment.