SendUpdate sends package update information to the server
(ctx context.Context, payload *models.ReportPayload)
| 105 | |
| 106 | // SendUpdate sends package update information to the server |
| 107 | func (c *Client) SendUpdate(ctx context.Context, payload *models.ReportPayload) (*models.UpdateResponse, error) { |
| 108 | url := fmt.Sprintf("%s/api/%s/hosts/update", c.config.PatchmonServer, c.config.APIVersion) |
| 109 | |
| 110 | c.logger.WithFields(logrus.Fields{ |
| 111 | "url": url, |
| 112 | "method": "POST", |
| 113 | }).Debug("Sending update to server") |
| 114 | |
| 115 | resp, err := c.client.R(). |
| 116 | SetContext(ctx). |
| 117 | SetHeader("Content-Type", "application/json"). |
| 118 | SetHeader("X-API-ID", c.credentials.APIID). |
| 119 | SetHeader("X-API-KEY", c.credentials.APIKey). |
| 120 | SetBody(payload). |
| 121 | SetResult(&models.UpdateResponse{}). |
| 122 | Post(url) |
| 123 | |
| 124 | if err != nil { |
| 125 | return nil, fmt.Errorf("update request failed: %w", err) |
| 126 | } |
| 127 | |
| 128 | if resp.StatusCode() != 200 { |
| 129 | c.logger.WithField("response", resp.String()).Debug("Full error response from update request") |
| 130 | return nil, fmt.Errorf("update request failed with status %d: %s", resp.StatusCode(), truncateResponse(resp.String(), 200)) |
| 131 | } |
| 132 | |
| 133 | result, ok := resp.Result().(*models.UpdateResponse) |
| 134 | if !ok { |
| 135 | return nil, fmt.Errorf("invalid response format") |
| 136 | } |
| 137 | |
| 138 | return result, nil |
| 139 | } |
| 140 | |
| 141 | // GetUpdateInterval gets the current update interval from server |
| 142 | func (c *Client) GetUpdateInterval(ctx context.Context) (*models.UpdateIntervalResponse, error) { |
no test coverage detected