-
Notifications
You must be signed in to change notification settings - Fork 0
/
RESTful_challenge.go
85 lines (72 loc) · 2.7 KB
/
RESTful_challenge.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
/* Alta3 Research | RZFeeser
CHALLENGE 01 - API lookup and JSON parsing */
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"time"
)
type SpaceX []struct {
CapsuleSerial string `json:"capsule_serial"`
CapsuleID string `json:"capsule_id"`
Status string `json:"status"`
OriginalLaunch time.Time `json:"original_launch"`
OriginalLaunchUnix int `json:"original_launch_unix"`
Missions []struct {
Name string `json:"name"`
Flight int `json:"flight"`
} `json:"missions"`
Landings int `json:"landings"`
Type string `json:"type"`
Details string `json:"details"`
ReuseCount int `json:"reuse_count"`
}
func main() {
// define our URL (API) as a string
url := "https://api.spacexdata.com/v3/capsules"
// Build the request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal("NewRequest: ", err)
return
}
// For control over HTTP client headers,
// redirect policy, and other settings,
// create a Client
// A Client is an HTTP client
client := &http.Client{}
// Send the request via a client
// Do sends an HTTP request and
// returns an HTTP response
resp, err := client.Do(req)
if err != nil {
log.Fatal("Do: ", err)
return
}
// Callers should close resp.Body
// when done reading from it
// Defer the closing of the body
defer resp.Body.Close()
// Fill the record with the data from the JSON
var record SpaceX
// Use json.Decode for reading streams of JSON data
if err := json.NewDecoder(resp.Body).Decode(&record); err != nil {
log.Println(err)
}
// the launchNo is an int
// launchData is the data
for launchNo, launchData := range record {
fmt.Println("Capsult Record =\n", launchData)
fmt.Println("Record Number =", launchNo)
fmt.Println("Capsule Serial =", launchData.CapsuleSerial)
fmt.Println("Capsule ID =", launchData.CapsuleID)
fmt.Println("Original Launch =", launchData.OriginalLaunch)
fmt.Println("Landings =", launchData.Landings)
fmt.Println("Type =", launchData.Type)
fmt.Println("Details =", launchData.Details)
fmt.Println("Reuse Count =", launchData.ReuseCount)
fmt.Println("End of Record\n\n")
}
}