Skip to content

Commit

Permalink
Reply-X_Add aides feature (#4)
Browse files Browse the repository at this point in the history
* Refactored status code logic to universal attribute function

* Fix error manifest merging bug

* Updated response aides docstring

* Updated README to include response aides + fixed typo

* Added examples of using aides in simple API example



Co-authored-by: Leon Silcott <lnsilcott+blizzard]@gmail.com>
  • Loading branch information
ooaklee and Leon Silcott committed Sep 5, 2021
1 parent f14bae3 commit 75a59b3
Show file tree
Hide file tree
Showing 4 changed files with 526 additions and 42 deletions.
83 changes: 74 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func ExampleHandler(w http.ResponseWriter, r *http.Request) {

// Pass error to replier's method to return predefined response, else
// 500
_ := replier.NewHTTPResponse(&reply.NewResponseRequest{
_ = replier.NewHTTPResponse(&reply.NewResponseRequest{
Writer: w,
Error: exampleErr,
})
Expand Down Expand Up @@ -89,7 +89,7 @@ func ExampleGetAllHandler(w http.ResponseWriter, r *http.Request) {

// build and sent default formatted JSON response for consumption
// by client
_ := replier.NewHTTPResponse(&reply.NewResponseRequest{
_ = replier.NewHTTPResponse(&reply.NewResponseRequest{
Writer: w,
Data: mockedUsers
StatusCode: htttp.StatusOK
Expand Down Expand Up @@ -192,13 +192,28 @@ func ExampleHandler(w http.ResponseWriter, r *http.Request) {
// error returned
exampleErr := errors.New("example-404-error")

_ := replier.NewHTTPResponse(&reply.NewResponseRequest{
_ = replier.NewHTTPResponse(&reply.NewResponseRequest{
Writer: w,
Error: exampleErr,
})
}
```

For readability and simplicity, you can use the `HTTP error response aide`. You can find a code snippet using this aide below:

```go
// inside of the request handler
_ = replier.NewHTTPErrorResponse(w, exampleErr)
```

You can also add additional `headers` and `meta data` to the response by using the optional `WithHeaders` and/ or `WithMeta` response attributes respectively. For example:

```go
_ = replier.NewHTTPErrorResponse(w, exampleErr, reply.WithMeta(map[string]interface{}{
"example": "meta in error reponse",
}))
```

#### JSON Representation

`Error` responses are returned with the format.
Expand Down Expand Up @@ -241,7 +256,7 @@ func ExampleHandler(w http.ResponseWriter, r *http.Request) {

// do something to get tokens

_ := replier.NewHTTPResponse(&reply.NewResponseRequest{
_ = replier.NewHTTPResponse(&reply.NewResponseRequest{
Writer: w,
AccessToken: "08a0a043-b532-4cea-8117-364739f2d994",
RefreshToken: "08b29914-09a8-4a4a-8aa5-b1ffaff266e6",
Expand All @@ -250,6 +265,23 @@ func ExampleHandler(w http.ResponseWriter, r *http.Request) {
}
```

For readability and simplicity, you can use the `HTTP token response aide`. You can find a code snippet using this aide below:

```go
// inside of the request handler
_ = replier.NewHTTPTokenResponse(w, 200, "08a0a043-b532-4cea-8117-364739f2d994", "08b29914-09a8-4a4a-8aa5-b1ffaff266e6")
```

You can also add additional `headers` and `meta data` to the response by using the optional `WithHeaders` and/ or `WithMeta` response attributes respectively. For example:

```go
_ = replier.NewHTTPErrorResponse(w, 200, "08a0a043-b532-4cea-8117-364739f2d994", "08b29914-09a8-4a4a-8aa5-b1ffaff266e6", reply.WithMeta(map[string]interface{}{
"example": "meta in token reponse",
}))
```

> NOTE: If you only want to return one token, pass an empty string, i.e. `""`. Although, it is important that you give at least one token string.
#### JSON Representation

`Error` responses are returned with the format.
Expand Down Expand Up @@ -303,14 +335,30 @@ func ExampleHandler(w http.ResponseWriter, r *http.Request) {
dob: "1/1/1970",
}

_ := replier.NewHTTPResponse(&reply.NewResponseRequest{
_ = replier.NewHTTPResponse(&reply.NewResponseRequest{
Writer: w,
Data: u,
StatusCode: 201,
})
}
```


For readability and simplicity, you can use the `HTTP data (successful) response aide`. You can find a code snippet using this aide below:

```go
// inside of the request handler
_ = replier.NewHTTPDataResponse(w, 201, u)
```

You can also add additional `headers` and `meta data` to the response by using the optional `WithHeaders` and/ or `WithMeta` response attributes respectively. For example:

```go
_ = replier.NewHTTPDataResponse(w, 201, u, reply.WithMeta(map[string]interface{}{
"example": "meta in data reponse",
}))
```

#### JSON Representation

`Data` responses are returned with the format.
Expand Down Expand Up @@ -342,9 +390,9 @@ When a `meta` is also declared, the response will have the following format. It
}
```

### Default Response Type
### Default (Blank) Response Type

The `default` response returns `"{}"` with a status code of `200` if no `error`, `tokens`, `data` and `status code` is passed. If desired, another `status code` can be specified with `default` responses.
The `default` (blank) response returns `"{}"` with a status code of `200` if no `error`, `tokens`, `data` and `status code` is passed. If desired, another `status code` can be specified with `default` responses.

#### As code

Expand All @@ -355,12 +403,29 @@ replier := reply.NewReplier([]reply.ErrorManifest{})

func ExampleHandler(w http.ResponseWriter, r *http.Request) {

_ := replier.NewHTTPResponse(&reply.NewResponseRequest{
Writer: w,
_ = replier.NewHTTPResponse(&reply.NewResponseRequest{
Writer: w,
StatusCode: 200,
})
}
```

For readability and simplicity, you can use the `HTTP default (blank) response aide`. You can find a code snippet using this aide below:

```go
// inside of the request handler
_ = replier.NewHTTPBlankResponse(w, 200)
```

You can also add additional `headers` and `meta data` to the response by using the optional `WithHeaders` and/ or `WithMeta` response attributes respectively. For example:

```go
_ = replier.NewHTTPBlankResponse(w, 200, reply.WithMeta(map[string]interface{}{
"example": "meta in default reponse",
}))
```


#### JSON Representation

`Default` responses are returned with the format.
Expand Down
78 changes: 73 additions & 5 deletions examples/example_simple_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func simpleUsersAPINotFoundHandler(w http.ResponseWriter, r *http.Request) {
// Do something with a server
serverErr := errors.New("example-404-error")

replier.NewHTTPResponse(&reply.NewResponseRequest{
_ = replier.NewHTTPResponse(&reply.NewResponseRequest{
Writer: w,
Error: serverErr,
})
Expand All @@ -106,7 +106,7 @@ func simpleUsersAPIHandler(w http.ResponseWriter, r *http.Request) {
{ID: 2, Name: "Sam Smith"},
}

replier.NewHTTPResponse(&reply.NewResponseRequest{
_ = replier.NewHTTPResponse(&reply.NewResponseRequest{
Writer: w,
Data: mockedQueriedUsers,
})
Expand All @@ -123,7 +123,7 @@ func simpleUsersAPINoManifestEntryHandler(w http.ResponseWriter, r *http.Request
"correlation-id": "d7c09ac2-fa46-4ece-bcde-1d7ad81d2230",
}

replier.NewHTTPResponse(&reply.NewResponseRequest{
_ = replier.NewHTTPResponse(&reply.NewResponseRequest{
Writer: w,
Error: unregisterdErr,
Headers: mockAdditionalHeaders,
Expand All @@ -135,7 +135,7 @@ func simpleTokensAPIHandler(w http.ResponseWriter, r *http.Request) {
mockedAccessToken := "05e42c11-8bdd-423d-a2c1-c3c5c6604a30"
mockedRefreshToken := "0e95c426-d373-41a5-bfe1-08db322527bd"

replier.NewHTTPResponse(&reply.NewResponseRequest{
_ = replier.NewHTTPResponse(&reply.NewResponseRequest{
Writer: w,
AccessToken: mockedAccessToken,
RefreshToken: mockedRefreshToken,
Expand All @@ -145,7 +145,7 @@ func simpleTokensAPIHandler(w http.ResponseWriter, r *http.Request) {
func simpleAPIDefaultResponseHandler(w http.ResponseWriter, r *http.Request) {

// Do something that only needs an empty response body, and 200 status code
replier.NewHTTPResponse(&reply.NewResponseRequest{
_ = replier.NewHTTPResponse(&reply.NewResponseRequest{
Writer: w,
})
}
Expand All @@ -161,6 +161,67 @@ func simpleUsersAPINotFoundCustomReplierHandler(w http.ResponseWriter, r *http.R
})
}

//////////////////////////////
//// Handlers Using Aides ////

func simpleUsersAPINotFoundUsingAideHandler(w http.ResponseWriter, r *http.Request) {

// Do something with a server
serverErr := errors.New("example-404-error")

_ = replier.NewHTTPErrorResponse(w, serverErr)
}

func simpleUsersAPIUsingAideHandler(w http.ResponseWriter, r *http.Request) {

mockedQueriedUsers := []user{
{ID: 1, Name: "John Doe"},
{ID: 2, Name: "Sam Smith"},
}

_ = replier.NewHTTPDataResponse(w, http.StatusCreated, mockedQueriedUsers)
}

func simpleUsersAPINoManifestEntryUsingAideHandler(w http.ResponseWriter, r *http.Request) {

// unregisterdErr an error that's unregistered in manifest
// should return 500
unregisterdErr := errors.New("unexpected-error")

// mock passing additional headers in request
mockAdditionalHeaders := map[string]string{
"correlation-id": "d7c09ac2-fa46-4ece-bcde-1d7ad81d2230",
}

_ = replier.NewHTTPErrorResponse(w, unregisterdErr, reply.WithHeaders(mockAdditionalHeaders))
}

func simpleTokensAPIUsingAideHandler(w http.ResponseWriter, r *http.Request) {

mockedAccessToken := "05e42c11-8bdd-423d-a2c1-c3c5c6604a30"
mockedRefreshToken := "0e95c426-d373-41a5-bfe1-08db322527bd"

_ = replier.NewHTTPTokenResponse(w, http.StatusOK, mockedAccessToken, mockedRefreshToken)
}

func simpleAPIDefaultResponseUsingAideHandler(w http.ResponseWriter, r *http.Request) {

// Do something that only needs an empty response body.
// Note: 200 status code will be returned if status code passed is 0.
// Otherwise passed code would be used
_ = replier.NewHTTPBlankResponse(w, http.StatusOK)
}

func simpleUsersAPINotFoundCustomReplierUsingAideHandler(w http.ResponseWriter, r *http.Request) {

// Do something with a server
serverErr := errors.New("example-404-error")

replierWithCustomTransitionObj.NewHTTPErrorResponse(w, serverErr)
}

/////////////////////////////

func handleRequest() {
var port string = ":8081"

Expand All @@ -171,6 +232,13 @@ func handleRequest() {
http.HandleFunc("/defaults/1", simpleAPIDefaultResponseHandler)
http.HandleFunc("/custom/users/3", simpleUsersAPINotFoundCustomReplierHandler)

http.HandleFunc("/aides/users", simpleUsersAPIUsingAideHandler)
http.HandleFunc("/aides/users/3", simpleUsersAPINotFoundUsingAideHandler)
http.HandleFunc("/aides/users/4", simpleUsersAPINoManifestEntryUsingAideHandler)
http.HandleFunc("/aides/tokens/refresh", simpleTokensAPIUsingAideHandler)
http.HandleFunc("/aides/defaults/1", simpleAPIDefaultResponseUsingAideHandler)
http.HandleFunc("/aides/custom/users/3", simpleUsersAPINotFoundCustomReplierUsingAideHandler)

log.Printf("Serving simple API on port %s...", port)
log.Fatal(http.ListenAndServe(port, nil))
}
Expand Down
Loading

0 comments on commit 75a59b3

Please sign in to comment.