Skip to content

Commit

Permalink
[Enhancement] Add Response Status (#209)
Browse files Browse the repository at this point in the history
* [Enhancement] Add Response Status

* Set the response code

---------

Co-authored-by: Wenbo Han <hwbrzzl@gmail.com>
  • Loading branch information
ahmed3mar and hwbrzzl authored Jun 25, 2023
1 parent 3ce7337 commit ae90575
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 18 deletions.
27 changes: 21 additions & 6 deletions contracts/http/mocks/Response.go

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

11 changes: 5 additions & 6 deletions contracts/http/mocks/ResponseOrigin.go

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

42 changes: 42 additions & 0 deletions contracts/http/mocks/ResponseStatus.go

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

11 changes: 5 additions & 6 deletions contracts/http/mocks/ResponseSuccess.go

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

8 changes: 8 additions & 0 deletions contracts/http/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@ type Response interface {
Redirect(code int, location string)
String(code int, format string, values ...any)
Success() ResponseSuccess
Status(code int) ResponseStatus
Writer() http.ResponseWriter
}

//go:generate mockery --name=ResponseStatus
type ResponseStatus interface {
Data(contentType string, data []byte)
Json(obj any)
String(format string, values ...any)
}

//go:generate mockery --name=ResponseSuccess
type ResponseSuccess interface {
Data(contentType string, data []byte)
Expand Down
25 changes: 25 additions & 0 deletions http/gin_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func (r *GinResponse) Success() httpcontract.ResponseSuccess {
return NewGinSuccess(r.instance)
}

func (r *GinResponse) Status(code int) httpcontract.ResponseStatus {
return NewGinStatus(r.instance, code)
}

func (r *GinResponse) Writer() http.ResponseWriter {
return r.instance.Writer
}
Expand All @@ -80,6 +84,27 @@ func (r *GinSuccess) String(format string, values ...any) {
r.instance.String(http.StatusOK, format, values...)
}

type GinStatus struct {
instance *gin.Context
status int
}

func NewGinStatus(instance *gin.Context, code int) httpcontract.ResponseSuccess {
return &GinStatus{instance, code}
}

func (r *GinStatus) Data(contentType string, data []byte) {
r.instance.Data(r.status, contentType, data)
}

func (r *GinStatus) Json(obj any) {
r.instance.JSON(r.status, obj)
}

func (r *GinStatus) String(format string, values ...any) {
r.instance.String(r.status, format, values...)
}

func GinResponseMiddleware() httpcontract.Middleware {
return func(ctx httpcontract.Context) {
blw := &BodyWriter{body: bytes.NewBufferString("")}
Expand Down

0 comments on commit ae90575

Please sign in to comment.