This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Persist a record in the repository #109
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -29,9 +29,9 @@ $ go get github.com/sul-dlss-labs/taco | |
|
||
## Running the Go Code locally without a build | ||
|
||
|
||
```shell | ||
$ go run main.go | ||
$ cd cmd/tacod | ||
$ AWS_ACCESS_KEY_ID=999999 AWS_SECRET_KEY=1231 go run main.go | ||
``` | ||
|
||
## Building to TACO Binary | ||
|
@@ -70,16 +70,26 @@ $ awslocal dynamodb create-table --table-name resources \ | |
--provisioned-throughput=ReadCapacityUnits=100,WriteCapacityUnits=100 | ||
``` | ||
|
||
And add a stub record: | ||
``` | ||
$ awslocal dynamodb put-item --table-name resources --item '{"id": {"S":"99"}, "title":{"S":"Ta-da!"}}' | ||
Now start the API server: | ||
```shell | ||
% AWS_ACCESS_KEY_ID=999999 AWS_SECRET_KEY=1231 ./tacod | ||
``` | ||
|
||
Then you can interact with it using `curl`: | ||
```shell | ||
% TACO_ENV=production AWS_ACCESS_KEY_ID=999999 AWS_SECRET_KEY=1231 ./tacod | ||
curl -X POST -H "Content-Type: application/json" -d '{"title":"value1", "sourceId":"value2"}' http://localhost:8080/v1/resource | ||
``` | ||
|
||
Now visit: http://localhost:8080/v1/resource/99 | ||
it will return a response like: | ||
```json | ||
{"id":"fe1f66a9-5285-4b28-8240-0482c8fff6c7"} | ||
``` | ||
|
||
Then you can use the returned identifier to retrieve the original: | ||
|
||
```shell | ||
curl -H "Content-Type: application/json" http://localhost:8080/v1/resource/fe1f66a9-5285-4b28-8240-0482c8fff6c7 | ||
``` | ||
|
||
## API Code Structure | ||
|
||
|
@@ -104,12 +114,6 @@ go install | |
|
||
Do this prior to generating code. | ||
|
||
### To run the API code | ||
|
||
```shell | ||
$ go run main.go | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This already exists on line 34, so removing. |
||
``` | ||
|
||
### Non-generated code | ||
|
||
The API code generation does **not** touch the following, which we are writing locally: | ||
|
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
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
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
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 |
---|---|---|
@@ -1,25 +1,55 @@ | ||
package handlers | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/go-openapi/runtime/middleware" | ||
"github.com/google/uuid" | ||
"github.com/sul-dlss-labs/taco" | ||
"github.com/sul-dlss-labs/taco/generated/models" | ||
"github.com/sul-dlss-labs/taco/generated/restapi/operations" | ||
"github.com/sul-dlss-labs/taco/persistence" | ||
) | ||
|
||
// NewDepositResource -- Accepts requests to create resource and pushes them to Kinesis. | ||
func NewDepositResource(rt *taco.Runtime) operations.DepositNewResourceHandler { | ||
return &depositResourceEntry{} | ||
return &depositResourceEntry{rt: rt} | ||
} | ||
|
||
type depositResourceEntry struct{} | ||
type depositResourceEntry struct { | ||
rt *taco.Runtime | ||
} | ||
|
||
// Handle the delete entry request | ||
func (d *depositResourceEntry) Handle(params operations.DepositNewResourceParams) middleware.Responder { | ||
// TODO: This should be a DRUID | ||
resourceID, _ := uuid.NewRandom() | ||
|
||
response := &models.DepositNewResourceOKBody{ID: resourceID.String()} | ||
resourceID := mintID() | ||
|
||
if err := d.persistResource(resourceID, params); err != nil { | ||
// TODO: handle this with an error response | ||
panic(err) | ||
} | ||
|
||
response := &models.DepositNewResourceOKBody{ID: resourceID} | ||
return operations.NewDepositNewResourceOK().WithPayload(response) | ||
} | ||
|
||
func (d *depositResourceEntry) persistResource(resourceID string, params operations.DepositNewResourceParams) error { | ||
resource := d.persistableResourceFromParams(resourceID, params) | ||
fmt.Println("Saving") | ||
return d.rt.Repository().SaveItem(resource) | ||
} | ||
|
||
func (d *depositResourceEntry) persistableResourceFromParams(resourceID string, params operations.DepositNewResourceParams) *persistence.Resource { | ||
resource := &persistence.Resource{ID: resourceID} | ||
// TODO: Expand this mapping: | ||
resource.Title = *params.Payload.Title | ||
resource.SourceID = *params.Payload.SourceID | ||
return resource | ||
} | ||
|
||
func mintID() string { | ||
// TODO: This should be a DRUID | ||
resourceID, _ := uuid.NewRandom() | ||
return resourceID.String() | ||
} |
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,71 @@ | ||
package handlers | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/appleboy/gofight" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/sul-dlss-labs/taco/persistence" | ||
) | ||
|
||
func mockErrorRepo() persistence.Repository { | ||
return &fakeErroringRepository{} | ||
} | ||
|
||
type fakeErroringRepository struct{} | ||
|
||
func (f *fakeErroringRepository) GetByID(id string) (*persistence.Resource, error) { | ||
return nil, nil | ||
} | ||
|
||
func (f *fakeErroringRepository) SaveItem(resource *persistence.Resource) error { | ||
return errors.New("not found") | ||
} | ||
|
||
func TestCreateResourceHappyPath(t *testing.T) { | ||
r := gofight.New() | ||
repo := mockRepo(nil) | ||
|
||
r.POST("/v1/resource"). | ||
SetJSON(gofight.D{ | ||
"id": "oo000oo0001", | ||
"sourceId": "bib12345678", | ||
"title": "My work", | ||
}). | ||
Run(setupFakeRuntime(repo), | ||
func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { | ||
assert.Equal(t, http.StatusOK, r.Code) | ||
assert.Equal(t, 1, len(repo.(*fakeRepository).CreatedResources)) | ||
|
||
}) | ||
} | ||
|
||
func TestCreateResourceMissingSourceId(t *testing.T) { | ||
r := gofight.New() | ||
r.POST("/v1/resource"). | ||
SetJSON(gofight.D{ | ||
"id": "oo000oo0001", | ||
"title": "My work", | ||
}). | ||
Run(setupFakeRuntime(mockRepo(nil)), | ||
func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { | ||
assert.Equal(t, http.StatusUnprocessableEntity, r.Code) | ||
}) | ||
} | ||
|
||
// TODO: Handle errors | ||
// func TestCreateResourceFailure(t *testing.T) { | ||
// r := gofight.New() | ||
// r.POST("/v1/resource"). | ||
// SetJSON(gofight.D{ | ||
// "id": "oo000oo0001", | ||
// "sourceId": "bib12345678", | ||
// "title": "My work", | ||
// }). | ||
// Run(setupFakeRuntime(mockErrorRepo()), | ||
// func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { | ||
// assert.Equal(t, http.StatusUnprocessableEntity, r.Code) | ||
// }) | ||
// } |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is an old version of the awscli that will prob bite you down the road. The preferred way to install it is in the
deploy_to_dev
job below. This can get changed later though, so not blocking this PR for that change