Skip to content

Commit

Permalink
nested mod
Browse files Browse the repository at this point in the history
  • Loading branch information
bnkamalesh committed Feb 17, 2024
1 parent 824d3c9 commit c69b91b
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/goapp/go.mod
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
2 changes: 2 additions & 0 deletions lib/goapp/go.sum
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=
110 changes: 110 additions & 0 deletions lib/goapp/goapp.go
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",
}
}

0 comments on commit c69b91b

Please sign in to comment.