feat(t224): add incremental freight sync
This commit is contained in:
@@ -57,11 +57,33 @@ func New(baseURL, apiKey string, timeout time.Duration) (*Client, error) {
|
||||
func (client *Client) QueryOrder(
|
||||
ctx context.Context,
|
||||
orderNumber string,
|
||||
) (domain.FreightSourceBatch, error) {
|
||||
return client.query(ctx, map[string]string{
|
||||
"mode": domain.FreightSyncOrderNumber,
|
||||
"order_number": orderNumber,
|
||||
}, domain.FreightSyncOrderNumber, "", "")
|
||||
}
|
||||
|
||||
func (client *Client) QueryCreatedRange(
|
||||
ctx context.Context,
|
||||
createdFrom, createdTo string,
|
||||
) (domain.FreightSourceBatch, error) {
|
||||
return client.query(ctx, map[string]string{
|
||||
"mode": domain.FreightSyncCreatedRange,
|
||||
"created_from": createdFrom,
|
||||
"created_to": createdTo,
|
||||
}, domain.FreightSyncCreatedRange, createdFrom, createdTo)
|
||||
}
|
||||
|
||||
func (client *Client) query(
|
||||
ctx context.Context,
|
||||
payload map[string]string,
|
||||
expectedMode, expectedFrom, expectedTo string,
|
||||
) (domain.FreightSourceBatch, error) {
|
||||
if client.apiKey == "" {
|
||||
return domain.FreightSourceBatch{}, ErrNotConfigured
|
||||
}
|
||||
body, err := json.Marshal(map[string]string{"order_number": orderNumber})
|
||||
body, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return domain.FreightSourceBatch{}, ErrProtocol
|
||||
}
|
||||
@@ -106,9 +128,19 @@ func (client *Client) QueryOrder(
|
||||
if err := decoder.Decode(&extra); !errors.Is(err, io.EOF) {
|
||||
return domain.FreightSourceBatch{}, ErrProtocol
|
||||
}
|
||||
if result.SchemaVersion != 1 || result.Query.Mode != "ORDER_NUMBER" ||
|
||||
if result.SchemaVersion != 1 || result.Query.Mode != expectedMode ||
|
||||
result.Orders == nil {
|
||||
return domain.FreightSourceBatch{}, ErrProtocol
|
||||
}
|
||||
if expectedMode == domain.FreightSyncOrderNumber {
|
||||
if result.Query.CreatedFrom != nil || result.Query.CreatedTo != nil {
|
||||
return domain.FreightSourceBatch{}, ErrProtocol
|
||||
}
|
||||
} else if result.Query.CreatedFrom == nil ||
|
||||
result.Query.CreatedTo == nil ||
|
||||
*result.Query.CreatedFrom != expectedFrom ||
|
||||
*result.Query.CreatedTo != expectedTo {
|
||||
return domain.FreightSourceBatch{}, ErrProtocol
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user