-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordblanks.go
94 lines (71 loc) · 2.86 KB
/
wordblanks.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
type returnedWords struct {
Nouns []string `json:"noun"`
PresentVerbs []string `json:"verb_present"`
Verbs []string `json:"verb"`
Adjectives []string `json:"adjective"`
}
func buildwordBlanksURL() string {
token := "2UdLWTsozqnPNltOB4n2h6X8nf7lAH3m"
//Hardcoding is so boring
// URL for GET from https://www.wordblanks.com/mad-libs/computing/moss-memory/
// wordBlanksURL := "https://www.wordblanks.com/scripts/wb_ajax.php?token=diZo19~pw9wzzTmjssdNBAcJ2SQECyG8Ip.LwefrOnM-&method=getRandomWordSet&posArray%5B%5D=noun&posArray%5B%5D=verb_present&posArray%5B%5D=verb&posArray%5B%5D=adjective"
// partsofspeech := "&posArray%5B%5D=noun&posArray%5B%5D=verb_present&posArray%5B%5D=verb&posArray%5B%5D=adjective"
// wordBlanksURL := wordblanksapibase + "token=" + token + "&" + "method=" + method + partsofspeech
wordblanksapibase := "https://www.wordblanks.com/scripts/wb_ajax.php?"
method := "getRandomWordSet"
// Want a slice of parts of speech and then range across to build this URL
partsOfSpeech := []string{"noun", "verb_present", "verb", "adjective"}
var speechparams string
for _, part := range partsOfSpeech {
speechparams = speechparams + "&posArray%5B%5D=" + part
}
fmt.Println("Calling Word Blanks")
wordBlanksURL := wordblanksapibase + "token=" + token + "&" + "method=" + method + speechparams
fmt.Println("Request to URL ", wordBlanksURL)
return wordBlanksURL
}
//Modified code from https://medium.com/@IndianGuru/consuming-json-apis-with-go-d711efc1dcf9
func buildRequest(requestMethod string, wordBlanksURL string) *http.Request {
// Build the request
request, err := http.NewRequest("GET", wordBlanksURL, nil)
if err != nil {
log.Fatal("NewRequest: ", err)
}
return request
}
func sendRequest(request *http.Request, client http.Client) *http.Response {
// Send the request via a client
// Do sends an HTTP request and returns an HTTP response
response, err := client.Do(request)
if err != nil {
log.Fatal("Do: ", err)
}
return response
}
func callWordBlanks() {
wordBlanksURL := buildwordBlanksURL()
var madLibWords returnedWords
request := buildRequest("GET", wordBlanksURL)
// For control over HTTP client headers, redirect policy, and other settings, create a Client
// A Client is an HTTP client
client := &http.Client{}
response := sendRequest(request, *client)
// Callers should close response.Body when done reading from it
// Defer the closing of the body
defer response.Body.Close()
// Use json.Decode for reading streams of JSON data
if err := json.NewDecoder(response.Body).Decode(&madLibWords); err != nil {
log.Println(err)
}
fmt.Println("Nouns list = ", madLibWords.Nouns)
fmt.Println("Present tense verbs = ", madLibWords.PresentVerbs)
fmt.Println("Verbs = ", madLibWords.Verbs)
fmt.Println("Adjectives = ", madLibWords.Adjectives)
}