From d0e21d542845a12e951b3ce2f9b959e8b305e901 Mon Sep 17 00:00:00 2001 From: Ilya Brin <464157+ilyabrin@users.noreply.github.com> Date: Mon, 21 Oct 2024 04:32:19 +0300 Subject: [PATCH] add operations --- operations.go | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/operations.go b/operations.go index 42a00d6..4dae3bd 100644 --- a/operations.go +++ b/operations.go @@ -1,7 +1,32 @@ package disk -// TODO +import ( + "context" + "encoding/json" + "fmt" + "net/http" +) -// func (c *Client) Status(ctx context.Context, operationId string, params *QueryParams) (*Operation, *ErrorResponse) { -// return nil, nil -// } +// TODO: add tests and use generics instead of interface{} +func (c *Client) OperationStatus(ctx context.Context, operationID string) (interface{}, *http.Response, error) { + resp, err := c.doRequest(ctx, GET, fmt.Sprintf("operations/operation_id=%s", operationID), nil) + if err != nil { + return nil, nil, fmt.Errorf("failed to make request: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + var errorResp ErrorResponse + if err := json.NewDecoder(resp.Body).Decode(&errorResp); err != nil { + return nil, resp, fmt.Errorf("failed to decode error response: %w", err) + } + return &errorResp, resp, nil + } + + var operation Operation + if err := json.NewDecoder(resp.Body).Decode(&operation); err != nil { + return nil, resp, fmt.Errorf("failed to decode operation: %w", err) + } + + return &operation, resp, nil +}