|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + |
| 10 | + "github.com/ambient-code/platform/components/ambient-sdk/go-sdk/types" |
| 11 | +) |
| 12 | + |
| 13 | +func (a *ScheduledSessionAPI) projectPath(projectID string) string { |
| 14 | + return "/projects/" + url.PathEscape(projectID) + "/scheduled-sessions" |
| 15 | +} |
| 16 | + |
| 17 | +func (a *ScheduledSessionAPI) ListInProject(ctx context.Context, projectID string, opts *types.ListOptions) (*types.ScheduledSessionList, error) { |
| 18 | + var result types.ScheduledSessionList |
| 19 | + if err := a.client.doWithQuery(ctx, http.MethodGet, a.projectPath(projectID), nil, http.StatusOK, &result, opts); err != nil { |
| 20 | + return nil, err |
| 21 | + } |
| 22 | + return &result, nil |
| 23 | +} |
| 24 | + |
| 25 | +func (a *ScheduledSessionAPI) GetInProject(ctx context.Context, projectID, id string) (*types.ScheduledSession, error) { |
| 26 | + var result types.ScheduledSession |
| 27 | + path := a.projectPath(projectID) + "/" + url.PathEscape(id) |
| 28 | + if err := a.client.do(ctx, http.MethodGet, path, nil, http.StatusOK, &result); err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + return &result, nil |
| 32 | +} |
| 33 | + |
| 34 | +func (a *ScheduledSessionAPI) CreateInProject(ctx context.Context, projectID string, resource *types.ScheduledSession) (*types.ScheduledSession, error) { |
| 35 | + body, err := json.Marshal(resource) |
| 36 | + if err != nil { |
| 37 | + return nil, fmt.Errorf("marshal scheduled session: %w", err) |
| 38 | + } |
| 39 | + var result types.ScheduledSession |
| 40 | + if err := a.client.do(ctx, http.MethodPost, a.projectPath(projectID), body, http.StatusCreated, &result); err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + return &result, nil |
| 44 | +} |
| 45 | + |
| 46 | +func (a *ScheduledSessionAPI) UpdateInProject(ctx context.Context, projectID, id string, patch any) (*types.ScheduledSession, error) { |
| 47 | + body, err := json.Marshal(patch) |
| 48 | + if err != nil { |
| 49 | + return nil, fmt.Errorf("marshal patch: %w", err) |
| 50 | + } |
| 51 | + var result types.ScheduledSession |
| 52 | + path := a.projectPath(projectID) + "/" + url.PathEscape(id) |
| 53 | + if err := a.client.do(ctx, http.MethodPatch, path, body, http.StatusOK, &result); err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + return &result, nil |
| 57 | +} |
| 58 | + |
| 59 | +func (a *ScheduledSessionAPI) DeleteInProject(ctx context.Context, projectID, id string) error { |
| 60 | + return a.client.do(ctx, http.MethodDelete, a.projectPath(projectID)+"/"+url.PathEscape(id), nil, http.StatusNoContent, nil) |
| 61 | +} |
| 62 | + |
| 63 | +func (a *ScheduledSessionAPI) Suspend(ctx context.Context, projectID, id string) (*types.ScheduledSession, error) { |
| 64 | + var result types.ScheduledSession |
| 65 | + path := a.projectPath(projectID) + "/" + url.PathEscape(id) + "/suspend" |
| 66 | + if err := a.client.do(ctx, http.MethodPost, path, nil, http.StatusOK, &result); err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + return &result, nil |
| 70 | +} |
| 71 | + |
| 72 | +func (a *ScheduledSessionAPI) Resume(ctx context.Context, projectID, id string) (*types.ScheduledSession, error) { |
| 73 | + var result types.ScheduledSession |
| 74 | + path := a.projectPath(projectID) + "/" + url.PathEscape(id) + "/resume" |
| 75 | + if err := a.client.do(ctx, http.MethodPost, path, nil, http.StatusOK, &result); err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + return &result, nil |
| 79 | +} |
| 80 | + |
| 81 | +func (a *ScheduledSessionAPI) Trigger(ctx context.Context, projectID, id string) error { |
| 82 | + path := a.projectPath(projectID) + "/" + url.PathEscape(id) + "/trigger" |
| 83 | + return a.client.do(ctx, http.MethodPost, path, nil, http.StatusOK, nil) |
| 84 | +} |
| 85 | + |
| 86 | +func (a *ScheduledSessionAPI) Runs(ctx context.Context, projectID, id string, opts *types.ListOptions) (*types.SessionList, error) { |
| 87 | + var result types.SessionList |
| 88 | + path := a.projectPath(projectID) + "/" + url.PathEscape(id) + "/runs" |
| 89 | + if err := a.client.doWithQuery(ctx, http.MethodGet, path, nil, http.StatusOK, &result, opts); err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + return &result, nil |
| 93 | +} |
| 94 | + |
| 95 | +func (a *ScheduledSessionAPI) GetByName(ctx context.Context, projectID, name string) (*types.ScheduledSession, error) { |
| 96 | + list, err := a.ListInProject(ctx, projectID, &types.ListOptions{Search: "name = '" + name + "'"}) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + for i := range list.Items { |
| 101 | + if list.Items[i].Name == name { |
| 102 | + return &list.Items[i], nil |
| 103 | + } |
| 104 | + } |
| 105 | + return nil, fmt.Errorf("scheduled session %q not found in project %q", name, projectID) |
| 106 | +} |
0 commit comments