-
Notifications
You must be signed in to change notification settings - Fork 0
/
credly.go
103 lines (86 loc) · 2.34 KB
/
credly.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
package credly
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
type CredlyBadges struct {
Data []*CredlyBadge `json:"data"`
}
type CredlyBadge struct {
ID string `json:"id"`
Info CredlyBadgeTemplate `json:"badge_template"`
CreatedAt string `json:"created_at"`
ExpiresAt string `json:"expires_at"`
ExpiresAtDate *CustomTime `json:"expires_at_date"`
IssuedAt string `json:"issued_at"`
IssuedAtDate *CustomTime `json:"issued_at_date"`
}
func (c *CredlyBadge) GetName() string {
return c.Info.Name
}
func (c *CredlyBadge) GetDescription() string {
return c.Info.Description
}
func (c *CredlyBadge) GetExpiredDate() *CustomTime {
return c.ExpiresAtDate
}
func (c *CredlyBadge) GetIssueDate() *CustomTime {
return c.IssuedAtDate
}
func (c *CredlyBadge) GetImageUrl() string {
return c.Info.ImageUrl
}
func (c *CredlyBadge) IsExpired() bool {
if c.ExpiresAtDate == nil {
return false
}
return c.ExpiresAtDate.Before(time.Now())
}
type CredlyBadgeIssuer struct {
Summary string `json:"summary"`
}
type CredlyBadgeTemplate struct {
ID string `json:"id"`
Description string `json:"description"`
GlobalActivityUrl string `json:"global_activity_url"`
Level string `json:"level"`
Name string `json:"name"`
State string `json:"state"`
Image *CredlyBadgeImage `json:"image"`
ImageUrl string `json:"image_url"`
Url string `json:"url"`
}
type CredlyBadgeImage struct {
ID string `json:"id"`
Url string `json:"url"`
}
type CredlyBadgeOwner struct {
Name string
VanityUrl string
}
type CustomTime struct {
time.Time
}
func (c *CustomTime) UnmarshalJSON(b []byte) error {
date, err := time.Parse(`"2006-01-02"`, string(b))
if err != nil {
return err
}
c.Time = date
return nil
}
type CredlyService struct{}
func (c *CredlyService) GetBadges(username string) (*CredlyBadges, error) {
url := fmt.Sprintf("https://www.credly.com/users/%s/badges.json", username)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
badges := CredlyBadges{}
if err := json.NewDecoder(resp.Body).Decode(&badges); err != nil {
return nil, err
}
return &badges, err
}