Skip to content

Commit

Permalink
Show send headers and metadata in response section
Browse files Browse the repository at this point in the history
  • Loading branch information
mirzakhany committed Oct 27, 2024
1 parent baa6bab commit dcb0721
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 80 deletions.
15 changes: 8 additions & 7 deletions internal/domain/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ type GRPCMethod struct {
}

type GRPCResponseDetail struct {
Response string
Metadata []KeyValue
Trailers []KeyValue
StatusCode int
Duration time.Duration
Size int
Error error
Response string
RequestMetadata []KeyValue
ResponseMetadata []KeyValue
Trailers []KeyValue
StatusCode int
Duration time.Duration
Size int
Error error

StatueCode int
Status string
Expand Down
13 changes: 7 additions & 6 deletions internal/domain/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,13 @@ func EncodeQueryParams(params []KeyValue) string {
}

type HTTPResponseDetail struct {
Response string
Headers []KeyValue
Cookies []KeyValue
StatusCode int
Duration time.Duration
Size int
Response string
ResponseHeaders []KeyValue
RequestHeaders []KeyValue
Cookies []KeyValue
StatusCode int
Duration time.Duration
Size int

Error error
}
4 changes: 2 additions & 2 deletions internal/egress/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (s *Service) handlePostRequestFromHeader(r domain.PostRequest, response *re
return nil
}

if result, ok := response.Headers[r.PostRequestSet.FromKey]; ok {
if result, ok := response.ResponseHeaders[r.PostRequestSet.FromKey]; ok {
if env != nil {
env.SetKey(r.PostRequestSet.Target, result)

Expand Down Expand Up @@ -252,7 +252,7 @@ func (s *Service) handlePostRequestFromMetaData(r domain.PostRequest, res *grpc.
return nil
}

for _, item := range res.Metadata {
for _, item := range res.ResponseMetadata {
if item.Key == r.PostRequestSet.FromKey {
if env != nil {
env.SetKey(r.PostRequestSet.Target, item.Value)
Expand Down
40 changes: 23 additions & 17 deletions internal/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var (
)

type Service struct {
appVersion string

requests *state.Requests
environments *state.Environments
protoFiles *state.ProtoFiles
Expand All @@ -44,24 +46,25 @@ type Service struct {
}

type Response struct {
Body string
Metadata []domain.KeyValue
Trailers []domain.KeyValue
TimePassed time.Duration
Size int
Error error
Body string
RequestMetadata []domain.KeyValue
ResponseMetadata []domain.KeyValue
Trailers []domain.KeyValue
TimePassed time.Duration
Size int
Error error

StatueCode int
Status string
}

var (
appName = "Chapar"
semver = "0.1.0-beta1"
)

func NewService(requests *state.Requests, envs *state.Environments, protoFiles *state.ProtoFiles) *Service {
func NewService(appVersion string, requests *state.Requests, envs *state.Environments, protoFiles *state.ProtoFiles) *Service {
return &Service{
appVersion: appVersion,
requests: requests,
environments: envs,
protoFiles: protoFiles,
Expand All @@ -71,7 +74,7 @@ func NewService(requests *state.Requests, envs *state.Environments, protoFiles *

func (s *Service) Dial(req *domain.GRPCRequestSpec) (*grpc.ClientConn, error) {
opts := []grpc.DialOption{
grpc.WithUserAgent(fmt.Sprintf("%s/%s", appName, semver)),
grpc.WithUserAgent(fmt.Sprintf("%s/%s", appName, s.appVersion)),
}

if !req.Settings.Insecure {
Expand Down Expand Up @@ -286,6 +289,8 @@ func (s *Service) Invoke(id, activeEnvironmentID string) (*Response, error) {
ctx, cancel := context.WithTimeout(ctx, timeOut)
defer cancel()

outgoingMetadata, _ := metadata.FromOutgoingContext(ctx)

callOpts := []grpc.CallOption{
grpc.Header(&respHeaders),
grpc.Trailer(&respTrailers),
Expand All @@ -305,14 +310,15 @@ func (s *Service) Invoke(id, activeEnvironmentID string) (*Response, error) {
elapsed := time.Since(start)

out := &Response{
TimePassed: elapsed,
Metadata: domain.MetadataToKeyValue(respHeaders),
Trailers: domain.MetadataToKeyValue(respTrailers),
Error: respErr,
StatueCode: int(status.Code(respErr)),
Status: status.Code(respErr).String(),
Size: len(respStr),
Body: respStr,
TimePassed: elapsed,
ResponseMetadata: domain.MetadataToKeyValue(respHeaders),
RequestMetadata: domain.MetadataToKeyValue(outgoingMetadata),
Trailers: domain.MetadataToKeyValue(respTrailers),
Error: respErr,
StatueCode: int(status.Code(respErr)),
Status: status.Code(respErr).String(),
Size: len(respStr),
Body: respStr,
}

if respErr != nil {
Expand Down
28 changes: 17 additions & 11 deletions internal/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import (
)

type Response struct {
StatusCode int
Headers map[string]string
Cookies []*http.Cookie
Body []byte
StatusCode int
ResponseHeaders map[string]string
RequestHeaders map[string]string
Cookies []*http.Cookie
Body []byte

TimePassed time.Duration

Expand Down Expand Up @@ -164,12 +165,13 @@ func (s *Service) sendRequest(req *domain.HTTPRequestSpec, e *domain.Environment

// handle response
response := &Response{
StatusCode: res.StatusCode,
Headers: map[string]string{},
Cookies: res.Cookies(),
Body: body,
TimePassed: elapsed,
IsJSON: false,
StatusCode: res.StatusCode,
ResponseHeaders: map[string]string{},
RequestHeaders: map[string]string{},
Cookies: res.Cookies(),
Body: body,
TimePassed: elapsed,
IsJSON: false,
}

if IsJSON(string(body)) {
Expand All @@ -183,7 +185,11 @@ func (s *Service) sendRequest(req *domain.HTTPRequestSpec, e *domain.Environment

// handle headers
for k, v := range res.Header {
response.Headers[k] = strings.Join(v, ", ")
response.ResponseHeaders[k] = strings.Join(v, ", ")
}

for k, v := range httpReq.Header {
response.RequestHeaders[k] = strings.Join(v, ", ")
}

return response, nil
Expand Down
12 changes: 6 additions & 6 deletions ui/app/sidebar.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ type Sidebar struct {

selectedIndex int

serviceVersion string
appVersion string
}

type SideBarButton struct {
Icon *widget.Icon
Text string
}

func NewSidebar(theme *chapartheme.Theme, serviceVersion string) *Sidebar {
func NewSidebar(theme *chapartheme.Theme, appVersion string) *Sidebar {
s := &Sidebar{
serviceVersion: serviceVersion,
Theme: theme,
cache: new(op.Ops),
appVersion: appVersion,
Theme: theme,
cache: new(op.Ops),

Buttons: []*SideBarButton{
{Icon: widgets.SwapHoriz, Text: "Requests"},
Expand Down Expand Up @@ -133,7 +133,7 @@ func (s *Sidebar) Layout(gtx layout.Context, theme *chapartheme.Theme) layout.Di
Bottom: unit.Dp(5),
}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
gtx.Constraints.Min.X = gtx.Dp(70)
st := material.Subtitle1(theme.Theme, s.serviceVersion)
st := material.Subtitle1(theme.Theme, s.appVersion)
st.Alignment = text.Middle
return st.Layout(gtx)
})
Expand Down
6 changes: 3 additions & 3 deletions ui/app/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type UI struct {
}

// New creates a new UI using the Go Fonts.
func New(w *app.Window, serviceVersion string) (*UI, error) {
func New(w *app.Window, appVersion string) (*UI, error) {
u := &UI{
window: w,
}
Expand Down Expand Up @@ -100,7 +100,7 @@ func New(w *app.Window, serviceVersion string) (*UI, error) {
return nil, err
}

grpcService := grpc.NewService(u.requestsState, u.environmentsState, u.protoFilesState)
grpcService := grpc.NewService(appVersion, u.requestsState, u.environmentsState, u.protoFilesState)
restService := rest.New(u.requestsState, u.environmentsState)

egressService := egress.New(u.requestsState, u.environmentsState, restService, grpcService)
Expand All @@ -112,7 +112,7 @@ func New(w *app.Window, serviceVersion string) (*UI, error) {
u.consolePage = console.New()

u.header = NewHeader(w, u.environmentsState, u.workspacesState, u.Theme)
u.sideBar = NewSidebar(u.Theme, serviceVersion)
u.sideBar = NewSidebar(u.Theme, appVersion)

u.header.LoadWorkspaces(u.workspacesState.GetWorkspaces())

Expand Down
34 changes: 18 additions & 16 deletions ui/pages/requests/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,14 @@ func (c *Controller) onGrpcInvoke(id string) {
}

c.view.SetGRPCResponse(id, domain.GRPCResponseDetail{
Response: resp.Body,
Metadata: resp.Metadata,
Trailers: resp.Trailers,
StatusCode: resp.StatueCode,
Duration: resp.TimePassed,
Status: resp.Status,
Size: resp.Size,
Response: resp.Body,
ResponseMetadata: resp.ResponseMetadata,
RequestMetadata: resp.RequestMetadata,
Trailers: resp.Trailers,
StatusCode: resp.StatueCode,
Duration: resp.TimePassed,
Status: resp.Status,
Size: resp.Size,
})
}

Expand Down Expand Up @@ -236,7 +237,7 @@ func (c *Controller) onPostRequestSetChanged(id string, statusCode int, item, fr
return
}
response = responseData.Response
headers = responseData.Headers
headers = responseData.ResponseHeaders
cookies = responseData.Cookies
responseFrom = clone.Spec.HTTP.Request.PostRequest.PostRequestSet.From
case domain.RequestTypeGRPC:
Expand All @@ -245,8 +246,8 @@ func (c *Controller) onPostRequestSetChanged(id string, statusCode int, item, fr
return
}
response = responseData.Response
headers = responseData.Metadata
metaData = responseData.Metadata
headers = responseData.ResponseMetadata
metaData = responseData.ResponseMetadata
trailers = responseData.Trailers
responseFrom = clone.Spec.GRPC.PostRequest.PostRequestSet.From
}
Expand Down Expand Up @@ -376,12 +377,13 @@ func (c *Controller) onSubmitRequest(id string) {
}

c.view.SetHTTPResponse(id, domain.HTTPResponseDetail{
Response: resp,
Headers: mapToKeyValue(res.Headers),
Cookies: cookieToKeyValue(res.Cookies),
StatusCode: res.StatusCode,
Duration: res.TimePassed,
Size: len(res.Body),
Response: resp,
ResponseHeaders: mapToKeyValue(res.ResponseHeaders),
RequestHeaders: mapToKeyValue(res.RequestHeaders),
Cookies: cookieToKeyValue(res.Cookies),
StatusCode: res.StatusCode,
Duration: res.TimePassed,
Size: len(res.Body),
})
}

Expand Down
8 changes: 4 additions & 4 deletions ui/pages/requests/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,17 @@ func (r *Grpc) SetRequestBody(body string) {

func (r *Grpc) SetResponse(detail domain.GRPCResponseDetail) {
r.Response.SetResponse(detail.Response)
r.Response.SetMetadata(detail.Metadata)
r.Response.SetMetadata(detail.RequestMetadata, detail.ResponseMetadata)
r.Response.SetTrailers(detail.Trailers)
r.Response.SetError(detail.Error)
r.Response.SetStatusParams(detail.StatusCode, detail.Status, detail.Duration, detail.Size)
}

func (r *Grpc) GetResponse() *domain.GRPCResponseDetail {
return &domain.GRPCResponseDetail{
Response: r.Response.response,
Metadata: domain.TextToKeyValue(r.Response.Metadata.Code()),
Trailers: domain.TextToKeyValue(r.Response.Trailers.Code()),
Response: r.Response.response,
ResponseMetadata: domain.TextToKeyValue(r.Response.Metadata.Code()),
Trailers: domain.TextToKeyValue(r.Response.Trailers.Code()),
}
}

Expand Down
20 changes: 18 additions & 2 deletions ui/pages/requests/grpc/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,24 @@ func (r *Response) SetStatusParams(code int, status string, duration time.Durati
r.status = status
}

func (r *Response) SetMetadata(metadata []domain.KeyValue) {
r.Metadata.SetCode(domain.KeyValuesToText(metadata))
func (r *Response) SetMetadata(requestMetadata, responseMetadata []domain.KeyValue) {
if len(requestMetadata) == 0 && len(responseMetadata) == 0 {
r.Metadata.SetCode("")
return
}

if len(requestMetadata) == 0 {
r.Metadata.SetCode(domain.KeyValuesToText(responseMetadata))
return
}

if len(responseMetadata) == 0 {
r.Metadata.SetCode(domain.KeyValuesToText(requestMetadata))
return
}

txt := fmt.Sprintf("# --- Request Metadata ---\n\n%s\n\n# --- Response Metadata ---\n\n%s", domain.KeyValuesToText(requestMetadata), domain.KeyValuesToText(responseMetadata))
r.Metadata.SetCode(txt)
}

func (r *Response) SetTrailers(trailers []domain.KeyValue) {
Expand Down
19 changes: 17 additions & 2 deletions ui/pages/requests/restful/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,23 @@ func (r *Response) SetStatusParams(code int, duration time.Duration, size int) {
r.responseSize = size
}

func (r *Response) SetHeaders(headers []domain.KeyValue) {
r.responseHeaders.SetCode(domain.KeyValuesToText(headers))
func (r *Response) SetHeaders(requestHeaders, responseHeaders []domain.KeyValue) {
if len(requestHeaders) == 0 && len(responseHeaders) == 0 {
r.responseHeaders.SetCode("")
}

if len(requestHeaders) == 0 {
r.responseHeaders.SetCode(domain.KeyValuesToText(responseHeaders))
return
}

if len(responseHeaders) == 0 {
r.responseHeaders.SetCode(domain.KeyValuesToText(requestHeaders))
return
}

txt := fmt.Sprintf("# --- Request Headers ---\n\n%s\n\n# --- Response Headers ---\n\n%s", domain.KeyValuesToText(requestHeaders), domain.KeyValuesToText(responseHeaders))
r.responseHeaders.SetCode(txt)
}

func (r *Response) SetMessage(message string) {
Expand Down
Loading

0 comments on commit dcb0721

Please sign in to comment.