|
| 1 | +package v2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | +) |
| 8 | + |
| 9 | +const ( |
| 10 | + CreatePolicyPath = "%s/api/v2/policies" |
| 11 | + DeletePolicyPath = "%s/api/v2/policies/%d" |
| 12 | + UpdatePolicyPath = "%s/api/v2/policies/%d" |
| 13 | + GetPolicyPath = "%s/api/v2/policies/%d" |
| 14 | +) |
| 15 | + |
| 16 | +type PolicyInterface interface { |
| 17 | + CreatePolicy(ctx context.Context, policy Policy) (Policy, error) |
| 18 | + DeletePolicy(ctx context.Context, policyID int) error |
| 19 | + UpdatePolicy(ctx context.Context, policy Policy) (Policy, error) |
| 20 | + GetPolicyByID(ctx context.Context, policyID int) (Policy, int, error) |
| 21 | +} |
| 22 | + |
| 23 | +func (client *Client) CreatePolicy(ctx context.Context, policy Policy) (Policy, error) { |
| 24 | + payload, err := Marshal(policy) |
| 25 | + if err != nil { |
| 26 | + return Policy{}, err |
| 27 | + } |
| 28 | + |
| 29 | + response, err := client.requester.Request(ctx, http.MethodPost, client.CreatePolicyURL(), payload) |
| 30 | + if err != nil { |
| 31 | + return Policy{}, err |
| 32 | + } |
| 33 | + defer response.Body.Close() |
| 34 | + |
| 35 | + if response.StatusCode != http.StatusOK { |
| 36 | + return Policy{}, client.ErrorFromResponse(response) |
| 37 | + } |
| 38 | + |
| 39 | + return Unmarshal[Policy](response.Body) |
| 40 | +} |
| 41 | + |
| 42 | +func (client *Client) DeletePolicy(ctx context.Context, policyID int) error { |
| 43 | + response, err := client.requester.Request(ctx, http.MethodDelete, client.DeletePolicyURL(policyID), nil) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + defer response.Body.Close() |
| 48 | + |
| 49 | + if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK { |
| 50 | + return client.ErrorFromResponse(response) |
| 51 | + } |
| 52 | + |
| 53 | + return err |
| 54 | +} |
| 55 | + |
| 56 | +func (client *Client) UpdatePolicy(ctx context.Context, policy Policy) (Policy, error) { |
| 57 | + payload, err := Marshal(policy) |
| 58 | + if err != nil { |
| 59 | + return Policy{}, err |
| 60 | + } |
| 61 | + |
| 62 | + response, err := client.requester.Request(ctx, http.MethodPut, client.UpdatePolicyURL(policy.ID), payload) |
| 63 | + if err != nil { |
| 64 | + return Policy{}, err |
| 65 | + } |
| 66 | + defer response.Body.Close() |
| 67 | + |
| 68 | + if response.StatusCode != http.StatusOK { |
| 69 | + return Policy{}, client.ErrorFromResponse(response) |
| 70 | + } |
| 71 | + |
| 72 | + return Unmarshal[Policy](response.Body) |
| 73 | +} |
| 74 | + |
| 75 | +func (client *Client) GetPolicyByID(ctx context.Context, policyID int) (Policy, int, error) { |
| 76 | + response, err := client.requester.Request(ctx, http.MethodGet, client.GetPolicyURL(policyID), nil) |
| 77 | + if err != nil { |
| 78 | + return Policy{}, 0, err |
| 79 | + |
| 80 | + } |
| 81 | + defer response.Body.Close() |
| 82 | + |
| 83 | + if response.StatusCode != http.StatusOK { |
| 84 | + return Policy{}, response.StatusCode, client.ErrorFromResponse(response) |
| 85 | + } |
| 86 | + |
| 87 | + policy, err := Unmarshal[Policy](response.Body) |
| 88 | + if err != nil { |
| 89 | + return Policy{}, 0, err |
| 90 | + } |
| 91 | + |
| 92 | + return policy, http.StatusOK, nil |
| 93 | +} |
| 94 | + |
| 95 | +func (client *Client) CreatePolicyURL() string { |
| 96 | + return fmt.Sprintf(CreatePolicyPath, client.config.url) |
| 97 | +} |
| 98 | + |
| 99 | +func (client *Client) DeletePolicyURL(policyID int) string { |
| 100 | + return fmt.Sprintf(DeletePolicyPath, client.config.url, policyID) |
| 101 | +} |
| 102 | + |
| 103 | +func (client *Client) UpdatePolicyURL(policyID int) string { |
| 104 | + return fmt.Sprintf(UpdatePolicyPath, client.config.url, policyID) |
| 105 | +} |
| 106 | + |
| 107 | +func (client *Client) GetPolicyURL(policyID int) string { |
| 108 | + return fmt.Sprintf(GetPolicyPath, client.config.url, policyID) |
| 109 | +} |
0 commit comments