Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Add Response Status #209

Merged
merged 3 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: We need to set the 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) httpcontract.ResponseSuccess {
return &GinStatus{instance, http.StatusOK}
}

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