Skip to content

Commit

Permalink
Allow to get a specific member by id (#17)
Browse files Browse the repository at this point in the history
* Fix a typo MembersRepsonse -> MembersResponse.

* Add members.Get method.
  • Loading branch information
francois2metz authored Oct 23, 2022
1 parent fbc4bd7 commit b80e317
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
39 changes: 39 additions & 0 deletions members/Get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package members

import (
"encoding/json"
"fmt"
"net/http"
)

func (c *MembersClient) Get(MemberId int) (*MemberGetResult, error) {
req, err := c.client.NewRequest("GET", fmt.Sprintf("/v1/members/%d", MemberId), nil)
if err != nil {
return nil, err
}

r, err := c.client.Client.Do(req)
if err != nil {
return nil, err
}
defer r.Body.Close()

if r.StatusCode != http.StatusOK {
var target Error
decode := json.NewDecoder(r.Body)
err = decode.Decode(&target)
if err != nil {
return nil, err
}
return &MemberGetResult{Error: &target}, fmt.Errorf("%s", target.Detail)
}

var target MembersResponse
decode := json.NewDecoder(r.Body)
err = decode.Decode(&target)
if err != nil {
return nil, err
}

return &MemberGetResult{Result: target}, nil
}
4 changes: 2 additions & 2 deletions members/List.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (c *MembersClient) List(lo ListOptions) (*MembersResult, *client.Pagination
return &MembersResult{Error: &target}, nil, fmt.Errorf("%s", target.Detail)
}

var target []MembersRepsonse
var target []MembersResponse
decode := json.NewDecoder(r.Body)
err = decode.Decode(&target)
if err != nil {
Expand All @@ -76,7 +76,7 @@ func (c *MembersClient) List(lo ListOptions) (*MembersResult, *client.Pagination
pagination, err := client.GetPaginationMeta(r)
if err != nil {
return nil, nil, err
}
}

return &MembersResult{Result: target}, pagination, nil
}
9 changes: 7 additions & 2 deletions members/Types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ type Error struct {
}

type MembersResult struct {
Result []MembersRepsonse `json:"result"`
Result []MembersResponse `json:"result"`
Error *Error `json:"error"`
}

type MembersRepsonse struct {
type MemberGetResult struct {
Result MembersResponse `json:"result"`
Error *Error `json:"error"`
}

type MembersResponse struct {
ID int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Expand Down

0 comments on commit b80e317

Please sign in to comment.