Skip to content

Commit

Permalink
fix http: ContentLength=XXX with Body length 0
Browse files Browse the repository at this point in the history
it is required to set body again after retry
implement it through recursion
  • Loading branch information
drypa committed Mar 24, 2024
1 parent 37deb01 commit a0f35e7
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions backend/nalogru/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Client struct {
}

var InternalError = errors.New("internal failed")
var unauthorizedError = errors.New("unauthorized")

// NewClient - creates instance of Client.
func NewClient(baseAddress string) *Client {
Expand Down Expand Up @@ -103,6 +104,14 @@ func (nalogruClient *Client) GetTicketId(queryString string, device *device.Devi
request, err := http.NewRequest(http.MethodPost, url, reader)
res, err := nalogruClient.sendAuthenticatedRequest(request, device)

if errors.Is(err, unauthorizedError) {
if err = nalogruClient.RefreshSession(device); err != nil {
log.Printf("failed to refresh session. %v\n", err)
return "", err
}
return nalogruClient.GetTicketId(queryString, device)
}

if err != nil {
log.Printf("Can't POST %s\n", url)
return "", err
Expand Down Expand Up @@ -172,6 +181,14 @@ func (nalogruClient *Client) GetTicketById(id string, device *device.Device) (*T
request, err := http.NewRequest(http.MethodGet, url, nil)
res, err := nalogruClient.sendAuthenticatedRequest(request, device)

if errors.Is(err, unauthorizedError) {
if err = nalogruClient.RefreshSession(device); err != nil {
log.Printf("failed to refresh session. %v\n", err)
return nil, err
}
return nalogruClient.GetTicketById(id, device)
}

if err != nil {
log.Printf("Can't GET %s\n", url)
return nil, err
Expand Down Expand Up @@ -204,12 +221,21 @@ func (nalogruClient *Client) GetTicketById(id string, device *device.Device) (*T

// GetElectronicTickets request all electronic receipts added by email or phone from nalog.ru api.
func (nalogruClient *Client) GetElectronicTickets(device *device.Device) ([]*TicketDetails, error) {

errorCount := 0
url := nalogruClient.BaseAddress + "/v2/tickets-with-electro"

request, err := http.NewRequest(http.MethodGet, url, nil)

res, err := nalogruClient.sendAuthenticatedRequest(request, device)

if errors.Is(err, unauthorizedError) {
if err = nalogruClient.RefreshSession(device); err != nil {
log.Printf("failed to refresh session. %v\n", err)
return nil, err
}
return nalogruClient.GetElectronicTickets(device)
}

if err != nil {
log.Printf("Can't GET %s\n", url)
return nil, err
Expand Down Expand Up @@ -273,13 +299,7 @@ func (nalogruClient *Client) sendAuthenticatedRequest(r *http.Request, device *d
}

if res.StatusCode == http.StatusUnauthorized {
if err = nalogruClient.RefreshSession(device); err != nil {
log.Printf("failed to refresh session. %v\n", err)
return nil, err
}
//update refreshed sessionId
r.Header.Set("sessionId", device.SessionId)
res, err = sendRequest(r)
return nil, unauthorizedError
}
return res, err
}
Expand Down

0 comments on commit a0f35e7

Please sign in to comment.