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

feat: [#507] Add Abort method for Response #795

Merged
merged 6 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 15 additions & 10 deletions contracts/http/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ type ContextResponse interface {
// Cookie adds a cookie to the response.
Cookie(cookie Cookie) ContextResponse
// Data write the given data to the response.
Data(code int, contentType string, data []byte) Response
Data(code int, contentType string, data []byte) AbortableResponse
// Download initiates a file download by specifying the file path and the desired filename
Download(filepath, filename string) Response
// File serves a file located at the specified file path as the response.
File(filepath string) Response
// Header sets an HTTP header field with the given key and value.
Header(key, value string) ContextResponse
// Json sends a JSON response with the specified status code and data object.
Json(code int, obj any) Response
Json(code int, obj any) AbortableResponse
// NoContent sends a response with no-body and the specified status code.
NoContent(code ...int) Response
NoContent(code ...int) AbortableResponse
// Origin returns the ResponseOrigin
Origin() ResponseOrigin
// Redirect performs an HTTP redirect to the specified location with the given status code.
Redirect(code int, location string) Response
Redirect(code int, location string) AbortableResponse
// String writes a string response with the specified status code and format.
// The 'values' parameter can be used to replace placeholders in the format string.
String(code int, format string, values ...any) Response
String(code int, format string, values ...any) AbortableResponse
// Success returns ResponseStatus with a 200 status code.
Success() ResponseStatus
// Status sets the HTTP response status code and returns the ResponseStatus.
Expand All @@ -49,6 +49,11 @@ type Response interface {
Render() error
}

type AbortableResponse interface {
Response
Abort() error
}

type StreamWriter interface {
// Write writes the specified data to the response.
Write(data []byte) (int, error)
Expand All @@ -62,11 +67,11 @@ type StreamWriter interface {

type ResponseStatus interface {
// Data write the given data to the Response.
Data(contentType string, data []byte) Response
// Json sends a JSON Response with the specified data object.
Json(obj any) Response
// String writes a string Response with the specified format and values.
String(format string, values ...any) Response
Data(contentType string, data []byte) AbortableResponse
// Json sends a JSON AbortResponse with the specified data object.
Json(obj any) AbortableResponse
// String writes a string AbortResponse with the specified format and values.
String(format string, values ...any) AbortableResponse
// Stream sends a streaming response with the specified status code and the given reader.
Stream(step func(w StreamWriter) error) Response
}
Expand Down
10 changes: 5 additions & 5 deletions http/middleware/throttle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ func (r *TestResponse) Cookie(cookie contractshttp.Cookie) contractshttp.Context
panic("do not need to implement it")
}

func (r *TestResponse) Data(code int, contentType string, data []byte) contractshttp.Response {
func (r *TestResponse) Data(code int, contentType string, data []byte) contractshttp.AbortableResponse {
panic("do not need to implement it")
}

Expand All @@ -612,23 +612,23 @@ func (r *TestResponse) Header(key, value string) contractshttp.ContextResponse {
return r
}

func (r *TestResponse) Json(code int, obj any) contractshttp.Response {
func (r *TestResponse) Json(code int, obj any) contractshttp.AbortableResponse {
panic("do not need to implement it")
}

func (r *TestResponse) NoContent(...int) contractshttp.Response {
func (r *TestResponse) NoContent(...int) contractshttp.AbortableResponse {
panic("do not need to implement it")
}

func (r *TestResponse) Origin() contractshttp.ResponseOrigin {
panic("do not need to implement it")
}

func (r *TestResponse) Redirect(code int, location string) contractshttp.Response {
func (r *TestResponse) Redirect(code int, location string) contractshttp.AbortableResponse {
panic("do not need to implement it")
}

func (r *TestResponse) String(code int, format string, values ...any) contractshttp.Response {
func (r *TestResponse) String(code int, format string, values ...any) contractshttp.AbortableResponse {
panic("do not need to implement it")
}

Expand Down
10 changes: 5 additions & 5 deletions log/logrus_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ func (r *TestResponse) Cookie(cookie contractshttp.Cookie) contractshttp.Context
panic("do not need to implement it")
}

func (r *TestResponse) Data(code int, contentType string, data []byte) contractshttp.Response {
func (r *TestResponse) Data(code int, contentType string, data []byte) contractshttp.AbortableResponse {
panic("do not need to implement it")
}

Expand All @@ -793,23 +793,23 @@ func (r *TestResponse) Header(key, value string) contractshttp.ContextResponse {
panic("do not need to implement it")
}

func (r *TestResponse) Json(code int, obj any) contractshttp.Response {
func (r *TestResponse) Json(code int, obj any) contractshttp.AbortableResponse {
panic("do not need to implement it")
}

func (r *TestResponse) NoContent(...int) contractshttp.Response {
func (r *TestResponse) NoContent(...int) contractshttp.AbortableResponse {
panic("do not need to implement it")
}

func (r *TestResponse) Origin() contractshttp.ResponseOrigin {
return &TestResponseOrigin{ctx: r}
}

func (r *TestResponse) Redirect(code int, location string) contractshttp.Response {
func (r *TestResponse) Redirect(code int, location string) contractshttp.AbortableResponse {
panic("do not need to implement it")
}

func (r *TestResponse) String(code int, format string, values ...any) contractshttp.Response {
func (r *TestResponse) String(code int, format string, values ...any) contractshttp.AbortableResponse {
panic("do not need to implement it")
}

Expand Down
122 changes: 122 additions & 0 deletions mocks/http/AbortableResponse.go

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

Loading
Loading