149 lines
4.1 KiB
Go
149 lines
4.1 KiB
Go
package erpconnector
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"cmroubao/backend-api/internal/domain"
|
|
)
|
|
|
|
var (
|
|
// Deprecated aliases keep the temporary Connector behavior compatible while
|
|
// the source-neutral Go ERP implementation is introduced in T-225 to T-228.
|
|
ErrNotConfigured = domain.ErrFreightSourceNotConfigured
|
|
ErrSessionRequired = domain.ErrFreightSourceSessionNeeded
|
|
ErrNotFound = domain.ErrFreightSourceNotFound
|
|
ErrUnavailable = domain.ErrFreightSourceUnavailable
|
|
ErrProtocol = domain.ErrFreightSourceProtocol
|
|
)
|
|
|
|
const maxResponseBytes = 4 << 20
|
|
|
|
type Client struct {
|
|
baseURL string
|
|
apiKey string
|
|
http *http.Client
|
|
}
|
|
|
|
func New(baseURL, apiKey string, timeout time.Duration) (*Client, error) {
|
|
if strings.TrimSpace(baseURL) == "" {
|
|
baseURL = "http://127.0.0.1:8091"
|
|
}
|
|
if timeout <= 0 {
|
|
timeout = 90 * time.Second
|
|
}
|
|
if timeout <= 0 {
|
|
return nil, errors.New("ERP connector client configuration is invalid")
|
|
}
|
|
return &Client{
|
|
baseURL: strings.TrimRight(baseURL, "/"),
|
|
apiKey: strings.TrimSpace(apiKey),
|
|
http: &http.Client{
|
|
Timeout: timeout,
|
|
CheckRedirect: func(
|
|
_ *http.Request,
|
|
_ []*http.Request,
|
|
) error {
|
|
return http.ErrUseLastResponse
|
|
},
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
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(payload)
|
|
if err != nil {
|
|
return domain.FreightSourceBatch{}, ErrProtocol
|
|
}
|
|
request, err := http.NewRequestWithContext(
|
|
ctx,
|
|
http.MethodPost,
|
|
client.baseURL+"/v1/freight/query",
|
|
bytes.NewReader(body),
|
|
)
|
|
if err != nil {
|
|
return domain.FreightSourceBatch{}, ErrUnavailable
|
|
}
|
|
request.Header.Set("Content-Type", "application/json")
|
|
request.Header.Set("X-API-Key", client.apiKey)
|
|
response, err := client.http.Do(request)
|
|
if err != nil {
|
|
return domain.FreightSourceBatch{}, ErrUnavailable
|
|
}
|
|
defer response.Body.Close()
|
|
limited := io.LimitReader(response.Body, maxResponseBytes+1)
|
|
content, err := io.ReadAll(limited)
|
|
if err != nil || len(content) > maxResponseBytes {
|
|
return domain.FreightSourceBatch{}, ErrUnavailable
|
|
}
|
|
if response.StatusCode != http.StatusOK {
|
|
switch response.StatusCode {
|
|
case http.StatusUnauthorized:
|
|
return domain.FreightSourceBatch{}, ErrSessionRequired
|
|
case http.StatusNotFound:
|
|
return domain.FreightSourceBatch{}, ErrNotFound
|
|
default:
|
|
return domain.FreightSourceBatch{}, ErrUnavailable
|
|
}
|
|
}
|
|
var result domain.FreightSourceBatch
|
|
decoder := json.NewDecoder(bytes.NewReader(content))
|
|
decoder.DisallowUnknownFields()
|
|
if err := decoder.Decode(&result); err != nil {
|
|
return domain.FreightSourceBatch{}, ErrProtocol
|
|
}
|
|
var extra any
|
|
if err := decoder.Decode(&extra); !errors.Is(err, io.EOF) {
|
|
return domain.FreightSourceBatch{}, ErrProtocol
|
|
}
|
|
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
|
|
}
|