Skip to content

Commit

Permalink
Merge pull request #34 from bsdlp/merge_jshapi
Browse files Browse the repository at this point in the history
merge jsh-api into go-json-spec-handler
  • Loading branch information
Derek Dowling authored Jul 19, 2016
2 parents eaea1e2 + 7f3d3f5 commit e9842c9
Show file tree
Hide file tree
Showing 20 changed files with 315 additions and 91 deletions.
5 changes: 0 additions & 5 deletions Godeps/Godeps.json

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

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ sending JSONAPI compatible responses.
Not Implementing:

* These features aren't handled because they are beyond the scope of what
this module is meant to achieve. See [jshapi](https://github.com/derekdowling/jsh-api)
this module is meant to achieve. See [jshapi](https://github.com/derekdowling/go-json-spec-handler/tree/master/jsh-api)
for a full-fledged API solution that solves many of these problems.

- Routing
Expand Down Expand Up @@ -126,10 +126,10 @@ user := &yourUser{}
err := object.Unmarshal("users", user)
```

### [JSH-API](https://github.com/derekdowling/jsh-api)
### [JSH-API](https://github.com/derekdowling/go-json-spec-handler/tree/master/jsh-api)

If you're looking for a good place to start with a new API, I've since created
[jshapi](https://github.com/derekdowling/jsh-api) which builds on top of [Goji 2](https://goji.io/)
[jshapi](https://github.com/derekdowling/go-json-spec-handler/tree/master/jsh-api) which builds on top of [Goji 2](https://goji.io/)
and `jsh` in order to handle the routing structure that JSON API requires as
well as a number of other useful tools for testing and mocking APIs as you
develop your own projects. JSHAPI is similar in spirit to this project as it
Expand Down
1 change: 0 additions & 1 deletion _vendor/github.com/derekdowling/jsh-api/.gitignore

This file was deleted.

12 changes: 0 additions & 12 deletions _vendor/github.com/derekdowling/jsh-api/.travis.yml

This file was deleted.

55 changes: 0 additions & 55 deletions _vendor/github.com/derekdowling/jsh-api/Godeps/Godeps.json

This file was deleted.

5 changes: 0 additions & 5 deletions _vendor/github.com/derekdowling/jsh-api/Godeps/Readme

This file was deleted.

4 changes: 2 additions & 2 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"golang.org/x/net/context"

"github.com/derekdowling/go-json-spec-handler"
"github.com/derekdowling/jsh-api"
"github.com/derekdowling/go-json-spec-handler/jsh-api"
. "github.com/smartystreets/goconvey/convey"
)

Expand Down Expand Up @@ -105,7 +105,7 @@ func TestResponseParsing(t *testing.T) {
}

// not a great for this, would much rather have it in test_util, but it causes an
// import cycle wit jsh-api
// import cycle with jsh-api
func testAPI() *jshapi.API {

resource := jshapi.NewMockResource("tests", 1, nil)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# JSH-API

[![GoDoc](https://godoc.org/github.com/derekdowling/go-json-spec-handler?status.png)](https://godoc.org/github.com/derekdowling/jsh-api)
[![Build Status](https://travis-ci.org/derekdowling/jsh-api.svg?branch=master)](https://travis-ci.org/derekdowling/jsh-api)
[![Go Report Card](http://goreportcard.com/badge/manyminds/api2go)](http://goreportcard.com/report/derekdowling/jsh-api)
[![GoDoc](https://godoc.org/github.com/derekdowling/go-json-spec-handler/jsh-api?status.png)](https://godoc.org/github.com/derekdowling/go-json-spec-handler/jsh-api)

A [JSON API](http://jsonapi.org) specification micro-service builder created on top of
[jsh](http://github.com/derekdowling/go-json-spec-handler), [Goji](http://goji.io), and [context](https://godoc.org/golang.org/x/net/context) to handle the nitty gritty but predictable (un)wrapping, validating, preparing, and logging necessary for any JSON API written in Go. The rest (storage, and business logic) is up to you.
Expand All @@ -12,7 +10,7 @@ A [JSON API](http://jsonapi.org) specification micro-service builder created on
The easiest way to get started is like so:

```go
import github.com/derekdowling/jsh-api
import github.com/derekdowling/go-json-spec-handler/jsh-api

// implement jshapi/store.CRUD interface and add resource specific middleware via Goji
userStorage := &UserStorage{}
Expand All @@ -31,7 +29,7 @@ http.ListenAndServe("localhost:8000", api)
For a completely custom setup:

```go
import github.com/derekdowling/jsh-api
import github.com/derekdowling/go-json-spec-handler/jsh-api

// manually setup your API
api := jshapi.New("<prefix>")
Expand Down Expand Up @@ -102,7 +100,7 @@ resource.Action("reset", resetAction)

## Working With Storage Interfaces

Below is a basic example of how one might implement parts of a [CRUD Storage](https://godoc.org/github.com/derekdowling/jsh-api/store#CRUD)
Below is a basic example of how one might implement parts of a [CRUD Storage](https://godoc.org/github.com/derekdowling/go-json-spec-handler/tree/master/jsh-api/store#CRUD)
interface for a basic user resource using [jsh](https://godoc.org/github.com/derekdowling/go-json-spec-handler)
for Save and Update. This should give you a pretty good idea of how easy it is to
implement the Storage driver with jsh.
Expand Down
File renamed without changes.
53 changes: 53 additions & 0 deletions jsh-api/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package jshapi

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/derekdowling/go-json-spec-handler"
"github.com/derekdowling/go-json-spec-handler/client"
. "github.com/smartystreets/goconvey/convey"
)

const testResourceType = "bars"

func TestAPI(t *testing.T) {

Convey("API Tests", t, func() {

api := New("api")

So(api.prefix, ShouldEqual, "/api")

testAttrs := map[string]string{
"foo": "bar",
}

Convey("->AddResource()", func() {
resource := NewMockResource(testResourceType, 1, testAttrs)
api.Add(resource)

So(api.Resources[testResourceType], ShouldEqual, resource)

server := httptest.NewServer(api)
baseURL := server.URL + api.prefix

Convey("should work with /<resource> routes", func() {
_, resp, err := jsc.List(baseURL, testResourceType)

So(resp.StatusCode, ShouldEqual, http.StatusOK)
So(err, ShouldBeNil)
})

Convey("should work with /<resource>/:id routes", func() {
patchObj, err := jsh.NewObject("1", testResourceType, testAttrs)
So(err, ShouldBeNil)

_, resp, patchErr := jsc.Patch(baseURL, patchObj)
So(resp.StatusCode, ShouldEqual, http.StatusOK)
So(patchErr, ShouldBeNil)
})
})
})
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"golang.org/x/net/context"

"github.com/derekdowling/go-json-spec-handler"
"github.com/derekdowling/jsh-api/store"
"github.com/derekdowling/go-json-spec-handler/jsh-api/store"
)

const (
Expand Down Expand Up @@ -310,6 +310,12 @@ func (res *Resource) patchHandler(ctx context.Context, w http.ResponseWriter, r
return
}

id := pat.Param(ctx, "id")
if id != parsedObject.ID {
SendHandler(ctx, w, r, jsh.InputError("Request ID does not match URL's", "id"))
return
}

object, err := storage(ctx, parsedObject)
if err != nil && reflect.ValueOf(err).IsNil() == false {
SendHandler(ctx, w, r, err)
Expand Down
Loading

0 comments on commit e9842c9

Please sign in to comment.