diff options
Diffstat (limited to '')
-rw-r--r-- | golang/pkg/api/api.go | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/golang/pkg/api/api.go b/golang/pkg/api/api.go index 7aee2fa..472d9e5 100644 --- a/golang/pkg/api/api.go +++ b/golang/pkg/api/api.go @@ -43,7 +43,7 @@ type Response struct { Err error } -func (c *Client) Send(method string, uriRef *url.URL, payload any) (*APIMessage, error) { +func (c *Client) Send(method string, uriRef *url.URL, payload any, response any) error { responseChannel := make(chan *Response) c.requestsChannel <- &Request{ method: method, @@ -53,7 +53,24 @@ func (c *Client) Send(method string, uriRef *url.URL, payload any) (*APIMessage, uri: c.baseURI.ResolveReference(uriRef), } res := <-responseChannel - return res.Message, res.Err + if res.Err != nil { + return res.Err + } + err := res.Message.Error + if err != nil { + switch err.Code { + case 4214: + e := decodeShipInTransitError(err.Data) + time.Sleep(e.SecondsToArrival.Duration() * time.Second) + return c.Send(method, uriRef, payload, response) + default: + return err + } + } + if err := json.Unmarshal(res.Message.Data, &response); err != nil { + return fmt.Errorf("failed to unmarshal message: %w", err) + } + return nil } func queueProcessor(client *Client) { |