-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgobreach.go
71 lines (56 loc) · 1.58 KB
/
gobreach.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
package gobreach
import (
"io"
"fmt"
"net/http"
"encoding/json"
)
type BreachEntry struct {
Email string `json:"email"`
Password string `json:"password"`
Sha1 string `json:"sha1"`
Hash string `json:"hash"`
Sources string `json:"sources"`
}
type BreachDirectoryResponse struct {
Found int `json:"found"`
Result []BreachEntry `json:"result"`
}
type BreachDirectoryClient struct {
APIKey string
}
func NewBreachDirectoryClient(apiKey string) (*BreachDirectoryClient, error) {
if apiKey == "" {
return nil, fmt.Errorf("API key cannot be empty")
}
return &BreachDirectoryClient{
APIKey: apiKey,
}, nil
}
func (client *BreachDirectoryClient) Search(term string) (*BreachDirectoryResponse, error) {
if term == "" {
return nil, fmt.Errorf("term cannot be empty; provide a username or email.")
}
url := "https://breachdirectory.p.rapidapi.com/?func=auto&term=" + term
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %v", err)
}
req.Header.Add("x-rapidapi-key", client.APIKey)
req.Header.Add("x-rapidapi-host", "breachdirectory.p.rapidapi.com")
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("error executing request: %v", err)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %v", err)
}
var response BreachDirectoryResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, fmt.Errorf("error parsing JSON response: %v", err)
}
return &response, nil
}