Files
inventory-plus-plus/internal/server/response/body.go
T

152 lines
2.7 KiB
Go

package response
import (
"fmt"
"io"
"net/http"
"ruben/inventory2/internal/server/redirect"
)
type (
bodyRes struct {
body io.ReadCloser
res Response
}
)
var _ Response = bodyRes{}
func Body(body io.ReadCloser) Response {
return bodyRes{
body: body,
}
}
func (b bodyRes) String() string {
if b.res != nil {
return fmt.Sprintf(`{"body": %q, "nested": %s}`, b.body, b.res)
}
return fmt.Sprintf(`{"body": %q}`, b.body)
}
func (b bodyRes) wrap(res Response) Response {
b.res = res
return b
}
func (b bodyRes) Status(code int) Response {
return Status(code).wrap(b)
}
func (b bodyRes) Redirect(code redirect.Code, to string) Response {
return Redirect(code, to).wrap(b)
}
func (b bodyRes) HTML(body []byte) Response {
return HTML(body).wrap(b)
}
func (b bodyRes) JSON(body any) Response {
return JSON(body).wrap(b)
}
func (b bodyRes) Body(body io.ReadCloser) Response {
b.body = body
return b
}
func (b bodyRes) Cookie(ck http.Cookie) Response {
return Cookie(ck).wrap(b)
}
func (b bodyRes) HXPushURL(v string) Response {
return HXPushURL(v).wrap(b)
}
func (b bodyRes) HXLocation(v string) Response {
return HXLocation(v).wrap(b)
}
func (b bodyRes) HXPushUrl(v string) Response {
return HXPushUrl(v).wrap(b)
}
func (b bodyRes) HXRedirect(v string) Response {
return HXRedirect(v).wrap(b)
}
func (b bodyRes) HXRefresh(v string) Response {
return HXRefresh(v).wrap(b)
}
func (b bodyRes) HXReplaceUrl(v string) Response {
return HXReplaceUrl(v).wrap(b)
}
func (b bodyRes) HXReswap(v string) Response {
return HXReswap(v).wrap(b)
}
func (b bodyRes) HXRetarget(v string) Response {
return HXRetarget(v).wrap(b)
}
func (b bodyRes) HXReselect(v string) Response {
return HXReselect(v).wrap(b)
}
func (b bodyRes) HXTrigger(v string) Response {
return HXTrigger(v).wrap(b)
}
func (b bodyRes) HXTriggerAfterSettle(v string) Response {
return HXTriggerAfterSettle(v).wrap(b)
}
func (b bodyRes) HXTriggerAfterSwap(v string) Response {
return HXTriggerAfterSwap(v).wrap(b)
}
func (b bodyRes) GetStatus() (int, bool) {
if b.res == nil {
return 0, false
}
return b.res.GetStatus()
}
func (b bodyRes) getHeaders() [][2]string {
if b.res != nil {
return b.res.getHeaders()
}
return nil
}
func (b bodyRes) GetRedirect() (code redirect.Code, to string, ok bool) {
if b.res == nil {
return 0, "", false
}
return b.res.GetRedirect()
}
func (b bodyRes) getBody() (body io.ReadCloser, ok bool, err error) {
return b.body, true, nil
}
func (b bodyRes) getCookies() []http.Cookie {
if b.res != nil {
return b.res.getCookies()
}
return nil
}
func (b bodyRes) getContentType() (contentType string, ok bool) {
if b.res != nil {
return b.res.getContentType()
}
return "", false
}