api/pkg/audit/details/http_details.go

60 lines
1.1 KiB
Go
Raw Normal View History

2021-01-07 21:00:12 +00:00
package details
2020-12-30 16:03:01 +00:00
import (
"net/http"
"strings"
"google.golang.org/protobuf/types/known/anypb"
2020-12-30 16:03:01 +00:00
)
2021-01-07 21:00:12 +00:00
type HTTP struct {
2020-12-30 16:03:01 +00:00
Method string
Host string
URI string
Proto string
Headers http.Header
}
2021-01-07 21:00:12 +00:00
func NewHTTPFromWireFormat(entity *HTTPDetailsEntity) HTTP {
headers := http.Header{}
for name, values := range entity.Headers {
for idx := range values.Values {
headers.Add(name, values.Values[idx])
}
}
return HTTP{
Method: entity.Method.String(),
Host: entity.Host,
URI: entity.Uri,
Proto: entity.Proto,
Headers: headers,
}
}
func (d HTTP) MarshalToWireFormat() (any *anypb.Any, err error) {
2020-12-30 16:03:01 +00:00
var method = HTTPMethod_GET
if methodValue, known := HTTPMethod_value[strings.ToUpper(d.Method)]; known {
method = HTTPMethod(methodValue)
}
headers := make(map[string]*HTTPHeaderValue)
for k, v := range d.Headers {
headers[k] = &HTTPHeaderValue{
Values: v,
}
}
protoDetails := &HTTPDetailsEntity{
2020-12-30 16:03:01 +00:00
Method: method,
Host: d.Host,
Uri: d.URI,
Proto: d.Proto,
Headers: headers,
}
any, err = anypb.New(protoDetails)
return
2020-12-30 16:03:01 +00:00
}