cuelang.org/go@v0.10.1/internal/httplog/event.go (about)

     1  package httplog
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  )
     7  
     8  type Logger interface {
     9  	// Log logs an event of the given kind with the given request
    10  	// or response (either *Request or *Response).
    11  	Log(ctx context.Context, kind EventKind, r RequestOrResponse)
    12  }
    13  
    14  type EventKind int
    15  
    16  const (
    17  	NoEvent EventKind = iota
    18  	KindClientSendRequest
    19  	KindClientRecvResponse
    20  
    21  	// TODO KindServerRecvRequest
    22  	// TODO KindServerSendResponse
    23  )
    24  
    25  func (k EventKind) String() string {
    26  	switch k {
    27  	case KindClientSendRequest:
    28  		return "http client->"
    29  	case KindClientRecvResponse:
    30  		return "http client<-"
    31  	default:
    32  		return "unknown"
    33  	}
    34  }
    35  
    36  // Request represents an HTTP request.
    37  type Request struct {
    38  	ID            int64       `json:"id"`
    39  	Method        string      `json:"method"`
    40  	URL           string      `json:"url"`
    41  	ContentLength int64       `json:"contentLength"`
    42  	Header        http.Header `json:"header"`
    43  	BodyData
    44  }
    45  
    46  func (*Request) requestOrResponse() {}
    47  
    48  // RequestOrResponse is implemented by [*Request] and [*Response].
    49  type RequestOrResponse interface {
    50  	requestOrResponse()
    51  }
    52  
    53  // TODO include timing data for when the initial response was received
    54  // vs the body being read?
    55  
    56  // Response represents an HTTP response.
    57  type Response struct {
    58  	ID         int64       `json:"id"`
    59  	Method     string      `json:"method,omitempty"`
    60  	URL        string      `json:"url,omitempty"`
    61  	Error      string      `json:"error,omitempty"`
    62  	StatusCode int         `json:"statusCode,omitempty"`
    63  	Header     http.Header `json:"header,omitempty"`
    64  	BodyData
    65  }
    66  
    67  // BodyData holds information about the body of a request
    68  // or response.
    69  type BodyData struct {
    70  	Body                string `json:"body,omitempty"`
    71  	Body64              []byte `json:"body64,omitempty"`
    72  	BodyRedactedBecause string `json:"bodyRedactedBecause,omitempty"`
    73  	BodyTruncated       bool   `json:"bodyTruncated,omitempty"`
    74  }
    75  
    76  func (*Response) requestOrResponse() {}