-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackages.go
60 lines (45 loc) · 1.28 KB
/
packages.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
package tebexgo
import (
"encoding/json"
"fmt"
"log"
"github.com/itschip/tebexgo/internal"
)
func (s *Session) GetAllPackages() ([]Package, error) {
resp, err := internal.GetRequest(s.Secret, AllPackagesEndpoint)
if err != nil {
log.Printf("Failed to fetch packages, Error: %v", err.Error())
return nil, err
}
var packages []Package
if err := internal.UnmarshalResponse(resp, &packages); err != nil {
return nil, err
}
return packages, nil
}
func (s *Session) GetPackage(packageId string) (Package, error) {
resp, err := internal.GetRequest(s.Secret, RetrievePackageEndpoint+packageId)
if err != nil {
log.Printf("Failed to fetch package, Error: %v", err.Error())
return Package{}, err
}
var pkg Package
if err := internal.UnmarshalResponse(resp, &pkg); err != nil {
return Package{}, err
}
return pkg, nil
}
func (s *Session) UpdatePackage(packageId string, updateObject *UpdatePackageObject) error {
endpoint := fmt.Sprintf("%s/%s", RetrievePackageEndpoint, packageId)
reqBody, err := json.Marshal(&updateObject)
if err != nil {
log.Println("Failed to marshal body")
return err
}
_, err = internal.PutRequest(s.Secret, endpoint, reqBody)
if err != nil {
log.Printf("Failed to update package, Error: %v", err.Error())
return err
}
return nil
}