-
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.
- Loading branch information
1 parent
ca153b1
commit 86eb210
Showing
3 changed files
with
122 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,61 @@ | ||
package ruletemplates | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/G-Core/gcorelabscdn-go/gcore" | ||
) | ||
|
||
type RuleTemplateService interface { | ||
Create(ctx context.Context, req *CreateRequest) (*RuleTemplate, error) | ||
Get(ctx context.Context, id int64) (*RuleTemplate, error) | ||
Update(ctx context.Context, id int64, req *UpdateRequest) (*RuleTemplate, error) | ||
Delete(ctx context.Context, id int64) error | ||
} | ||
|
||
type Protocol string | ||
|
||
const ( | ||
HTTPProtocol Protocol = "HTTP" | ||
HTTPSProtocol Protocol = "HTTPS" | ||
MatchProtocol Protocol = "MATCH" | ||
) | ||
|
||
type RuleType int | ||
|
||
const ( | ||
RuleType0 RuleType = 0 | ||
RuleType1 RuleType = 1 | ||
) | ||
|
||
type RuleTemplate struct { | ||
ID int64 `json:"id"` | ||
Name string `json:"name"` | ||
Client int64 `json:"client"` | ||
Deleted bool `json:"deleted"` | ||
Rule string `json:"rule"` | ||
RuleType RuleType `json:"ruleType"` | ||
Weight int `json:"weight"` | ||
Template bool `json:"template"` | ||
Default bool `json:"default"` | ||
OverrideOriginProtocol *Protocol `json:"overrideOriginProtocol"` | ||
Options *gcore.Options `json:"options,omitempty"` | ||
} | ||
|
||
type CreateRequest struct { | ||
Name string `json:"name,omitempty"` | ||
Rule string `json:"rule"` | ||
RuleType RuleType `json:"ruleType"` | ||
Weight int `json:"weight,omitempty"` | ||
OverrideOriginProtocol *Protocol `json:"overrideOriginProtocol"` | ||
Options *gcore.Options `json:"options,omitempty"` | ||
} | ||
|
||
type UpdateRequest struct { | ||
Name string `json:"name,omitempty"` | ||
Rule string `json:"rule,omitempty"` | ||
RuleType RuleType `json:"ruleType,omitempty"` | ||
Weight int `json:"weight,omitempty"` | ||
OverrideOriginProtocol *Protocol `json:"overrideOriginProtocol"` | ||
Options *gcore.Options `json:"options,omitempty"` | ||
} |
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,53 @@ | ||
package ruletemplates | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/G-Core/gcorelabscdn-go/gcore" | ||
) | ||
|
||
type Service struct { | ||
r gcore.Requester | ||
} | ||
|
||
func NewService(r gcore.Requester) *Service { | ||
return &Service{r: r} | ||
} | ||
|
||
func (s *Service) Create(ctx context.Context, req *CreateRequest) (*RuleTemplate, error) { | ||
var ruleTemplate RuleTemplate | ||
|
||
if err := s.r.Request(ctx, http.MethodPost, "/cdn/resources/rule_templates", req, &ruleTemplate); err != nil { | ||
return nil, fmt.Errorf("request: %w", err) | ||
} | ||
|
||
return &ruleTemplate, nil | ||
} | ||
|
||
func (s *Service) Get(ctx context.Context, id int64) (*RuleTemplate, error) { | ||
var ruleTemplate RuleTemplate | ||
if err := s.r.Request(ctx, http.MethodGet, fmt.Sprintf("/cdn/resources/rule_templates/%d", id), nil, &ruleTemplate); err != nil { | ||
return nil, fmt.Errorf("request: %w", err) | ||
} | ||
|
||
return &ruleTemplate, nil | ||
} | ||
|
||
func (s *Service) Update(ctx context.Context, id int64, req *UpdateRequest) (*RuleTemplate, error) { | ||
var ruleTemplate RuleTemplate | ||
if err := s.r.Request(ctx, http.MethodPatch, fmt.Sprintf("/cdn/resources/rule_templates/%d", id), req, &ruleTemplate); err != nil { | ||
return nil, fmt.Errorf("request: %w", err) | ||
} | ||
|
||
return &ruleTemplate, nil | ||
} | ||
|
||
func (s *Service) Delete(ctx context.Context, id int64) error { | ||
if err := s.r.Request(ctx, http.MethodDelete, fmt.Sprintf("/cdn/resources/rule_templates/%d", id), nil, nil); err != nil { | ||
return fmt.Errorf("request: %w", err) | ||
} | ||
|
||
return nil | ||
} |