| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "strconv" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | ) |
| 10 | |
| 11 | // AssistantClient calls the Assistant lane's routes, /api/v1/assistant, |
| 12 | // as Base does: with the person's device bearer. |
| 13 | type AssistantClient struct{ c *Client } |
| 14 | |
| 15 | // Assistant is the client's Assistant lane. |
| 16 | func (c *Client) Assistant() *AssistantClient { return &AssistantClient{c: c} } |
| 17 | |
| 18 | const assistantPath = "/api/v1/assistant" |
| 19 | |
| 20 | // Snapshot is GET /api/v1/assistant/snapshot. |
| 21 | func (a *AssistantClient) Snapshot(ctx context.Context) (out wire.AssistantSnapshot, err error) { |
| 22 | return out, a.c.do(ctx, "GET", assistantPath+"/snapshot", nil, &out) |
| 23 | } |
| 24 | |
| 25 | // Send is POST /api/v1/assistant/messages. |
| 26 | func (a *AssistantClient) Send(ctx context.Context, text, key string) (out wire.Sent, err error) { |
| 27 | return out, a.c.do(ctx, "POST", assistantPath+"/messages", wire.SendMessage{Text: text, IdempotencyKey: key}, &out) |
| 28 | } |
| 29 | |
| 30 | // SendWith is POST /api/v1/assistant/messages with attachments (ids |
| 31 | // from Upload). |
| 32 | func (a *AssistantClient) SendWith(ctx context.Context, text, key string, files []string) (out wire.Sent, err error) { |
| 33 | return out, a.c.do(ctx, "POST", assistantPath+"/messages", wire.SendMessage{Text: text, IdempotencyKey: key, Attachments: files}, &out) |
| 34 | } |
| 35 | |
| 36 | // Messages is GET /api/v1/assistant/messages. |
| 37 | func (a *AssistantClient) Messages(ctx context.Context, before string, limit int) (out wire.MessagePage, err error) { |
| 38 | path := assistantPath + "/messages?limit=" + strconv.Itoa(limit) |
| 39 | if before != "" { |
| 40 | path += "&before=" + before |
| 41 | } |
| 42 | return out, a.c.do(ctx, "GET", path, nil, &out) |
| 43 | } |
| 44 | |
| 45 | // Cancel is POST /api/v1/assistant/turns/{id}/cancel. |
| 46 | func (a *AssistantClient) Cancel(ctx context.Context, id string) (out wire.CancelAccepted, err error) { |
| 47 | return out, a.c.do(ctx, "POST", assistantPath+"/turns/"+id+"/cancel", struct{}{}, &out) |
| 48 | } |
| 49 | |
| 50 | // Answer is POST /api/v1/assistant/cards/{id}/answer. |
| 51 | func (a *AssistantClient) Answer(ctx context.Context, id, choice string, revision int64) (out wire.AssistantAnswered, err error) { |
| 52 | return out, a.c.do(ctx, "POST", assistantPath+"/cards/"+id+"/answer", wire.AnswerCard{Choice: choice, Revision: revision}, &out) |
| 53 | } |
| 54 | |
| 55 | // Open opens GET /api/v1/assistant/events?after=cursor. |
| 56 | func (a *AssistantClient) Open(ctx context.Context, after int64) (*Stream, error) { |
| 57 | return a.c.openEvents(ctx, assistantPath+"/events", after) |
| 58 | } |
| 59 | |
| 60 | // PutHost is PUT /api/v1/assistant/host: where Base's operations live, |
| 61 | // and the token Base minted for this person. |
| 62 | func (a *AssistantClient) PutHost(ctx context.Context, origin, token string) (out wire.AssistantHost, err error) { |
| 63 | return out, a.c.do(ctx, http.MethodPut, assistantPath+"/host", wire.AssistantHost{Origin: origin, Token: token}, &out) |
| 64 | } |
| 65 | |
| 66 | // OficinaConnect is POST /api/v1/assistant/oficina/connect: the address |
| 67 | // of connect's consent page for this person, whose browser comes back to |
| 68 | // returnTo. |
| 69 | func (a *AssistantClient) OficinaConnect(ctx context.Context, returnTo string) (string, error) { |
| 70 | var out struct { |
| 71 | URL string `json:"url"` |
| 72 | } |
| 73 | err := a.c.do(ctx, http.MethodPost, assistantPath+"/oficina/connect", map[string]string{"return_to": returnTo}, &out) |
| 74 | return out.URL, err |
| 75 | } |
| 76 | |
| 77 | // OficinaFinish is POST /api/v1/assistant/oficina/connect/finish: the |
| 78 | // person finishes the consent connect's callback handed back. |
| 79 | func (a *AssistantClient) OficinaFinish(ctx context.Context, state, code string) (string, error) { |
| 80 | var out struct { |
| 81 | Status string `json:"status"` |
| 82 | } |
| 83 | err := a.c.do(ctx, http.MethodPost, assistantPath+"/oficina/connect/finish", map[string]string{"state": state, "code": code}, &out) |
| 84 | return out.Status, err |
| 85 | } |
| 86 | |
| 87 | // SnapshotKnowing is GET /api/v1/assistant/snapshot?job_states=states: |
| 88 | // a client that knows those job states. |
| 89 | func (a *AssistantClient) SnapshotKnowing(ctx context.Context, states string) (out wire.AssistantSnapshot, err error) { |
| 90 | return out, a.c.do(ctx, "GET", assistantPath+"/snapshot?job_states="+states, nil, &out) |
| 91 | } |
| 92 | |
| 93 | // OpenKnowing opens GET /api/v1/assistant/events?job_states=states&after=cursor. |
| 94 | func (a *AssistantClient) OpenKnowing(ctx context.Context, states string, after int64) (*Stream, error) { |
| 95 | return a.c.openEvents(ctx, assistantPath+"/events?job_states="+states+"&", after) |
| 96 | } |
| 97 | |
| 98 | // CarryOn is POST /api/v1/assistant/cards/{id}/carry_on. |
| 99 | func (a *AssistantClient) CarryOn(ctx context.Context, id string, revision int64, key string) (out wire.AssistantCarriedOn, err error) { |
| 100 | return out, a.c.do(ctx, http.MethodPost, assistantPath+"/cards/"+id+"/carry_on", wire.CarryOn{Revision: revision, IdempotencyKey: key}, &out) |
| 101 | } |
| 102 | |
| 103 | // PutTimeZone is PUT /api/v1/assistant/time-zone. |
| 104 | func (a *AssistantClient) PutTimeZone(ctx context.Context, zone string) (out wire.TimeZone, err error) { |
| 105 | return out, a.c.do(ctx, http.MethodPut, assistantPath+"/time-zone", wire.TimeZone{TimeZone: zone}, &out) |
| 106 | } |
| 107 | |
| 108 | // PutTimeZoneIfUnset is PUT /api/v1/assistant/time-zone with only_if_unset: |
| 109 | // Base's team zone, never over the person's own device's. |
| 110 | func (a *AssistantClient) PutTimeZoneIfUnset(ctx context.Context, zone string) (out wire.TimeZone, err error) { |
| 111 | return out, a.c.do(ctx, http.MethodPut, assistantPath+"/time-zone", wire.AssistantTimeZone{TimeZone: zone, OnlyIfUnset: true}, &out) |
| 112 | } |
| 113 | |