diff --git a/client.go b/client.go index 44b4241..a07a24e 100644 --- a/client.go +++ b/client.go @@ -4,6 +4,7 @@ import ( "github.com/G-Core/gcorelabscdn-go/gcore" "github.com/G-Core/gcorelabscdn-go/origingroups" "github.com/G-Core/gcorelabscdn-go/originshielding" + "github.com/G-Core/gcorelabscdn-go/presets" "github.com/G-Core/gcorelabscdn-go/resources" "github.com/G-Core/gcorelabscdn-go/rules" "github.com/G-Core/gcorelabscdn-go/sslcerts" @@ -15,6 +16,7 @@ type ClientService interface { OriginGroups() origingroups.OriginGroupService OriginShielding() originshielding.OriginShieldingService SSLCerts() sslcerts.SSLCertService + Presets() presets.PresetsService } var _ ClientService = (*Service)(nil) @@ -26,6 +28,7 @@ type Service struct { originGroupsService origingroups.OriginGroupService originShieldingService originshielding.OriginShieldingService sslCertsService sslcerts.SSLCertService + presetsService presets.PresetsService } func NewService(r gcore.Requester) *Service { @@ -36,6 +39,7 @@ func NewService(r gcore.Requester) *Service { originGroupsService: origingroups.NewService(r), originShieldingService: originshielding.NewService(r), sslCertsService: sslcerts.NewService(r), + presetsService: presets.NewService(r), } } @@ -58,3 +62,7 @@ func (s *Service) OriginShielding() originshielding.OriginShieldingService { func (s *Service) SSLCerts() sslcerts.SSLCertService { return s.sslCertsService } + +func (s *Service) Presets() presets.PresetsService { + return s.presetsService +} diff --git a/presets/presets.go b/presets/presets.go new file mode 100644 index 0000000..7c9e1b9 --- /dev/null +++ b/presets/presets.go @@ -0,0 +1,31 @@ +package presets + +import ( + "context" +) + +type PresetsService interface { + Apply(ctx context.Context, presetID int, req *ApplyRequest) (*AppliedPreset, error) + GetAppliedPreset(ctx context.Context, presetID, objectID int) (*AppliedPreset, error) + Unapply(ctx context.Context, presetID, objectID int) error + Get(ctx context.Context, presetID int) (*Preset, error) +} + +type ApplyRequest struct { + ObjectID int `json:"object_id"` +} + +type GetAppliedPresetResponse struct { + ObjectType *string `json:"object_type"` + ObjectIDs []int `json:"object_ids"` +} + +type AppliedPreset struct { + PresetID int + ObjectID int +} + +type Preset struct { + ID int `json:"id"` + Name string `json:"name"` +} diff --git a/presets/service.go b/presets/service.go new file mode 100644 index 0000000..ba3c9db --- /dev/null +++ b/presets/service.go @@ -0,0 +1,76 @@ +package presets + +import ( + "context" + "fmt" + "net/http" + + "github.com/G-Core/gcorelabscdn-go/gcore" +) + +var _ PresetsService = (*Service)(nil) + +type Service struct { + r gcore.Requester +} + +func NewService(r gcore.Requester) *Service { + return &Service{r: r} +} + +func (s *Service) Apply(ctx context.Context, presetID int, req *ApplyRequest) (*AppliedPreset, error) { + preset := AppliedPreset{ + PresetID: presetID, + ObjectID: req.ObjectID, + } + + path := fmt.Sprintf("/cdn/presets/%d/applied", presetID) + if err := s.r.Request(ctx, http.MethodPost, path, req, nil); err != nil { + return nil, fmt.Errorf("request: %w", err) + } + + return &preset, nil +} + +func (s *Service) GetAppliedPreset(ctx context.Context, presetID, objectID int) (*AppliedPreset, error) { + var response GetAppliedPresetResponse + preset := AppliedPreset{ + PresetID: presetID, + ObjectID: objectID, + } + + path := fmt.Sprintf("/cdn/presets/%d/applied", presetID) + if err := s.r.Request(ctx, http.MethodGet, path, nil, &response); err != nil { + return nil, fmt.Errorf("request: %w", err) + } + + // check if objectID is in response.ObjectIDs list, if no - return error + for _, id := range response.ObjectIDs { + if id == objectID { + return &preset, nil + } + } + + return nil, nil +} + +func (s *Service) Unapply(ctx context.Context, presetID, objectID int) error { + path := fmt.Sprintf("/cdn/presets/%d/applied/%d", presetID, objectID) + + if err := s.r.Request(ctx, http.MethodDelete, path, nil, nil); err != nil { + return fmt.Errorf("request: %w", err) + } + + return nil +} + +func (s *Service) Get(ctx context.Context, presetID int) (*Preset, error) { + path := fmt.Sprintf("/cdn/presets/%d", presetID) + var preset Preset + + if err := s.r.Request(ctx, http.MethodGet, path, nil, &preset); err != nil { + return nil, fmt.Errorf("unable to retrieve preset with id=%d. Make sure the ID provided is correct. Details: %w", presetID, err) + } + + return &preset, nil +}