This repository was archived by the owner on Dec 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoigbo.go
117 lines (102 loc) · 2.9 KB
/
goigbo.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
package goigbo
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
// New will accept an api key and return an interface that will have a
// GetWords function & a GetExample function
func New(apikey string, client http_Do) (*GoIgboClient, error) {
if apikey != "" {
instance := GoIgboClient{
apikey: apikey,
client: client,
}
return &instance, nil
}
return nil, &ErrApiKeyRequired{}
}
type GoIgboClient struct {
apikey string
client http_Do
}
type http_Do interface {
Do(req *http.Request) (*http.Response, error)
}
// GetWords will retrieve a keyword and return an array of revelant GetWordsOutput
func (g *GoIgboClient) GetWords(keyword string) ([]GetWordsOutput, error) {
// Create an http request
request, err := http.NewRequest("GET", "https://www.igboapi.com/api/v1/words", nil)
if err != nil {
return []GetWordsOutput{}, err
}
// Set the Request Header
request.Header.Add("X-API-Key", g.apikey)
// Apply keyword to url.Values
q := request.URL.Query()
q.Add("keyword", keyword)
request.URL.RawQuery = q.Encode()
// Execute Request
response, err := g.client.Do(request)
if err != nil {
return []GetWordsOutput{}, err
}
// http module recommends closing the body after a request
defer response.Body.Close()
var n int = -1
var outputBytes []byte
var output []GetWordsOutput
for n != 0 {
b := make([]byte, 1024)
n, err = response.Body.Read(b)
if err != io.EOF && err != nil {
return []GetWordsOutput{}, err
}
outputBytes = append(outputBytes, b...)
}
// migrate our byte array into a structure we can return
err = json.Unmarshal(bytes.Trim(outputBytes, "\x00"), &output)
if err != nil {
return []GetWordsOutput{}, &ErrJsonUnrecognized{
n: n,
bytes: outputBytes,
err: err,
}
}
return output, err
}
type GetWordsOutput struct {
WordClass string `json:"wordClass"`
Definitions []string `json:"definitions"`
Variations []string `json:"variations"`
Stems []string `json:"stems"`
Word string `json:"word"`
IsStandardIgbo *bool `json:"isStandardIgbo"`
Antonyms []string `json:"antonyms"`
Hypernyms []string `json:"hypernyms"`
Hyponyms []string `json:"hyponyms"`
Synonyms []string `json:"synonyms"`
Nsibidi string `json:"nsibidi"`
}
type GetExampleOutput struct {
Igbo string `json:"igbo"`
English string `json:"english"`
AssociatedWords []string `json:"associatedWords"`
Pronunciation string `json:"pronunciation"`
UpdatedOn string `json:"-"`
Id string `json:"id"`
}
type ErrJsonUnrecognized struct {
n int
bytes []byte
err error
}
func (e *ErrJsonUnrecognized) Error() string {
return fmt.Sprintf("failed to recognized %d bytes of json: %v (%s)", e.n, e.err, string(e.bytes))
}
type ErrApiKeyRequired struct{}
func (e *ErrApiKeyRequired) Error() string {
return "api key is required to create a new instance of goigbo"
}