This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
forked from jazibjohar/intercom-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversation_api.go
65 lines (56 loc) · 1.81 KB
/
conversation_api.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
package intercom
import (
"encoding/json"
"fmt"
"gopkg.in/intercom/intercom-go.v2/interfaces"
)
// ConversationRepository defines the interface for working with Conversations through the API.
type ConversationRepository interface {
find(id string) (Conversation, error)
list(params conversationListParams) (ConversationList, error)
read(id string) (Conversation, error)
reply(id string, reply *Reply) (Conversation, error)
}
// ConversationAPI implements ConversationRepository
type ConversationAPI struct {
httpClient interfaces.HTTPClient
}
type conversationReadRequest struct {
Read bool `json:"read"`
}
func (api ConversationAPI) list(params conversationListParams) (ConversationList, error) {
convoList := ConversationList{}
data, err := api.httpClient.Get("/conversations", params)
if err != nil {
return convoList, err
}
err = json.Unmarshal(data, &convoList)
return convoList, err
}
func (api ConversationAPI) read(id string) (Conversation, error) {
conversation := Conversation{}
data, err := api.httpClient.Post(fmt.Sprintf("/conversations/%s", id), conversationReadRequest{Read: true})
if err != nil {
return conversation, err
}
err = json.Unmarshal(data, &conversation)
return conversation, err
}
func (api ConversationAPI) reply(id string, reply *Reply) (Conversation, error) {
conversation := Conversation{}
data, err := api.httpClient.Post(fmt.Sprintf("/conversations/%s/reply", id), reply)
if err != nil {
return conversation, err
}
err = json.Unmarshal(data, &conversation)
return conversation, nil
}
func (api ConversationAPI) find(id string) (Conversation, error) {
conversation := Conversation{}
data, err := api.httpClient.Get(fmt.Sprintf("/conversations/%s", id), nil)
if err != nil {
return conversation, err
}
err = json.Unmarshal(data, &conversation)
return conversation, err
}