11package api
22
33import (
4- "bytes"
5- "encoding/json"
6- "fmt"
7- "io"
84 "net/http"
9-
10- "github.com/honganh1206/clue/message"
11- "github.com/honganh1206/clue/server/data/conversation"
125)
136
147type Client struct {
@@ -24,117 +17,4 @@ func NewClient(baseURL string) *Client {
2417 baseURL : baseURL ,
2518 httpClient : & http.Client {},
2619 }
27- }
28-
29- func (c * Client ) CreateConversation () (* conversation.Conversation , error ) {
30- resp , err := c .httpClient .Post (c .baseURL + "/conversations" , "application/json" , nil )
31- if err != nil {
32- return nil , fmt .Errorf ("failed to create conversation: %w" , err )
33- }
34- defer resp .Body .Close ()
35-
36- if resp .StatusCode != http .StatusOK {
37- body , _ := io .ReadAll (resp .Body )
38- return nil , fmt .Errorf ("server error: %s" , string (body ))
39- }
40-
41- var result map [string ]string
42- if err := json .NewDecoder (resp .Body ).Decode (& result ); err != nil {
43- return nil , fmt .Errorf ("failed to decode response: %w" , err )
44- }
45-
46- // TODO: Use NewConversation method?
47- return & conversation.Conversation {
48- ID : result ["id" ],
49- Messages : make ([]* message.Message , 0 ),
50- }, nil
51- }
52-
53- func (c * Client ) ListConversations () ([]conversation.ConversationMetadata , error ) {
54- resp , err := c .httpClient .Get (c .baseURL + "/conversations" )
55- if err != nil {
56- return nil , fmt .Errorf ("failed to list conversations: %w" , err )
57- }
58- defer resp .Body .Close ()
59-
60- if resp .StatusCode != http .StatusOK {
61- body , _ := io .ReadAll (resp .Body )
62- return nil , fmt .Errorf ("server error: %s" , string (body ))
63- }
64-
65- var conversations []conversation.ConversationMetadata
66- if err := json .NewDecoder (resp .Body ).Decode (& conversations ); err != nil {
67- return nil , fmt .Errorf ("failed to decode response: %w" , err )
68- }
69-
70- return conversations , nil
71- }
72-
73- func (c * Client ) GetConversation (id string ) (* conversation.Conversation , error ) {
74- resp , err := c .httpClient .Get (c .baseURL + "/conversations/" + id )
75- if err != nil {
76- return nil , fmt .Errorf ("failed to get conversation: %w" , err )
77- }
78- defer resp .Body .Close ()
79-
80- if resp .StatusCode == http .StatusNotFound {
81- return nil , conversation .ErrConversationNotFound
82- }
83-
84- if resp .StatusCode != http .StatusOK {
85- body , _ := io .ReadAll (resp .Body )
86- return nil , fmt .Errorf ("server error: %s" , string (body ))
87- }
88-
89- var conv conversation.Conversation
90- if err := json .NewDecoder (resp .Body ).Decode (& conv ); err != nil {
91- return nil , fmt .Errorf ("failed to decode response: %w" , err )
92- }
93-
94- return & conv , nil
95- }
96-
97- func (c * Client ) SaveConversation (conv * conversation.Conversation ) error {
98- jsonData , err := json .Marshal (conv )
99- if err != nil {
100- return fmt .Errorf ("failed to marshal conversation: %w" , err )
101- }
102-
103- url := fmt .Sprintf ("%s/conversations/%s" , c .baseURL , conv .ID )
104- req , err := http .NewRequest (http .MethodPut , url , bytes .NewBuffer (jsonData ))
105- if err != nil {
106- return fmt .Errorf ("failed to create request: %w" , err )
107- }
108- req .Header .Set ("Content-Type" , "application/json" )
109-
110- resp , err := c .httpClient .Do (req )
111- if err != nil {
112- return fmt .Errorf ("failed to save conversation: %w" , err )
113- }
114- defer resp .Body .Close ()
115-
116- if resp .StatusCode == http .StatusNotFound {
117- return conversation .ErrConversationNotFound
118- }
119-
120- if resp .StatusCode != http .StatusOK {
121- body , _ := io .ReadAll (resp .Body )
122- return fmt .Errorf ("server error: %s" , string (body ))
123- }
124-
125- return nil
126- }
127-
128- func (c * Client ) GetLatestConversationID () (string , error ) {
129- conversations , err := c .ListConversations ()
130- if err != nil {
131- return "" , err
132- }
133-
134- if len (conversations ) == 0 {
135- return "" , conversation .ErrConversationNotFound
136- }
137-
138- return conversations [0 ].ID , nil
139- }
140-
20+ }
0 commit comments