cuelang.org/go@v0.13.0/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  //go:generate go run golang.org/x/tools/cmd/stringer -type=EventKind -linecomment
    17  
    18  const (
    19  	NoEvent                EventKind = iota
    20  	KindClientSendRequest            // http client->
    21  	KindClientRecvResponse           // http client<-
    22  
    23  	// TODO KindServerRecvRequest
    24  	// TODO KindServerSendResponse
    25  )
    26  
    27  // Request represents an HTTP request.
    28  type Request struct {
    29  	ID            int64       `json:"id"`
    30  	Method        string      `json:"method"`
    31  	URL           string      `json:"url"`
    32  	ContentLength int64       `json:"contentLength"`
    33  	Header        http.Header `json:"header"`
    34  	BodyData
    35  }
    36  
    37  func (*Request) requestOrResponse() {}
    38  
    39  // RequestOrResponse is implemented by [*Request] and [*Response].
    40  type RequestOrResponse interface {
    41  	requestOrResponse()
    42  }
    43  
    44  // TODO include timing data for when the initial response was received
    45  // vs the body being read?
    46  
    47  // Response represents an HTTP response.
    48  type Response struct {
    49  	ID         int64       `json:"id"`
    50  	Method     string      `json:"method,omitempty"`
    51  	URL        string      `json:"url,omitempty"`
    52  	Error      string      `json:"error,omitempty"`
    53  	StatusCode int         `json:"statusCode,omitempty"`
    54  	Header     http.Header `json:"header,omitempty"`
    55  	BodyData
    56  }
    57  
    58  // BodyData holds information about the body of a request
    59  // or response.
    60  type BodyData struct {
    61  	Body                string `json:"body,omitempty"`
    62  	Body64              []byte `json:"body64,omitempty"`
    63  	BodyRedactedBecause string `json:"bodyRedactedBecause,omitempty"`
    64  	BodyTruncated       bool   `json:"bodyTruncated,omitempty"`
    65  }
    66  
    67  func (*Response) requestOrResponse() {}