-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
170 lines (128 loc) · 3.44 KB
/
main.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
)
func main() {
jokeType := flag.String("type", "", "Type of joke to fetch")
jokeTypeList := flag.Bool("list-types", false, "List available joke types")
jokeCount := 1
flag.Parse()
if *jokeTypeList {
fmt.Println("Available joke types:")
jokeTypes, errorMessage := getJokeTypes()
if errorMessage != "" {
fmt.Println(errorMessage)
return
}
for _, jokeType := range jokeTypes {
fmt.Println(jokeType)
}
return
}
apiQuery := buildApiQuery(*jokeType, jokeCount)
joke, errorMessage := getJoke(apiQuery)
if errorMessage != "" {
fmt.Println(errorMessage)
return
}
printJoke(joke)
}
type Joke struct {
Setup string `json:"setup"`
Punchline string `json:"punchline"`
Type string `json:"type"`
Id int `json:"id"`
}
type ApiQuery struct {
Url string
QueryType string
JokeType string
JokeCount int
}
func getJokeTypes() ([]string, string) {
baseUrl := "https://official-joke-api.appspot.com"
errorMessage := ""
req, err := http.Get(baseUrl + "/types")
if err != nil {
errorMessage = fmt.Sprintf("Error occurred while sending the HTTP request: %v", err.Error())
return []string{}, errorMessage
}
if req.StatusCode != http.StatusOK {
errorMessage = fmt.Sprintf("Received non 200 status code: %v", req.Status)
return []string{}, errorMessage
}
body, err := io.ReadAll(req.Body)
if err != nil {
errorMessage = fmt.Sprintf("Error occurred while reading the response body: %v", err.Error())
return []string{}, errorMessage
}
defer req.Body.Close()
var jokeTypes []string
err = json.Unmarshal(body, &jokeTypes)
if err != nil {
errorMessage = fmt.Sprintf("Error occurred while parsing the JSON response: %v", err.Error())
return []string{}, errorMessage
}
return jokeTypes, errorMessage
}
func getJoke(aq ApiQuery) (Joke, string) {
req, err := http.Get(aq.Url)
errorMessage := ""
if err != nil {
errorMessage = fmt.Sprintf("Error occurred while sending the HTTP request: %v", err.Error())
return Joke{}, errorMessage
}
if req.StatusCode != http.StatusOK {
errorMessage = fmt.Sprintf("Received non 200 status code: %v", req.Status)
return Joke{}, errorMessage
}
body, err := io.ReadAll(req.Body)
if err != nil {
errorMessage = fmt.Sprintf("Error occurred while reading the response body: %v", err.Error())
return Joke{}, errorMessage
}
defer req.Body.Close()
if len(body) == 0 || string(body) == "[]" {
errorMessage = fmt.Sprintf("Error: could not find joke of type '%v'", aq.JokeType)
return Joke{}, errorMessage
}
var jokes []Joke
err = json.Unmarshal(body, &jokes)
if err != nil {
errorMessage = fmt.Sprintf("Error occurred while parsing the JSON response: %v", err.Error())
return Joke{}, errorMessage
}
return jokes[0], errorMessage
}
func printJoke(joke Joke) {
fmt.Println(joke.Setup)
fmt.Println("...")
fmt.Println("")
fmt.Println("...")
fmt.Println("")
fmt.Println("...")
fmt.Println(joke.Punchline)
}
func buildApiQuery(jokeType string, jokeCount int) ApiQuery {
queryType := "default"
if jokeType != "" {
queryType = "custom"
}
aq := ApiQuery{
QueryType: queryType,
JokeType: jokeType,
JokeCount: jokeCount,
}
baseUrl := "https://official-joke-api.appspot.com"
if aq.QueryType == "default" {
baseUrl += fmt.Sprintf("/jokes/random/%v", aq.JokeCount)
} else {
baseUrl += fmt.Sprintf("/jokes/%v/random", aq.JokeType)
}
aq.Url = baseUrl
return aq
}