Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
name: Run Tests
on:
push:
branches:
- master
pull_request:
branches:
- master
Expand All @@ -12,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: [ '1.17', '1.15', '1.14', '1.13' ]
go: [ '1.24' ]
name: Go ${{ matrix.go }} tests
steps:
- uses: actions/checkout@v2
Expand All @@ -23,18 +21,16 @@ jobs:
- name: Install dependencies
run: |
go get -t -d -v ./...
go get github.com/onsi/ginkgo/ginkgo
go get -u golang.org/x/lint/golint
go get -u github.com/modocache/gover
go get -u github.com/mattn/goveralls
go get -u github.com/onsi/ginkgo/ginkgo
go install github.com/onsi/ginkgo/ginkgo
go install honnef.co/go/tools/cmd/staticcheck@latest
- name: Run tests
run: |
ginkgo -r -cover --randomizeAllSpecs --randomizeSuites --failOnPending --trace --race --progress
ginkgo -tags=gorillamux -r --randomizeSuites --failOnPending --trace --race
ginkgo -tags=gingonic -r --randomizeSuites --failOnPending --trace --race
ginkgo -tags=echo -r --randomizeSuites --failOnPending --trace --race
rm examples/examples.coverprofile
- name: Run linter
run: |
bash scripts/fmtpolice
gover
goveralls -coverprofile=gover.coverprofile -repotoken gY90SprlNRGmSMl7MgybLreYa05wUXJTU

4 changes: 2 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -1101,7 +1101,7 @@ func (res *resource) respondWithPagination(obj Responder, info information, stat

func unmarshalRequest(r *http.Request) ([]byte, error) {
defer r.Body.Close()
data, err := ioutil.ReadAll(r.Body)
data, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions api2go_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package api2go

import (
"io"
"log"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"io/ioutil"
"log"

"testing"
)

func TestApi2go(t *testing.T) {
RegisterFailHandler(Fail)
log.SetOutput(ioutil.Discard)
log.SetOutput(io.Discard)
RunSpecs(t, "Api2go Suite")
}
6 changes: 3 additions & 3 deletions api_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ type ObjectInitializer interface {
InitializeObject(interface{})
}

//URLResolver allows you to implement a static
//way to return a baseURL for all incoming
//requests for one api2go instance.
// URLResolver allows you to implement a static
// way to return a baseURL for all incoming
// requests for one api2go instance.
type URLResolver interface {
GetBaseURL() string
}
Expand Down
15 changes: 3 additions & 12 deletions api_public.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api2go

import (
"net/http"
"strings"
"sync"

"github.com/manyminds/api2go/jsonapi"
Expand All @@ -24,12 +23,12 @@ type API struct {
}

// Handler returns the http.Handler instance for the API.
func (api API) Handler() http.Handler {
func (api *API) Handler() http.Handler {
return api.router.Handler()
}

//Router returns the specified router on an api instance
func (api API) Router() routing.Routeable {
// Router returns the specified router on an api instance
func (api *API) Router() routing.Routeable {
return api.router
}

Expand Down Expand Up @@ -105,14 +104,6 @@ func NewAPIWithRouting(prefix string, resolver URLResolver, router routing.Route

// newAPI is now an internal method that can be changed if params are changing
func newAPI(prefix string, resolver URLResolver, router routing.Routeable) *API {
// Add initial and trailing slash to prefix
prefixSlashes := strings.Trim(prefix, "/")
if len(prefixSlashes) > 0 {
prefixSlashes = "/" + prefixSlashes + "/"
} else {
prefixSlashes = "/"
}

info := information{prefix: prefix, resolver: resolver}

api := &API{
Expand Down
15 changes: 6 additions & 9 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import (
const testPrefix = "v1"

type requestURLResolver struct {
r http.Request
calls int
r http.Request
}

func (m requestURLResolver) GetBaseURL() string {
Expand Down Expand Up @@ -866,7 +865,7 @@ var _ = Describe("RestHandler", func() {
api.Handler().ServeHTTP(rec, req)
// It's up to the user how to implement this. Api2go just checks if the type is correct
Expect(rec.Code).To(Equal(http.StatusConflict))
Expect(string(rec.Body.Bytes())).To(MatchJSON(`{"errors":[{"status":"409","title":"id in the resource does not match servers endpoint"}]}`))
Expect(rec.Body.String()).To(MatchJSON(`{"errors":[{"status":"409","title":"id in the resource does not match servers endpoint"}]}`))
})

It("POST without type returns 406", func() {
Expand All @@ -875,7 +874,7 @@ var _ = Describe("RestHandler", func() {
Expect(err).To(BeNil())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusNotAcceptable))
Expect(string(rec.Body.Bytes())).To(MatchJSON(`{"errors":[{"status":"406","title":"invalid record, no type was specified"}]}`))
Expect(rec.Body.String()).To(MatchJSON(`{"errors":[{"status":"406","title":"invalid record, no type was specified"}]}`))

})

Expand Down Expand Up @@ -904,7 +903,7 @@ var _ = Describe("RestHandler", func() {
Expect(err).To(BeNil())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusConflict))
Expect(string(rec.Body.Bytes())).To(MatchJSON(`{"errors":[{"status":"409","title":"id in the resource does not match servers endpoint"}]}`))
Expect(rec.Body.String()).To(MatchJSON(`{"errors":[{"status":"409","title":"id in the resource does not match servers endpoint"}]}`))
})

It("UPDATEs correctly using null.* values", func() {
Expand Down Expand Up @@ -1075,7 +1074,7 @@ var _ = Describe("RestHandler", func() {
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusBadRequest))
expected := `{"errors":[{"id":"SomeErrorID","source":{"pointer":"Title"}}]}`
actual := strings.TrimSpace(string(rec.Body.Bytes()))
actual := strings.TrimSpace(rec.Body.String())
Expect(actual).To(Equal(expected))
})
})
Expand Down Expand Up @@ -1428,9 +1427,7 @@ var _ = Describe("RestHandler", func() {
})

Context("test utility function getPointerToStruct", func() {
type someStruct struct {
someEntry string
}
type someStruct struct{}

It("Should work as expected", func() {
testItem := someStruct{}
Expand Down
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var _ APIContexter = &APIContext{}
// ContextQueryParams fetches the QueryParams if Set
func ContextQueryParams(c *APIContext) map[string][]string {
qp, ok := c.Get("QueryParams")
if ok == false {
if !ok {
qp = make(map[string][]string)
c.Set("QueryParams", qp)
}
Expand Down
13 changes: 13 additions & 0 deletions examples/crud_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,51 @@ To play with this example server you can run some of the following curl requests
In order to demonstrate dynamic baseurl handling for requests, apply the --header="REQUEST_URI:https://www.your.domain.example.com" parameter to any of the commands.

Create a new user:

curl -X POST http://localhost:31415/v0/users -d '{"data" : {"type" : "users" , "attributes": {"user-name" : "marvin"}}}'

List users:

curl -X GET http://localhost:31415/v0/users

List paginated users:

curl -X GET 'http://localhost:31415/v0/users?page\[offset\]=0&page\[limit\]=2'

OR

curl -X GET 'http://localhost:31415/v0/users?page\[number\]=1&page\[size\]=2'

Update:

curl -vX PATCH http://localhost:31415/v0/users/1 -d '{ "data" : {"type" : "users", "id": "1", "attributes": {"user-name" : "better marvin"}}}'

Delete:

curl -vX DELETE http://localhost:31415/v0/users/2

Create a chocolate with the name sweet

curl -X POST http://localhost:31415/v0/chocolates -d '{"data" : {"type" : "chocolates" , "attributes": {"name" : "Ritter Sport", "taste": "Very Good"}}}'

Create a user with a sweet

curl -X POST http://localhost:31415/v0/users -d '{"data" : {"type" : "users" , "attributes": {"user-name" : "marvin"}, "relationships": {"sweets": {"data": [{"type": "chocolates", "id": "1"}]}}}}'

List a users sweets

curl -X GET http://localhost:31415/v0/users/1/sweets

Replace a users sweets

curl -X PATCH http://localhost:31415/v0/users/1/relationships/sweets -d '{"data" : [{"type": "chocolates", "id": "2"}]}'

Add a sweet

curl -X POST http://localhost:31415/v0/users/1/relationships/sweets -d '{"data" : [{"type": "chocolates", "id": "2"}]}'

Remove a sweet

curl -X DELETE http://localhost:31415/v0/users/1/relationships/sweets -d '{"data" : [{"type": "chocolates", "id": "2"}]}'
*/
package main
Expand Down
1 change: 0 additions & 1 deletion examples/model/model_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type User struct {
PasswordHash string `json:"-"`
Chocolates []*Chocolate `json:"-"`
ChocolatesIDs []string `json:"-"`
exists bool
}

// GetID to satisfy jsonapi.MarshalIdentifier interface
Expand Down
10 changes: 5 additions & 5 deletions examples/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import (
"net/http"
)

//RequestURL simply returns
//the request url from REQUEST_URI header
//this should not be done in production applications
// RequestURL simply returns
// the request url from REQUEST_URI header
// this should not be done in production applications
type RequestURL struct {
r http.Request
Port int
}

//SetRequest to implement `RequestAwareResolverInterface`
// SetRequest to implement `RequestAwareResolverInterface`
func (m *RequestURL) SetRequest(r http.Request) {
m.r = r
}

//GetBaseURL implements `URLResolver` interface
// GetBaseURL implements `URLResolver` interface
func (m RequestURL) GetBaseURL() string {
if uri := m.r.Header.Get("REQUEST_URI"); uri != "" {
return uri
Expand Down
2 changes: 1 addition & 1 deletion examples/resource/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (s UserResource) Delete(id string, r api2go.Request) (api2go.Responder, err
return &Response{Code: http.StatusNoContent}, err
}

//Update stores all changes on the user
// Update stores all changes on the user
func (s UserResource) Update(obj interface{}, r api2go.Request) (api2go.Responder, error) {
user, ok := obj.(model.User)
if !ok {
Expand Down
53 changes: 43 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,21 +1,54 @@
module github.com/manyminds/api2go

go 1.14
go 1.24

toolchain go1.24.1

require (
github.com/gedex/inflector v0.0.0-20170307190818-16278e9db813
github.com/gin-gonic/gin v1.6.2
github.com/gorilla/mux v1.7.4
github.com/gin-gonic/gin v1.10.0
github.com/gorilla/mux v1.8.1
github.com/julienschmidt/httprouter v1.3.0
github.com/labstack/echo v3.3.10+incompatible
github.com/labstack/gommon v0.3.0 // indirect
github.com/mattn/goveralls v0.0.11 // indirect
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5 // indirect
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.10.1
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/mod v0.5.1 // indirect
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d // indirect
golang.org/x/tools v0.1.7 // indirect
gopkg.in/guregu/null.v3 v3.4.0
)

require (
github.com/bytedance/sonic v1.13.2 // indirect
github.com/bytedance/sonic/loader v0.2.4 // indirect
github.com/cloudwego/base64x v0.1.5 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/gabriel-vasile/mimetype v1.4.9 // indirect
github.com/gin-contrib/sse v1.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.26.0 // indirect
github.com/goccy/go-json v0.10.5 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/arch v0.16.0 // indirect
golang.org/x/crypto v0.37.0 // indirect
golang.org/x/net v0.39.0 // indirect
golang.org/x/sys v0.32.0 // indirect
golang.org/x/text v0.24.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.36.6 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading