-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1472e3f
Showing
4 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
name: Go package | ||
|
||
on: [push] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
go-version: ['1.19.x'] | ||
|
||
steps: | ||
# setup actions matrix | ||
- uses: actions/checkout@v3 | ||
- name: Setup Go ${{matrix.go-version}} | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: ${{matrix.go-version}} | ||
cache: true | ||
|
||
# show version & install dependencies | ||
- name: Show Go version & Install dependencies | ||
run: | | ||
go version | ||
go get . | ||
# build Go application | ||
- name: Build | ||
run: go build -v ./... | ||
|
||
# test Go application | ||
- name: Test with Go | ||
run: go test -v ./... -json > TestResults-${{matrix.go-version}}.json | ||
|
||
# upload Go test results | ||
- name: Upload Go test results | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: Go-results-${{matrix.go-version}} | ||
path: TestResults-${{matrix.go-version}}.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package fetch | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/goccy/go-json" | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
type RequestHeader struct { | ||
Method string `json:"Method,omitempty"` | ||
Body interface{} `json:"Body,omitempty"` | ||
Header []string `json:"Header"` | ||
Authorization string `json:"Authorization,omitempty"` | ||
Agent *fiber.Agent | ||
} | ||
|
||
type Respnse struct { | ||
Status int | ||
Data interface{} | ||
Error error | ||
} | ||
|
||
func Method(method string) *RequestHeader { | ||
return &RequestHeader{ | ||
Method: method, | ||
} | ||
} | ||
|
||
func (header *RequestHeader) SetAuthorization(auth string) *RequestHeader { | ||
header.Authorization = auth | ||
return header | ||
} | ||
|
||
func stringInSlice(a string, list []string) bool { | ||
for _, b := range list { | ||
if b == a { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (header *RequestHeader) FiberFetch(url string) *Respnse { | ||
|
||
// FastHttp agent | ||
agent := (map[bool]*fiber.Agent{ | ||
true: header.Agent, | ||
false: fiber.AcquireAgent(), | ||
})[header.Agent != nil] | ||
|
||
// Prepare request | ||
req := agent.Request() | ||
req.Header.SetMethod(header.Method) | ||
req.SetRequestURI(url) | ||
|
||
// body | ||
if header.Body != nil && !stringInSlice(header.Method, []string{"GET", "HEAD"}) { | ||
payload, _ := json.Marshal(header.Body) | ||
agent.Body(payload) | ||
} | ||
// authorization | ||
if header.Authorization != "" { | ||
agent.Set("Authorization", header.Authorization) | ||
} | ||
// headers | ||
if len(header.Header) > 0 { | ||
for _, v := range header.Header { | ||
h := strings.Split(v, "=") | ||
if len(h) == 2 { | ||
agent.Set(h[0], h[1]) | ||
} | ||
} | ||
} | ||
|
||
// Execute | ||
if err := agent.Parse(); err != nil { | ||
return &Respnse{} | ||
} | ||
|
||
// Parse | ||
code, body, errs := agent.Bytes() | ||
if len(errs) > 0 { | ||
var errStr string | ||
for _, v := range errs { | ||
errStr += v.Error() + "; " | ||
} | ||
if code == 0 { | ||
code = 500 | ||
} | ||
return &Respnse{Status: code, Error: errors.New(errStr)} | ||
} | ||
|
||
if code >= 400 && body != nil { | ||
return &Respnse{Status: code, Error: fmt.Errorf("error: %s", string(body))} | ||
} | ||
|
||
return &Respnse{Status: code, Data: body} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module github.com/yngfoxx/gofiber-fetch | ||
|
||
go 1.19 | ||
|
||
require ( | ||
github.com/goccy/go-json v0.9.11 | ||
github.com/gofiber/fiber/v2 v2.39.0 | ||
) | ||
|
||
require ( | ||
github.com/andybalholm/brotli v1.0.4 // indirect | ||
github.com/klauspost/compress v1.15.0 // indirect | ||
github.com/mattn/go-colorable v0.1.13 // indirect | ||
github.com/mattn/go-isatty v0.0.16 // indirect | ||
github.com/mattn/go-runewidth v0.0.14 // indirect | ||
github.com/rivo/uniseg v0.2.0 // indirect | ||
github.com/valyala/bytebufferpool v1.0.0 // indirect | ||
github.com/valyala/fasthttp v1.40.0 // indirect | ||
github.com/valyala/tcplisten v1.0.0 // indirect | ||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY= | ||
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= | ||
github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk= | ||
github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= | ||
github.com/gofiber/fiber/v2 v2.39.0 h1:uhWpYQ6EHN8J7FOPYbI2hrdBD/KNZBC5CjbuOd4QUt4= | ||
github.com/gofiber/fiber/v2 v2.39.0/go.mod h1:Cmuu+elPYGqlvQvdKyjtYsjGMi69PDp8a1AY2I5B2gM= | ||
github.com/klauspost/compress v1.15.0 h1:xqfchp4whNFxn5A4XFyyYtitiWI8Hy5EW59jEwcyL6U= | ||
github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= | ||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= | ||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= | ||
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= | ||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= | ||
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= | ||
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= | ||
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= | ||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= | ||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= | ||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= | ||
github.com/valyala/fasthttp v1.40.0 h1:CRq/00MfruPGFLTQKY8b+8SfdK60TxNztjRMnH0t1Yc= | ||
github.com/valyala/fasthttp v1.40.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I= | ||
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= | ||
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= | ||
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= | ||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= | ||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= | ||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU= | ||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | ||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= | ||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= |