Skip to content

Commit

Permalink
Merge pull request #3 from onflow/chasefleming/config
Browse files Browse the repository at this point in the history
Add default url, config, context
  • Loading branch information
chasefleming authored Jul 18, 2023
2 parents 14e46af + b4ef55b commit b6418b8
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 48 deletions.
115 changes: 72 additions & 43 deletions flixkit.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package flixkit

import (
"context"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -58,6 +59,69 @@ type FlowInteractionTemplate struct {
Data Data `json:"data"`
}

type FlixService interface {
GetFlixRaw(ctx context.Context, templateName string) (string, error)
GetFlix(ctx context.Context, templateName string) (*FlowInteractionTemplate, error)
GetFlixByIDRaw(ctx context.Context, templateID string) (string, error)
GetFlixByID(ctx context.Context, templateID string) (*FlowInteractionTemplate, error)
}

type flixServiceImpl struct {
config *Config
}

type Config struct {
FlixServerURL string
}

func NewFlixService(config *Config) FlixService {
if config.FlixServerURL == "" {
config.FlixServerURL = "https://flix.flow.com/v1/templates"
}

return &flixServiceImpl{
config: config,
}
}

func (s *flixServiceImpl) GetFlixRaw(ctx context.Context, templateName string) (string, error) {
url := fmt.Sprintf("%s?name=%s", s.config.FlixServerURL, templateName)
return FetchFlixWithContext(ctx, url)
}

func (s *flixServiceImpl) GetFlix(ctx context.Context, templateName string) (*FlowInteractionTemplate, error) {
template, err := s.GetFlixRaw(ctx, templateName)
if err != nil {
return nil, err
}

parsedTemplate, err := ParseFlix(template)
if err != nil {
return nil, err
}

return parsedTemplate, nil
}

func (s *flixServiceImpl) GetFlixByIDRaw(ctx context.Context, templateID string) (string, error) {
url := fmt.Sprintf("%s/%s", s.config.FlixServerURL, templateID)
return FetchFlixWithContext(ctx, url)
}

func (s *flixServiceImpl) GetFlixByID(ctx context.Context, templateID string) (*FlowInteractionTemplate, error) {
template, err := s.GetFlixByIDRaw(ctx, templateID)
if err != nil {
return nil, err
}

parsedTemplate, err := ParseFlix(template)
if err != nil {
return nil, err
}

return parsedTemplate, nil
}

func (t *FlowInteractionTemplate) IsScript() bool {
return t.Data.Type == "script"
}
Expand Down Expand Up @@ -101,8 +165,13 @@ func ParseFlix(template string) (*FlowInteractionTemplate, error) {
return &flowTemplate, nil
}

func FetchFlix(url string) (string, error) {
resp, err := http.Get(url)
func FetchFlixWithContext(ctx context.Context, url string) (string, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return "", err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
Expand All @@ -117,45 +186,5 @@ func FetchFlix(url string) (string, error) {
return "", err
}

sb := string(body)

return sb, nil
}

func GetFlixRaw(flixBaseURL string, templateName string) (string, error) {
url := fmt.Sprintf("%s?name=%s", flixBaseURL, templateName)
return FetchFlix(url)
}

func GetFlix(flixBaseURL string, templateName string) (*FlowInteractionTemplate, error) {
template, err := GetFlixRaw(flixBaseURL, templateName)
if err != nil {
return nil, err
}

parsedTemplate, err := ParseFlix(template)
if err != nil {
return nil, err
}

return parsedTemplate, nil
}

func GetFlixByIDRaw(flixBaseURL string, templateID string) (string, error) {
url := fmt.Sprintf("%s/%s", flixBaseURL, templateID)
return FetchFlix(url)
}

func GetFlixByID(flixBaseURL string, templateID string) (*FlowInteractionTemplate, error) {
template, err := GetFlixByIDRaw(flixBaseURL, templateID)
if err != nil {
return nil, err
}

parsedTemplate, err := ParseFlix(template)
if err != nil {
return nil, err
}

return parsedTemplate, nil
return string(body), nil
}
20 changes: 15 additions & 5 deletions flixkit_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package flixkit

import (
"context"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -243,7 +244,8 @@ func TestFetchFlix(t *testing.T) {
}))
defer server.Close()

body, err := FetchFlix(server.URL)
ctx := context.Background()
body, err := FetchFlixWithContext(ctx, server.URL)
assert.NoError(err, "GetFlix should not return an error")
assert.Equal("Hello World", body, "GetFlix should return the correct body")
}
Expand All @@ -257,7 +259,9 @@ func TestGetFlixRaw(t *testing.T) {
}))
defer server.Close()

body, err := GetFlixRaw(server.URL, "templateName")
flixService := NewFlixService(&Config{FlixServerURL: server.URL})
ctx := context.Background()
body, err := flixService.GetFlixRaw(ctx, "templateName")
assert.NoError(err, "GetFlixByName should not return an error")
assert.Equal("Hello World", body, "GetFlixByName should return the correct body")
}
Expand All @@ -270,7 +274,9 @@ func TestGetFlix(t *testing.T) {
}))
defer server.Close()

flix, err := GetFlix(server.URL, "templateName")
flixService := NewFlixService(&Config{FlixServerURL: server.URL})
ctx := context.Background()
flix, err := flixService.GetFlix(ctx, "templateName")
assert.NoError(err, "GetParsedFlixByName should not return an error")
assert.NotNil(flix, "GetParsedFlixByName should not return a nil Flix")
assert.Equal(parsedTemplate, flix, "GetParsedFlixByName should return the correct Flix")
Expand All @@ -285,7 +291,9 @@ func TestGetFlixByIDRaw(t *testing.T) {
}))
defer server.Close()

body, err := GetFlixByIDRaw(server.URL, "templateID")
flixService := NewFlixService(&Config{FlixServerURL: server.URL})
ctx := context.Background()
body, err := flixService.GetFlixByIDRaw(ctx, "templateID")
assert.NoError(err, "GetFlixByID should not return an error")
assert.Equal("Hello World", body, "GetFlixByID should return the correct body")
}
Expand All @@ -298,7 +306,9 @@ func TestGetFlixByID(t *testing.T) {
}))
defer server.Close()

flix, err := GetFlixByID(server.URL, "templateID")
flixService := NewFlixService(&Config{FlixServerURL: server.URL})
ctx := context.Background()
flix, err := flixService.GetFlixByID(ctx, "templateID")
assert.NoError(err, "GetParsedFlixByID should not return an error")
assert.NotNil(flix, "GetParsedFlixByID should not return a nil Flix")
assert.Equal(parsedTemplate, flix, "GetParsedFlixByID should return the correct Flix")
Expand Down

0 comments on commit b6418b8

Please sign in to comment.