-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.go
More file actions
83 lines (66 loc) · 1.98 KB
/
user.go
File metadata and controls
83 lines (66 loc) · 1.98 KB
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
package main
import (
"errors"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
// APILogin: Url for the login api (which uses username and password to generate the user key)
// APIPost: Url for the pastes api, it is responsible for creating, listing and deleting pastes
const (
APILogin = "https://pastebin.com/api/api_login.php"
APIPost = "https://pastebin.com/api/api_post.php"
)
// User structure contains information about the pastebin user(username, password) and methods to generate the api user key
type User struct {
apiKey string
username string
password string
userKey string
}
// GenerateKey method generates the user api key, which is used to create private pastes and to list user's pastes
func (u *User) GenerateKey() (string, error) {
dataValues := url.Values{
"api_dev_key": {u.apiKey},
"api_user_name": {u.username},
"api_user_password": {u.password},
}
data, httpResponseError := http.PostForm(APILogin, dataValues)
if httpResponseError != nil {
return "", httpResponseError
}
if data.StatusCode != 200 {
return "", errors.New("Couldn't generate the key: " + strconv.Itoa(data.StatusCode))
}
defer data.Body.Close()
returnedData, err := ioutil.ReadAll(data.Body)
if err != nil {
return "", err
}
u.userKey = string(returnedData)
return u.userKey, nil
}
// List method, lists the pastes of a user
func (u *User) List(limit int) (string, error) {
apiOption := "list"
dataValues := url.Values{
"api_dev_key": {u.apiKey},
"api_user_key": {u.userKey},
"api_option": {apiOption},
"api_results_limit": {strconv.Itoa(limit)},
}
data, httpResponseError := http.PostForm(APIPost, dataValues)
if httpResponseError != nil {
return "", httpResponseError
}
if data.StatusCode != 200 {
return "", errors.New("Couldn't list the pastes: " + strconv.Itoa(data.StatusCode))
}
defer data.Body.Close()
returnedData, err := ioutil.ReadAll(data.Body)
if err != nil {
return "", err
}
return string(returnedData), nil
}