|
1 | 1 | package user |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io/ioutil" |
5 | 9 | "net/http" |
6 | | - |
7 | | - "go-service/pkg/client" |
8 | 10 | ) |
9 | 11 |
|
10 | 12 | type UserClient struct { |
11 | 13 | Client *http.Client |
12 | 14 | Url string |
13 | | - Config *client.LogConfig |
14 | | - Log func(context.Context, string, map[string]interface{}) |
15 | 15 | } |
16 | 16 |
|
17 | | -type ResultInfo struct { |
18 | | - Status int64 `mapstructure:"status" json:"status" gorm:"column:status" bson:"status" dynamodbav:"status" firestore:"status"` |
19 | | - Errors []ErrorMessage `mapstructure:"errors" json:"errors,omitempty" gorm:"column:errors" bson:"errors,omitempty" dynamodbav:"errors,omitempty" firestore:"errors,omitempty"` |
20 | | - Message string `mapstructure:"message" json:"message,omitempty" gorm:"column:message" bson:"message,omitempty" dynamodbav:"message,omitempty" firestore:"message,omitempty"` |
21 | | -} |
22 | | -type ErrorMessage struct { |
23 | | - Field string `mapstructure:"field" json:"field,omitempty" gorm:"column:field" bson:"field,omitempty" dynamodbav:"field,omitempty" firestore:"field,omitempty"` |
24 | | - Code string `mapstructure:"code" json:"code,omitempty" gorm:"column:code" bson:"code,omitempty" dynamodbav:"code,omitempty" firestore:"code,omitempty"` |
25 | | - Param string `mapstructure:"param" json:"param,omitempty" gorm:"column:param" bson:"param,omitempty" dynamodbav:"param,omitempty" firestore:"param,omitempty"` |
26 | | - Message string `mapstructure:"message" json:"message,omitempty" gorm:"column:message" bson:"message,omitempty" dynamodbav:"message,omitempty" firestore:"message,omitempty"` |
| 17 | +func NewUserClient(client *http.Client, url string) *UserClient { |
| 18 | + return &UserClient{Client: client, Url: url} |
27 | 19 | } |
28 | 20 |
|
29 | | -func NewUserClient(config client.ClientConfig, log func(context.Context, string, map[string]interface{})) (*UserClient, error) { |
30 | | - c, _, conf, err := client.InitializeClient(config) |
| 21 | +func (c *UserClient) Load(ctx context.Context, id string) (*User, error) { |
| 22 | + requestURL := fmt.Sprintf("%s/%s", c.Url, id) |
| 23 | + |
| 24 | + req, err := http.NewRequest("GET", requestURL, nil) |
31 | 25 | if err != nil { |
32 | | - return nil, err |
| 26 | + panic(err) |
| 27 | + } |
| 28 | + resp, err := c.Client.Do(req) |
| 29 | + if err != nil { |
| 30 | + panic(err) |
33 | 31 | } |
34 | | - return &UserClient{Client: c, Url: config.Endpoint.Url, Config: conf, Log: log}, nil |
35 | | -} |
36 | 32 |
|
37 | | -func (c *UserClient) Load(ctx context.Context, id string) (*User, error) { |
38 | | - url := c.Url + "/" + id |
39 | | - var user User |
40 | | - err := client.Get(ctx, c.Client, url, &user, c.Config, c.Log) |
41 | | - return &user, err |
| 33 | + defer resp.Body.Close() |
| 34 | + |
| 35 | + body, err := ioutil.ReadAll(resp.Body) |
| 36 | + if err != nil { |
| 37 | + panic(err) |
| 38 | + } |
| 39 | + |
| 40 | + var res User |
| 41 | + err = json.Unmarshal(body, &res) |
| 42 | + if err != nil { |
| 43 | + panic(err) |
| 44 | + } |
| 45 | + |
| 46 | + return &res, err |
42 | 47 | } |
43 | 48 |
|
44 | 49 | func (c *UserClient) Create(ctx context.Context, user *User) (int64, error) { |
45 | | - var res ResultInfo |
46 | | - err := client.Post(ctx, c.Client, c.Url, user, &res, c.Config, c.Log) |
47 | | - return res.Status, err |
| 50 | + requestURL := c.Url |
| 51 | + |
| 52 | + data, err := json.Marshal(user) |
| 53 | + req, err := http.NewRequest("POST", requestURL, bytes.NewBuffer(data)) |
| 54 | + if err != nil { |
| 55 | + panic(err) |
| 56 | + } |
| 57 | + |
| 58 | + resp, err := c.Client.Do(req) |
| 59 | + if err != nil { |
| 60 | + panic(err) |
| 61 | + } |
| 62 | + |
| 63 | + defer resp.Body.Close() |
| 64 | + |
| 65 | + if err != nil { |
| 66 | + panic(err) |
| 67 | + } |
| 68 | + |
| 69 | + if resp.StatusCode == 201 { |
| 70 | + return 1, err |
| 71 | + } else { |
| 72 | + return 0, err |
| 73 | + } |
48 | 74 | } |
49 | 75 |
|
50 | | -func (c *UserClient) Update(ctx context.Context, user *User) (int64, error) { |
51 | | - url := c.Url + "/" + user.Id |
52 | | - var res ResultInfo |
53 | | - err := client.Put(ctx, c.Client, url, user, &res, c.Config, c.Log) |
54 | | - return res.Status, err |
| 76 | +func (c *UserClient) Update(ctx context.Context, user *User, id string) (int64, error) { |
| 77 | + requestURL := fmt.Sprintf("%s/%s", c.Url, id) |
| 78 | + |
| 79 | + data, err := json.Marshal(user) |
| 80 | + req, err := http.NewRequest("PUT", requestURL, bytes.NewBuffer(data)) |
| 81 | + if err != nil { |
| 82 | + panic(err) |
| 83 | + } |
| 84 | + |
| 85 | + resp, err := c.Client.Do(req) |
| 86 | + if err != nil { |
| 87 | + panic(err) |
| 88 | + } |
| 89 | + |
| 90 | + defer resp.Body.Close() |
| 91 | + |
| 92 | + if err != nil { |
| 93 | + panic(err) |
| 94 | + } |
| 95 | + |
| 96 | + if resp.StatusCode == 200 { |
| 97 | + return 1, err |
| 98 | + } else { |
| 99 | + return 0, err |
| 100 | + } |
55 | 101 | } |
56 | 102 |
|
57 | | -func (c *UserClient) Patch(ctx context.Context, id string, user map[string]interface{}) (int64, error) { |
58 | | - url := c.Url + "/" + id |
59 | | - var res ResultInfo |
60 | | - err := client.Patch(ctx, c.Client, url, user, &res, c.Config, c.Log) |
61 | | - return res.Status, err |
| 103 | +func (c *UserClient) Patch(ctx context.Context, user map[string]interface{}) (int64, error) { |
| 104 | + id := user["id"] |
| 105 | + requestURL := fmt.Sprintf("%s/%s", c.Url, id) |
| 106 | + |
| 107 | + data, err := json.Marshal(user) |
| 108 | + req, err := http.NewRequest("PATCH", requestURL, bytes.NewBuffer(data)) |
| 109 | + if err != nil { |
| 110 | + panic(err) |
| 111 | + } |
| 112 | + |
| 113 | + resp, err := c.Client.Do(req) |
| 114 | + if err != nil { |
| 115 | + panic(err) |
| 116 | + } |
| 117 | + |
| 118 | + defer resp.Body.Close() |
| 119 | + |
| 120 | + if err != nil { |
| 121 | + panic(err) |
| 122 | + } |
| 123 | + |
| 124 | + if resp.StatusCode == 200 { |
| 125 | + return 1, err |
| 126 | + } else { |
| 127 | + return 0, err |
| 128 | + } |
62 | 129 | } |
63 | 130 |
|
64 | 131 | func (c *UserClient) Delete(ctx context.Context, id string) (int64, error) { |
65 | | - url := c.Url + "/" + id |
66 | | - var res int64 |
67 | | - err := client.Delete(ctx, c.Client, url, &res, c.Config, c.Log) |
68 | | - return res, err |
| 132 | + requestURL := fmt.Sprintf("%s/%s", c.Url, id) |
| 133 | + |
| 134 | + req, err := http.NewRequest("DELETE", requestURL, nil) |
| 135 | + if err != nil { |
| 136 | + panic(err) |
| 137 | + } |
| 138 | + |
| 139 | + resp, err := c.Client.Do(req) |
| 140 | + if err != nil { |
| 141 | + panic(err) |
| 142 | + } |
| 143 | + |
| 144 | + defer resp.Body.Close() |
| 145 | + |
| 146 | + if err != nil { |
| 147 | + panic(err) |
| 148 | + } |
| 149 | + |
| 150 | + if resp.StatusCode == 200 { |
| 151 | + return 1, err |
| 152 | + } else { |
| 153 | + return 0, err |
| 154 | + } |
69 | 155 | } |
0 commit comments