-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
824d3c9
commit c69b91b
Showing
3 changed files
with
117 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/bnkamalesh/goapp/lib/goapp | ||
|
||
go 1.22.0 | ||
|
||
require github.com/bnkamalesh/errors v0.11.1 |
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,2 @@ | ||
github.com/bnkamalesh/errors v0.11.1 h1:lxPIza88BwIf/k8jD1UlQyLS+QzEMCwqM2I6Uo81GVs= | ||
github.com/bnkamalesh/errors v0.11.1/go.mod h1:X8+uM23IDiSq8q5kYC3dEP+sMTRlZz0IWFXo7sfrn18= |
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 |
---|---|---|
@@ -1 +1,111 @@ | ||
package goapp | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/bnkamalesh/errors" | ||
) | ||
|
||
type User struct { | ||
ID string | ||
FullName string | ||
Email string | ||
Phone string | ||
ContactAddress string | ||
} | ||
|
||
type GoApp struct { | ||
client *http.Client | ||
basePath string | ||
usersBase string | ||
} | ||
|
||
func (ht *GoApp) makeRequest(ctx context.Context, req *http.Request) ([]byte, error) { | ||
resp, err := ht.client.Do(req) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed making request") | ||
} | ||
|
||
raw, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed reading response body") | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, errors.Errorf("%d: %s", resp.StatusCode, string(raw)) | ||
} | ||
|
||
return raw, nil | ||
} | ||
|
||
func (ht *GoApp) CreateUser(ctx context.Context, us *User) (*User, error) { | ||
payload, err := json.Marshal(us) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed marshaling to json") | ||
} | ||
buff := bytes.NewBuffer(payload) | ||
req, err := http.NewRequestWithContext( | ||
ctx, | ||
http.MethodPost, | ||
ht.usersBase, | ||
buff, | ||
) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed preparing request") | ||
} | ||
|
||
raw, err := ht.makeRequest(ctx, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
respUsr := struct { | ||
Data User `json:"data"` | ||
}{} | ||
err = json.Unmarshal(raw, &respUsr) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed unmarshaling user") | ||
} | ||
|
||
return &respUsr.Data, nil | ||
} | ||
|
||
func (ht *GoApp) UserByEmail(ctx context.Context, email string) (*User, error) { | ||
req, err := http.NewRequestWithContext( | ||
ctx, | ||
http.MethodGet, | ||
fmt.Sprintf("%s/%s", ht.usersBase, email), | ||
nil, | ||
) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed preparing request") | ||
} | ||
|
||
raw, err := ht.makeRequest(ctx, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
respUsr := struct { | ||
Data User `json:"data"` | ||
}{} | ||
err = json.Unmarshal(raw, &respUsr) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed unmarshaling user") | ||
} | ||
|
||
return &respUsr.Data, nil | ||
} | ||
|
||
func NewClient(basePath string) *GoApp { | ||
return &GoApp{ | ||
client: http.DefaultClient, | ||
basePath: basePath, | ||
usersBase: basePath + "/users", | ||
} | ||
} |