115 lines
3.0 KiB
Go
115 lines
3.0 KiB
Go
package erpconnector
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"cmroubao/backend-api/internal/domain"
|
|
)
|
|
|
|
var (
|
|
ErrNotConfigured = errors.New("ERP connector is not configured")
|
|
ErrSessionRequired = errors.New("ERP session is required")
|
|
ErrNotFound = errors.New("ERP freight order not found")
|
|
ErrUnavailable = errors.New("ERP connector is unavailable")
|
|
ErrProtocol = errors.New("ERP connector protocol is invalid")
|
|
)
|
|
|
|
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) {
|
|
if client.apiKey == "" {
|
|
return domain.FreightSourceBatch{}, ErrNotConfigured
|
|
}
|
|
body, err := json.Marshal(map[string]string{"order_number": orderNumber})
|
|
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 != "ORDER_NUMBER" ||
|
|
result.Orders == nil {
|
|
return domain.FreightSourceBatch{}, ErrProtocol
|
|
}
|
|
return result, nil
|
|
}
|