Skip to content

Commit

Permalink
feat: add interceptor pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
kenriortega committed Nov 8, 2021
1 parent 942e3e7 commit 0cac172
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions pkg/interceptor/interceptor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package interceptor

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)

// Can be use this
// c := &http.Client{
// Transport: Interceptor{http.DefaultTransport},
// }

type Interceptor struct {
core http.RoundTripper
}

func (Interceptor) ModifyRequest(r *http.Request) *http.Request {
reqBody := MustHumanize(r.Body)

// this is the request modification
modReqBody := []byte(fmt.Sprintf(`{"req": %s}`, reqBody))
ModReqBodyLen := len(modReqBody)

req := r.Clone(context.Background())
req.Body = io.NopCloser(bytes.NewReader(modReqBody))
req.ContentLength = int64(ModReqBodyLen)
req.Header.Set("Content-Length", fmt.Sprintf("%d", ModReqBodyLen))

return req
}

func (Interceptor) ModifyResponse(r *http.Response) *http.Response {
respBody := MustHumanize(r.Body)

// this is the response modification
modRespBody := []byte(fmt.Sprintf(`{"resp": %s}`, respBody))
ModRespBodyLen := len(modRespBody)

r.Body = io.NopCloser(bytes.NewReader(modRespBody))
r.ContentLength = int64(ModRespBodyLen)
r.Header.Set("Content-Length", fmt.Sprintf("%d", ModRespBodyLen))

return r
}

func (i Interceptor) RoundTrip(r *http.Request) (*http.Response, error) {
defer func() {
_ = r.Body.Close()
}()

// modify before the request is sent
newReq := i.ModifyRequest(r)

// send the request using the DefaultTransport
resp, _ := i.core.RoundTrip(newReq)
defer func() {
_ = resp.Body.Close()
}()

// modify after the response is received
newResp := i.ModifyResponse(resp)

return newResp, nil
}

func MustHumanize(r io.Reader) string {
var m map[string]interface{}
_ = json.NewDecoder(r).Decode(&m)
b, _ := json.MarshalIndent(m, "", " ")
return string(b)
}

0 comments on commit 0cac172

Please sign in to comment.