2020-02-23 22:07:46 +01:00
|
|
|
package api
|
|
|
|
|
2020-09-19 01:27:55 +02:00
|
|
|
//go:generate go run github.com/99designs/gqlgen
|
|
|
|
|
2020-02-23 22:07:46 +01:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/dstotijn/gurp/pkg/reqlog"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Resolver struct {
|
2020-09-19 01:27:55 +02:00
|
|
|
RequestLogService *reqlog.Service
|
2020-02-23 22:07:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type queryResolver struct{ *Resolver }
|
|
|
|
|
|
|
|
func (r *Resolver) Query() QueryResolver { return &queryResolver{r} }
|
|
|
|
|
2020-09-19 01:27:55 +02:00
|
|
|
func (r *queryResolver) GetHTTPRequests(ctx context.Context) ([]HTTPRequest, error) {
|
|
|
|
logs := r.RequestLogService.Requests()
|
|
|
|
reqs := make([]HTTPRequest, len(logs))
|
2020-02-23 22:07:46 +01:00
|
|
|
|
2020-09-19 01:27:55 +02:00
|
|
|
for i, log := range logs {
|
|
|
|
method := HTTPMethod(log.Request.Method)
|
2020-02-23 22:07:46 +01:00
|
|
|
if !method.IsValid() {
|
|
|
|
return nil, fmt.Errorf("request has invalid method: %v", method)
|
|
|
|
}
|
2020-09-19 01:27:55 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-02-23 22:07:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-19 01:27:55 +02:00
|
|
|
return reqs, nil
|
2020-02-23 22:07:46 +01:00
|
|
|
}
|