-
Notifications
You must be signed in to change notification settings - Fork 31
/
edge_function.go
120 lines (100 loc) · 3.28 KB
/
edge_function.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package edgefunction
import (
"context"
"github.com/aziontech/azion-cli/pkg/contracts"
"github.com/aziontech/azion-cli/pkg/logger"
"github.com/aziontech/azion-cli/utils"
sdk "github.com/aziontech/azionapi-go-sdk/edgefunctions"
"go.uber.org/zap"
)
const javascript = "javascript"
func (c *Client) Get(ctx context.Context, id int64) (EdgeFunctionResponse, error) {
logger.Debug("Get Edge Function")
request := c.apiClient.EdgeFunctionsAPI.EdgeFunctionsIdGet(ctx, id)
res, httpResp, err := request.Execute()
if err != nil {
if httpResp != nil {
logger.Debug("Error while getting an Edge Function", zap.Error(err))
err := utils.LogAndRewindBody(httpResp)
if err != nil {
return nil, err
}
}
return nil, utils.ErrorPerStatusCode(httpResp, err)
}
return res.Results, nil
}
func (c *Client) Delete(ctx context.Context, id int64) error {
logger.Debug("Delete Edge Function")
request := c.apiClient.EdgeFunctionsAPI.EdgeFunctionsIdDelete(ctx, id)
httpResp, err := request.Execute()
if err != nil {
if httpResp != nil {
logger.Debug("Error while deleting an Edge Function", zap.Error(err))
err := utils.LogAndRewindBody(httpResp)
if err != nil {
return err
}
}
return utils.ErrorPerStatusCode(httpResp, err)
}
return nil
}
func (c *Client) Create(ctx context.Context, req *CreateRequest) (EdgeFunctionResponse, error) {
// Although there's only one option, the API requires the `language` field.
// Hard-coding javascript for now
logger.Debug("Create Edge Function")
req.CreateEdgeFunctionRequest.SetLanguage(javascript)
request := c.apiClient.EdgeFunctionsAPI.EdgeFunctionsPost(ctx).CreateEdgeFunctionRequest(req.CreateEdgeFunctionRequest)
edgeFuncResponse, httpResp, err := request.Execute()
if err != nil {
if httpResp != nil {
logger.Debug("Error while creating an Edge Function", zap.Error(err))
err := utils.LogAndRewindBody(httpResp)
if err != nil {
return nil, err
}
}
return nil, utils.ErrorPerStatusCode(httpResp, err)
}
return edgeFuncResponse.Results, nil
}
func (c *Client) Update(ctx context.Context, req *UpdateRequest, id int64) (EdgeFunctionResponse, error) {
logger.Debug("Update Edge Function")
request := c.apiClient.EdgeFunctionsAPI.EdgeFunctionsIdPatch(ctx, id).PatchEdgeFunctionRequest(req.PatchEdgeFunctionRequest)
edgeFuncResponse, httpResp, err := request.Execute()
if err != nil {
if httpResp != nil {
logger.Debug("Error while updating an Edge Function", zap.Error(err))
err := utils.LogAndRewindBody(httpResp)
if err != nil {
return nil, err
}
}
return nil, utils.ErrorPerStatusCode(httpResp, err)
}
return edgeFuncResponse.Results, nil
}
func (c *Client) List(ctx context.Context, opts *contracts.ListOptions) (*sdk.ListEdgeFunctionResponse, error) {
logger.Debug("List Edge Functions")
if opts.OrderBy == "" {
opts.OrderBy = "id"
}
resp, httpResp, err := c.apiClient.EdgeFunctionsAPI.EdgeFunctionsGet(ctx).
OrderBy(opts.OrderBy).
Page(opts.Page).
PageSize(opts.PageSize).
Sort(opts.Sort).
Execute()
if err != nil {
if httpResp != nil {
logger.Debug("Error while listing the Edge Functions", zap.Error(err))
err := utils.LogAndRewindBody(httpResp)
if err != nil {
return nil, err
}
}
return nil, utils.ErrorPerStatusCode(httpResp, err)
}
return resp, nil
}