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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
package flyghttracker
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"log/slog"
"net/http"
"strings"
"time"
"within.website/x/web"
)
var (
flyghttrackerURL = flag.String("flyghttracker-url", "https://flyght-tracker.fly.dev/api/upcoming_events", "Flyghttracker URL")
)
// Date represents a date in the format "YYYY-MM-DD"
type Date struct {
time.Time
}
// UnmarshalJSON parses a JSON string in the format "YYYY-MM-DD" to a Date
func (d *Date) UnmarshalJSON(b []byte) error {
s := strings.Trim(string(b), "\"")
t, err := time.Parse("2006-01-02", s)
if err != nil {
return err
}
d.Time = t
return nil
}
// MarshalJSON returns a JSON string in the format "YYYY-MM-DD"
func (d Date) MarshalJSON() ([]byte, error) {
return json.Marshal(d.Time.Format("2006-01-02"))
}
// Event represents an event that members of DevRel will be attending.
type Event struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
StartDate Date `json:"start_date,omitempty"`
EndDate Date `json:"end_date,omitempty"`
Location string `json:"location,omitempty"`
People []string `json:"people,omitempty"`
}
type Error struct {
Code int
Detail string `json:"detail"`
}
func (e Error) LogValues() slog.Value {
return slog.GroupValue(
slog.Int("code", e.Code),
slog.String("detail", e.Detail),
)
}
func (e Error) Error() string {
return fmt.Sprintf("flyghttracker: %d %s", e.Code, e.Detail)
}
type Client struct {
URL string
}
// New creates a new Flyght Tracker client.
func New(url string) *Client {
return &Client{
URL: url,
}
}
// Create creates a new Flyght Tracker event.
func (c *Client) Create(ctx context.Context, event Event) error {
body, err := json.Marshal(event)
if err != nil {
return fmt.Errorf("failed to marshal event: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.URL+"/api/events", bytes.NewReader(body))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("failed to create event: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
var e Error
if err := json.NewDecoder(resp.Body).Decode(&e); err != nil {
return fmt.Errorf("failed to decode error: %w", err)
}
e.Code = resp.StatusCode
return e
}
return nil
}
// Fetch new events from the Flyght Tracker URL.
//
// It returns a list of events that end in the future and that have "Xe" as one of the attendees.
func (c *Client) Fetch() ([]Event, error) {
resp, err := http.Get(c.URL + "/api/upcoming_events")
if err != nil {
return nil, fmt.Errorf("failed to fetch flyghttracker events: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, web.NewError(http.StatusOK, resp)
}
var events []Event
if err := json.NewDecoder(resp.Body).Decode(&events); err != nil {
return nil, fmt.Errorf("failed to decode flyghttracker events: %w", err)
}
var result []Event
for _, event := range events {
if event.EndDate.Before(time.Now()) {
continue
}
found := false
for _, person := range event.People {
if person == "Xe" {
found = true
break
}
}
if found {
result = append(result, event)
}
}
return result, nil
}
|