mirror of
https://github.com/dstotijn/hetty.git
synced 2025-07-01 18:47:29 -04:00
Finish first working version of reqlog
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
package api
|
||||
|
||||
//go:generate go run github.com/99designs/gqlgen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
@ -8,27 +10,44 @@ import (
|
||||
)
|
||||
|
||||
type Resolver struct {
|
||||
RequestLogStore *reqlog.RequestLogStore
|
||||
RequestLogService *reqlog.Service
|
||||
}
|
||||
|
||||
type queryResolver struct{ *Resolver }
|
||||
|
||||
func (r *Resolver) Query() QueryResolver { return &queryResolver{r} }
|
||||
|
||||
func (r *queryResolver) GetRequests(ctx context.Context) ([]Request, error) {
|
||||
reqs := r.RequestLogStore.Requests()
|
||||
resp := make([]Request, len(reqs))
|
||||
func (r *queryResolver) GetHTTPRequests(ctx context.Context) ([]HTTPRequest, error) {
|
||||
logs := r.RequestLogService.Requests()
|
||||
reqs := make([]HTTPRequest, len(logs))
|
||||
|
||||
for i := range resp {
|
||||
method := HTTPMethod(reqs[i].Request.Method)
|
||||
for i, log := range logs {
|
||||
method := HTTPMethod(log.Request.Method)
|
||||
if !method.IsValid() {
|
||||
return nil, fmt.Errorf("request has invalid method: %v", method)
|
||||
}
|
||||
resp[i] = Request{
|
||||
URL: reqs[i].Request.URL.String(),
|
||||
Method: method,
|
||||
|
||||
reqs[i] = HTTPRequest{
|
||||
URL: log.Request.URL.String(),
|
||||
Method: method,
|
||||
Timestamp: log.Timestamp,
|
||||
}
|
||||
|
||||
if len(log.Body) > 0 {
|
||||
reqBody := string(log.Body)
|
||||
reqs[i].Body = &reqBody
|
||||
}
|
||||
|
||||
if log.Response != nil {
|
||||
reqs[i].Response = &HTTPResponse{
|
||||
StatusCode: log.Response.Response.StatusCode,
|
||||
}
|
||||
if len(log.Response.Body) > 0 {
|
||||
resBody := string(log.Response.Body)
|
||||
reqs[i].Response.Body = &resBody
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
return reqs, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user