-
Notifications
You must be signed in to change notification settings - Fork 2
/
message_files.go
92 lines (73 loc) · 2.63 KB
/
message_files.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
package assistants
import (
"context"
"fmt"
"net/http"
"net/url"
)
// MessageFileObject represents a message file object.
type MessageFileObject struct {
ID string `json:"id"`
Object string `json:"object"`
CreatedAt int64 `json:"created_at"`
MessageID string `json:"message_id"`
FileID string `json:"file_id"`
}
type MessageFileParams struct {
ThreadID string `json:"thread_id"`
MessageID string `json:"message_id"`
FileID string `json:"FileID"`
}
// AssembleMessageFileURL constructs the URL for retrieving a specific file attached to a message.
func AssembleMessageFileURL(params MessageFileParams) string {
return getRequestURL(fmt.Sprintf("threads/%s/messages/%s/files/%s", params.ThreadID, params.MessageID, params.FileID))
}
// RetrieveMessageFile retrieves a specific file attached to a message in a thread.
func (c *Client) RetrieveMessageFile(ctx context.Context, urlParams MessageFileParams) (*MessageFileObject, error) {
var result MessageFileObject
err := c.sendHTTPRequest(ctx, http.MethodGet, AssembleMessageFileURL(urlParams), nil, &result, assistantsBaseHeaders)
if err != nil {
return nil, err
}
return &result, nil
}
// ListMessageFilesParams represents parameters for listing message files.
type ListMessageFilesParams struct {
ThreadID string `json:"thread_id"`
MessageID string `json:"message_id"`
Limit int `json:"limit"`
Order string `json:"order"`
After string `json:"after"`
Before string `json:"before"`
}
// AssembleMessageFilesListURL constructs the URL for listing files attached to a message.
func AssembleMessageFilesListURL(threadID, messageID string, urlValues url.Values) (string, error) {
baseURL := getRequestURL(fmt.Sprintf("threads/%s/messages/%s/files", threadID, messageID))
return addQueryParams(baseURL, urlValues)
}
// ListMessageFiles lists files attached to a message in a thread.
func (c *Client) ListMessageFiles(ctx context.Context, urlParams ListMessageFilesParams) (*MessageFileObject, error) {
queryParams := url.Values{}
if urlParams.Limit > 0 {
queryParams.Set("limit", fmt.Sprintf("%d", urlParams.Limit))
}
if urlParams.Order != "" {
queryParams.Set("order", urlParams.Order)
}
if urlParams.After != "" {
queryParams.Set("after", urlParams.After)
}
if urlParams.Before != "" {
queryParams.Set("before", urlParams.Before)
}
fullURL, err := AssembleMessageFilesListURL(urlParams.ThreadID, urlParams.MessageID, queryParams)
if err != nil {
return nil, err
}
var result MessageFileObject
err = c.sendHTTPRequest(ctx, http.MethodGet, fullURL, nil, &result, assistantsBaseHeaders)
if err != nil {
return nil, err
}
return &result, nil
}