Skip to content

Commit

Permalink
new middleware fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
lyricat authored and xwjdsh committed Apr 11, 2023
1 parent dadd7b9 commit 2764d39
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
MiddlewareBotasticSearch = "botastic-search"
MiddlewareDuckduckgoSearch = "duckduckgo-search"
MiddlewareIntentRecognition = "intent-recognition"
MiddlewareFetch = "fetch"
)

const (
Expand Down
69 changes: 69 additions & 0 deletions service/middleware/fetch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package middleware

import (
"context"
"fmt"
"io"
"net/http"
"net/url"

"github.com/pandodao/botastic/core"
)

type fetch struct{}

func (m *fetch) Name() string {
return core.MiddlewareFetch
}

type fetchOptions struct {
*generalOptions
URL string `json:"url"`
}

func (m *fetch) ValidateOptions(gopts *generalOptions, opts map[string]any) (any, error) {
options := &fetchOptions{
generalOptions: gopts,
}

if val, ok := opts["url"]; ok {
v, ok := val.(string)
if !ok {
return nil, fmt.Errorf("url must be a string")
}

_, err := url.Parse(v)
if err != nil {
return nil, fmt.Errorf("url is not valid")
}

options.URL = v
}

return options, nil
}

func (m *fetch) Process(ctx context.Context, opts any, input string) (string, error) {
options := opts.(*fetchOptions)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, options.URL, nil)
if err != nil {
return "", err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()

if resp.StatusCode/100 != 2 {
return "", fmt.Errorf("fetch failed with status code: %d", resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}

return string(body), nil
}
1 change: 1 addition & 0 deletions service/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func New(
},
&duckDuckGoSearch{},
&intentRecognition{},
&fetch{},
} {
middlewareMap[m.Name()] = m
}
Expand Down

0 comments on commit 2764d39

Please sign in to comment.