-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add response mutator option to config and update .gitignore to …
…exclude aide files
- Loading branch information
1 parent
c4185aa
commit c2a668d
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,4 @@ | |
# vendor/ | ||
|
||
.DS_Store | ||
.aider* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package hypert | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
// ResponseMutator is an interface for types that can modify HTTP responses. | ||
type ResponseMutator interface { | ||
MutateResponse(resp *http.Response) (*http.Response, error) | ||
} | ||
|
||
// JSONResponseMutator is a ResponseMutator that modifies JSON responses. | ||
type JSONResponseMutator[T any] func(T) (T, error) | ||
|
||
// Ensure JSONResponseMutator implements ResponseMutator | ||
var _ ResponseMutator = JSONResponseMutator[any](nil) | ||
|
||
// MutateResponse implements the ResponseMutator interface for JSONResponseMutator. | ||
func (m JSONResponseMutator[T]) MutateResponse(resp *http.Response) (mutatedResp *http.Response, err error) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
err = fmt.Errorf("panic in JSONResponseMutator: %v", r) | ||
} | ||
}() | ||
|
||
if resp.Header.Get("Content-Type") != "application/json" { | ||
return resp, nil | ||
} | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("read response body: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
var data T | ||
if err := json.Unmarshal(body, &data); err != nil { | ||
return nil, fmt.Errorf("unmarshal JSON: %w", err) | ||
} | ||
|
||
mutatedData, err := m(data) | ||
if err != nil { | ||
return nil, fmt.Errorf("mutate JSON: %w", err) | ||
} | ||
|
||
mutatedBody, err := json.Marshal(mutatedData) | ||
if err != nil { | ||
return nil, fmt.Errorf("marshal mutated JSON: %w", err) | ||
} | ||
|
||
resp.Body = io.NopCloser(bytes.NewReader(mutatedBody)) | ||
resp.ContentLength = int64(len(mutatedBody)) | ||
resp.Header.Set("Content-Length", strconv.Itoa(len(mutatedBody))) | ||
|
||
return resp, nil | ||
} |