Skip to content

Commit e9842c9

Browse files
author
Derek Dowling
authored
Merge pull request #34 from bsdlp/merge_jshapi
merge jsh-api into go-json-spec-handler
2 parents eaea1e2 + 7f3d3f5 commit e9842c9

20 files changed

+315
-91
lines changed

Godeps/Godeps.json

-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ sending JSONAPI compatible responses.
9393
Not Implementing:
9494

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

9999
- Routing
@@ -126,10 +126,10 @@ user := &yourUser{}
126126
err := object.Unmarshal("users", user)
127127
```
128128

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

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

_vendor/github.com/derekdowling/jsh-api/.gitignore

-1
This file was deleted.

_vendor/github.com/derekdowling/jsh-api/.travis.yml

-12
This file was deleted.

_vendor/github.com/derekdowling/jsh-api/Godeps/Godeps.json

-55
This file was deleted.

_vendor/github.com/derekdowling/jsh-api/Godeps/Readme

-5
This file was deleted.

client/client_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"golang.org/x/net/context"
1010

1111
"github.com/derekdowling/go-json-spec-handler"
12-
"github.com/derekdowling/jsh-api"
12+
"github.com/derekdowling/go-json-spec-handler/jsh-api"
1313
. "github.com/smartystreets/goconvey/convey"
1414
)
1515

@@ -105,7 +105,7 @@ func TestResponseParsing(t *testing.T) {
105105
}
106106

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

111111
resource := jshapi.NewMockResource("tests", 1, nil)
File renamed without changes.

_vendor/github.com/derekdowling/jsh-api/README.md renamed to jsh-api/README.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# JSH-API
22

3-
[![GoDoc](https://godoc.org/github.com/derekdowling/go-json-spec-handler?status.png)](https://godoc.org/github.com/derekdowling/jsh-api)
4-
[![Build Status](https://travis-ci.org/derekdowling/jsh-api.svg?branch=master)](https://travis-ci.org/derekdowling/jsh-api)
5-
[![Go Report Card](http://goreportcard.com/badge/manyminds/api2go)](http://goreportcard.com/report/derekdowling/jsh-api)
3+
[![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)
64

75
A [JSON API](http://jsonapi.org) specification micro-service builder created on top of
86
[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.
@@ -12,7 +10,7 @@ A [JSON API](http://jsonapi.org) specification micro-service builder created on
1210
The easiest way to get started is like so:
1311

1412
```go
15-
import github.com/derekdowling/jsh-api
13+
import github.com/derekdowling/go-json-spec-handler/jsh-api
1614

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

3331
```go
34-
import github.com/derekdowling/jsh-api
32+
import github.com/derekdowling/go-json-spec-handler/jsh-api
3533

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

103101
## Working With Storage Interfaces
104102

105-
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)
103+
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)
106104
interface for a basic user resource using [jsh](https://godoc.org/github.com/derekdowling/go-json-spec-handler)
107105
for Save and Update. This should give you a pretty good idea of how easy it is to
108106
implement the Storage driver with jsh.
File renamed without changes.

jsh-api/api_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package jshapi
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
8+
"github.com/derekdowling/go-json-spec-handler"
9+
"github.com/derekdowling/go-json-spec-handler/client"
10+
. "github.com/smartystreets/goconvey/convey"
11+
)
12+
13+
const testResourceType = "bars"
14+
15+
func TestAPI(t *testing.T) {
16+
17+
Convey("API Tests", t, func() {
18+
19+
api := New("api")
20+
21+
So(api.prefix, ShouldEqual, "/api")
22+
23+
testAttrs := map[string]string{
24+
"foo": "bar",
25+
}
26+
27+
Convey("->AddResource()", func() {
28+
resource := NewMockResource(testResourceType, 1, testAttrs)
29+
api.Add(resource)
30+
31+
So(api.Resources[testResourceType], ShouldEqual, resource)
32+
33+
server := httptest.NewServer(api)
34+
baseURL := server.URL + api.prefix
35+
36+
Convey("should work with /<resource> routes", func() {
37+
_, resp, err := jsc.List(baseURL, testResourceType)
38+
39+
So(resp.StatusCode, ShouldEqual, http.StatusOK)
40+
So(err, ShouldBeNil)
41+
})
42+
43+
Convey("should work with /<resource>/:id routes", func() {
44+
patchObj, err := jsh.NewObject("1", testResourceType, testAttrs)
45+
So(err, ShouldBeNil)
46+
47+
_, resp, patchErr := jsc.Patch(baseURL, patchObj)
48+
So(resp.StatusCode, ShouldEqual, http.StatusOK)
49+
So(patchErr, ShouldBeNil)
50+
})
51+
})
52+
})
53+
}
File renamed without changes.

_vendor/github.com/derekdowling/jsh-api/resource.go renamed to jsh-api/resource.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"golang.org/x/net/context"
1414

1515
"github.com/derekdowling/go-json-spec-handler"
16-
"github.com/derekdowling/jsh-api/store"
16+
"github.com/derekdowling/go-json-spec-handler/jsh-api/store"
1717
)
1818

1919
const (
@@ -310,6 +310,12 @@ func (res *Resource) patchHandler(ctx context.Context, w http.ResponseWriter, r
310310
return
311311
}
312312

313+
id := pat.Param(ctx, "id")
314+
if id != parsedObject.ID {
315+
SendHandler(ctx, w, r, jsh.InputError("Request ID does not match URL's", "id"))
316+
return
317+
}
318+
313319
object, err := storage(ctx, parsedObject)
314320
if err != nil && reflect.ValueOf(err).IsNil() == false {
315321
SendHandler(ctx, w, r, err)

0 commit comments

Comments
 (0)