github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/httptransport/httpx/httpx.go (about)

     1  package httpx
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"net/url"
     7  
     8  	"github.com/machinefi/w3bstream/pkg/depends/x/contextx"
     9  )
    10  
    11  type WithContentType interface {
    12  	ContentType() string
    13  }
    14  
    15  type WithStatusCode interface {
    16  	StatusCode() int
    17  }
    18  
    19  type WithCookies interface {
    20  	Cookies() []*http.Cookie
    21  }
    22  
    23  type RedirectDescriber interface {
    24  	WithStatusCode
    25  	Location() *url.URL
    26  }
    27  
    28  type WithHeader interface {
    29  	Header() http.Header
    30  }
    31  
    32  type ckStatusCode struct{}
    33  
    34  func ContextWithStatusCode(ctx context.Context, code int) context.Context {
    35  	return contextx.WithValue(ctx, ckStatusCode{}, code)
    36  }
    37  
    38  func StatusCodeFromContext(ctx context.Context) int {
    39  	if code, ok := ctx.Value(ckStatusCode{}).(int); ok {
    40  		return code
    41  	}
    42  	return http.StatusOK
    43  }