Ping sends a ping request to the server
(ctx context.Context)
| 71 | |
| 72 | // Ping sends a ping request to the server |
| 73 | func (c *Client) Ping(ctx context.Context) (*models.PingResponse, error) { |
| 74 | url := fmt.Sprintf("%s/api/%s/hosts/ping", c.config.PatchmonServer, c.config.APIVersion) |
| 75 | |
| 76 | c.logger.WithFields(logrus.Fields{ |
| 77 | "url": url, |
| 78 | "method": "POST", |
| 79 | }).Debug("Sending ping request to server") |
| 80 | |
| 81 | resp, err := c.client.R(). |
| 82 | SetContext(ctx). |
| 83 | SetHeader("Content-Type", "application/json"). |
| 84 | SetHeader("X-API-ID", c.credentials.APIID). |
| 85 | SetHeader("X-API-KEY", c.credentials.APIKey). |
| 86 | SetResult(&models.PingResponse{}). |
| 87 | Post(url) |
| 88 | |
| 89 | if err != nil { |
| 90 | return nil, fmt.Errorf("ping request failed: %w", err) |
| 91 | } |
| 92 | |
| 93 | if resp.StatusCode() != 200 { |
| 94 | c.logger.WithField("response", resp.String()).Debug("Full error response from ping request") |
| 95 | return nil, fmt.Errorf("ping request failed with status %d: %s", resp.StatusCode(), truncateResponse(resp.String(), 200)) |
| 96 | } |
| 97 | |
| 98 | result, ok := resp.Result().(*models.PingResponse) |
| 99 | if !ok { |
| 100 | return nil, fmt.Errorf("invalid response format") |
| 101 | } |
| 102 | |
| 103 | return result, nil |
| 104 | } |
| 105 | |
| 106 | // SendUpdate sends package update information to the server |
| 107 | func (c *Client) SendUpdate(ctx context.Context, payload *models.ReportPayload) (*models.UpdateResponse, error) { |
no test coverage detected