-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsozlukcli.go
111 lines (82 loc) · 1.89 KB
/
sozlukcli.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package sozlukcli
import (
"errors"
"fmt"
"strconv"
"github.com/gosimple/slug"
)
// Config :
type Config struct {
token string
slug string
}
var config Config
// NewSozluk :
func NewSozluk(token string) bool {
config = Config{token: token, slug: ""}
response, err := isTokenExistReq(config.token)
done := err == nil && response.Data.IsAlive
if done {
config.slug = response.Data.Slug
}
return done
}
// CreateEntry :
func CreateEntry(topic string, entry string) int {
topicID := TopicIsAlreadyExist(topic)
if topicID == "" {
response, err := createTopicWithEntryReq(topic, entry, config.token)
done := err == nil && response.Success
if done {
return response.ID
} else if err == nil && !response.Success {
fmt.Printf("- %s\n", response.Message)
}
} else {
response, err := createEntryIntoTopicReq(topicID, entry, config.token)
done := err == nil && response.Success
if done {
return response.Data.ID
} else if err == nil && !response.Success {
fmt.Printf("- %s\n", response.Message)
}
}
return -1
}
// DeleteEntry :
func DeleteEntry(ID int) {
deleteEntryReq(ID, config.token)
}
// GetSlug :
func GetSlug() string {
return config.slug
}
// TopicIsAlreadyExist :
func TopicIsAlreadyExist(topic string) string {
response, err := isTopicExistReq(topic, config.token)
done := err == nil && response.Success
if done {
target := slug.Make(topic)
for index, element := range response.Data.Topics {
_ = index
if element.Slug == target {
return strconv.Itoa((&element).ID)
}
}
}
return ""
}
// DoLogin :
func DoLogin(email string, password string) (string, error) {
response, err := createSessionReq(email, password)
done := err == nil && response.Success
if done {
return response.Data.Token, nil
}
return "", errors.New("login failed")
}
// DoLogout :
func DoLogout(token string) bool {
deleteSessionReq(token)
return true
}