-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
53 lines (50 loc) · 1.2 KB
/
Copy pathmain.go
File metadata and controls
53 lines (50 loc) · 1.2 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
// DO NOT EDIT BY HAND — StayingAPI examples, generated by the export script. Edit the script + re-run.
// Quickstart: search airbnb stays. Run: go run main.go
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
)
type Stay struct {
Platform string `json:"platform"`
Name string `json:"name"`
Price float64 `json:"price"`
}
type Envelope struct {
Data []Stay `json:"data"`
}
func main() {
key := os.Getenv("STAYINGAPI_KEY")
if key == "" {
log.Fatal("Set STAYINGAPI_KEY=stay_...")
}
u, _ := url.Parse("https://api.stayingapi.com/v1/search")
q := u.Query()
q.Set("location", "Split, HR")
q.Set("platforms", "airbnb")
q.Set("limit", "5")
u.RawQuery = q.Encode()
req, _ := http.NewRequest("GET", u.String(), nil)
req.Header.Set("Authorization", "Bearer "+key)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != 200 {
log.Fatalf("HTTP %d: %s", resp.StatusCode, body)
}
var env Envelope
if err := json.Unmarshal(body, &env); err != nil {
log.Fatal(err)
}
for _, s := range env.Data {
fmt.Printf("%s - %s - %.0f\n", s.Platform, s.Name, s.Price)
}
}