Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 0.3.0, remove JQ, update headers #19

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ if resp.Status == 200 {
fmt.Println("Found pet with id 1")
// Response.Body is the Pet object.
fmt.Println("Pet's name is ", resp.Body.Name)
fmt.Println("Response headers", resp.Headers())
fmt.Println("Response headers", resp.Headers)
}
```
If you don't need the HTTP body you can use `fetch.ResponseEmpty`
Expand All @@ -115,7 +115,7 @@ if err != nil {
panic(err)
}
fmt.Println("Status:", res.Status)
fmt.Println("Headers:", res.Headers())
fmt.Println("Headers:", res.Headers)
```
#### Error handling
The error will contain the status and other http attributes. Any non-2xx response status is treated as an error.
Expand Down
20 changes: 14 additions & 6 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
)

type Error struct {
inner error
Msg string
Status int
Headers map[string]string
Body string
inner error
Msg string
Status int
Headers map[string]string
DuplicateHeaders map[string][]string
Body string
}

func (e *Error) Error() string {
Expand All @@ -37,7 +38,14 @@ func httpErr(prefix string, err error, r *http.Response, body []byte) *Error {
if r == nil {
return nonHttpErr(prefix, err)
}
return &Error{inner: err, Msg: prefix + err.Error(), Status: r.StatusCode, Headers: uniqueHeaders(r.Header), Body: string(body)}
return &Error{
inner: err,
Msg: prefix + err.Error(),
Status: r.StatusCode,
Headers: uniqueHeaders(r.Header),
DuplicateHeaders: r.Header,
Body: string(body),
}
}

// JQError is returned from J.Q on invalid syntax.
Expand Down
2 changes: 2 additions & 0 deletions fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func Request[T any](url string, config ...Config) (T, *Error) {
if typeOf != nil && typeOf == typeFor[ResponseEmpty]() && firstDigit(res.StatusCode) == 2 {
re := any(&t).(*ResponseEmpty)
re.Status = res.StatusCode
re.Headers = uniqueHeaders(res.Header)
re.DuplicateHeaders = res.Header
return t, nil
}
Expand Down Expand Up @@ -196,6 +197,7 @@ func Request[T any](url string, config ...Config) (T, *Error) {
valueOf := reflect.Indirect(reflect.ValueOf(&t))
valueOf.FieldByName("Status").SetInt(int64(res.StatusCode))
valueOf.FieldByName("DuplicateHeaders").Set(reflect.ValueOf(res.Header))
valueOf.FieldByName("Headers").Set(reflect.ValueOf(uniqueHeaders(res.Header)))
valueOf.FieldByName("BodyBytes").SetBytes(body)
valueOf.FieldByName("Body").Set(reflect.ValueOf(resInstance).Elem())

Expand Down
4 changes: 2 additions & 2 deletions fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestRequest_ResponseT(t *testing.T) {
t.Errorf("wrong status")
}

if res.Headers()["Content-type"] != "application/json" {
if res.Headers["Content-type"] != "application/json" {
t.Errorf("wrong headers")
}

Expand All @@ -106,7 +106,7 @@ func TestRequest_ResponseEmpty(t *testing.T) {
if res.Status != 200 {
t.Errorf("response status isn't 200")
}
if res.Headers()["Content-type"] != "application/json" {
if res.Headers["Content-type"] != "application/json" {
t.Errorf("wrong headers")
}

Expand Down
7 changes: 0 additions & 7 deletions j.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,6 @@ func (n Nil) AsString() (string, bool) { return "", false }
func (n Nil) AsBoolean() (bool, bool) { return false, false }
func (n Nil) IsNil() bool { return true }

// Deprecated, use fetch.Parse(jsonStr).Q(pattern)
// JQ parses jsonStr into fetch.J
// and calls J.Q method with the pattern.
func JQ(jsonStr, pattern string) J {
return Parse(jsonStr).Q(pattern)
}

func isJNil(v any) bool {
return v == nil || reflect.TypeOf(v) == typeFor[Nil]()
}
Expand Down
9 changes: 0 additions & 9 deletions j_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,6 @@ func TestJ_AsSecondValue(t *testing.T) {
}
}

func TestJQ(t *testing.T) {
if JQ(`{"key":"value"}`, ".key").String() != "value" {
t.Errorf("JQ mismatch")
}
if !JQ(`{`, ".key").IsNil() {
t.Errorf("JQ parsed invalid json")
}
}

func mustUnmarshal(s string) J {
j, err := Unmarshal[J](s)
if err != nil {
Expand Down
16 changes: 7 additions & 9 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Response is a wrapper type for (generic) ReturnType to be used in
the HTTP methods. It allows you to access HTTP attributes
of the HTTP response and unmarshal the HTTP body.
e.g.

type User struct {
FirstName string
}
Expand All @@ -17,27 +18,24 @@ e.g.
fmt.Println(res.Body.FirstName)
*/
type Response[T any] struct {
Status int
Status int
// HTTP headers are not unique.
// In the majority of the cases Headers is enough.
// Headers are filled with the last value from DuplicateHeaders.
DuplicateHeaders map[string][]string
Headers map[string]string
Body T
BodyBytes []byte
}

func (r Response[T]) Headers() map[string]string {
return uniqueHeaders(r.DuplicateHeaders)
}

// ResponseEmpty is a special ResponseType that completely ignores the HTTP body.
// Can be used as the (generic) ReturnType for any HTTP method.
type ResponseEmpty struct {
Status int
Headers map[string]string
DuplicateHeaders map[string][]string
}

func (r ResponseEmpty) Headers() map[string]string {
return uniqueHeaders(r.DuplicateHeaders)
}

func uniqueHeaders(headers map[string][]string) map[string]string {
h := make(map[string]string, len(headers))
for key, val := range headers {
Expand Down
Loading