-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from G-Core/feature/CDI-696-terraform-enable-d…
…isable-live-streaming-preset-support CDI-696: add preset support
- Loading branch information
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |