Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 861a1640d4 | |||
| 28a8810ffb | |||
| 3ed7d33b84 | |||
| 3e02c6f2d5 |
@@ -40,6 +40,6 @@ func NewDMClient(token, dmURL, device string) *Client {
|
|||||||
device: device,
|
device: device,
|
||||||
resty: restyClient,
|
resty: restyClient,
|
||||||
apiBaseURL: dmURL,
|
apiBaseURL: dmURL,
|
||||||
fiscalEndpoint: "/dm/fiscal",
|
fiscalEndpoint: "/dm/execute",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,18 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type APIError struct {
|
||||||
|
Code int
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *APIError) Error() string {
|
||||||
|
return fmt.Sprintf("vchasno api error %d: %s", e.Code, e.Message)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) executeRequest(ctx context.Context, request FiscalRequest, response interface{}) error {
|
func (c *Client) executeRequest(ctx context.Context, request FiscalRequest, response interface{}) error {
|
||||||
request.Device = c.device
|
request.Device = c.device
|
||||||
|
|
||||||
@@ -15,31 +25,44 @@ func (c *Client) executeRequest(ctx context.Context, request FiscalRequest, resp
|
|||||||
return fmt.Errorf("failed to marshal request: %w", err)
|
return fmt.Errorf("failed to marshal request: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
url := c.apiBaseURL + c.fiscalEndpoint
|
||||||
|
log.Printf("[VCHASNO] POST %s", url)
|
||||||
|
log.Printf("[VCHASNO] Request: %s", string(reqJson))
|
||||||
|
|
||||||
resp, err := c.resty.R().
|
resp, err := c.resty.R().
|
||||||
SetContext(ctx).
|
SetContext(ctx).
|
||||||
SetHeader("Authorization", c.token).
|
SetHeader("Authorization", c.token).
|
||||||
|
SetHeader("Content-Type", "application/json").
|
||||||
SetBody(reqJson).
|
SetBody(reqJson).
|
||||||
Post(c.apiBaseURL + c.fiscalEndpoint)
|
Post(url)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("request failed: %w", err)
|
return fmt.Errorf("request failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.IsError() {
|
|
||||||
return fmt.Errorf("api error: %v", resp.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if resp.StatusCode() != 200 {
|
|
||||||
return fmt.Errorf("unexpected status code: %d", resp.StatusCode())
|
|
||||||
}
|
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to read response: %w", err)
|
return fmt.Errorf("failed to read response: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("[VCHASNO] Response (status %d): %s", resp.StatusCode(), string(body))
|
||||||
|
|
||||||
|
if resp.StatusCode() != 200 {
|
||||||
|
return fmt.Errorf("api error (status %d): %s", resp.StatusCode(), string(body))
|
||||||
|
}
|
||||||
|
|
||||||
if err := json.Unmarshal(body, response); err != nil {
|
if err := json.Unmarshal(body, response); err != nil {
|
||||||
return fmt.Errorf("failed to unmarshal response: %w", err)
|
return fmt.Errorf("failed to unmarshal response: %w, body: %s", err, string(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
var baseResp BaseResponse
|
||||||
|
if err := json.Unmarshal(body, &baseResp); err == nil {
|
||||||
|
if baseResp.HasError() {
|
||||||
|
return &APIError{
|
||||||
|
Code: baseResp.Res,
|
||||||
|
Message: baseResp.Errortxt,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -15,6 +15,17 @@ type BaseResponse struct {
|
|||||||
ErrorExtra interface{} `json:"error_extra"`
|
ErrorExtra interface{} `json:"error_extra"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *BaseResponse) HasError() bool {
|
||||||
|
return r.Res != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *BaseResponse) Error() string {
|
||||||
|
if r.Errortxt != "" {
|
||||||
|
return r.Errortxt
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
type SellResponse struct {
|
type SellResponse struct {
|
||||||
BaseResponse
|
BaseResponse
|
||||||
Info SellInfo `json:"info"`
|
Info SellInfo `json:"info"`
|
||||||
|
|||||||
Reference in New Issue
Block a user