-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.go
110 lines (84 loc) · 2.6 KB
/
API.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
package goqrz
import (
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
// GetSessionKey takes user/pass and returns a valid session
func GetSessionKey(user string, pass string, agent string) (string, error) {
xmlBytes, err := getXML(
fmt.Sprintf("https://xmldata.qrz.com/xml/1.34/?username=%s;password=%s;agent=%s",
user, pass, agent))
if err != nil {
return "", err
}
var database QRZDatabase
xml.Unmarshal(xmlBytes, &database)
newSession := database.Session
if newSession.Error != "" {
return "", errors.New(newSession.Error)
}
return newSession.Key, nil
}
// GetSession creates a session object from just a key
func GetSession(key string, agent string) (Session, error) {
xmlBytes, err := getXML(fmt.Sprintf("https://xmldata.qrz.com/xml/1.34/?s=%s;agent=%s", key, agent))
if err != nil {
return Session{}, err
}
var database QRZDatabase
xml.Unmarshal(xmlBytes, &database)
if database.Session.Error != "" {
return database.Session, errors.New(database.Session.Error)
}
return database.Session, nil
}
// GetCallsign takes a callsign and returns the QRZ information on that
// callsign.
func GetCallsign(key string, callsign string, agent string) (Callsign, error) {
xmlBytes, err := getXML(fmt.Sprintf("https://xmldata.qrz.com/xml/1.34/?s=%s;callsign=%s;agent=%s", key, callsign, agent))
if err != nil {
return Callsign{}, err
}
var database QRZDatabase
xml.Unmarshal(xmlBytes, &database)
if database.Session.Error != "" {
return Callsign{}, errors.New(database.Session.Error)
}
return database.Callsign, nil
}
// GetDXCC takes a dxcc id and returns the QRZ information on that
// region.
func GetDXCC(key string, dxcc string, agent string) (DXCC, error) {
if dxcc == "all" {
return DXCC{}, errors.New("get all DXCC not implemented in this function")
}
xmlBytes, err := getXML(fmt.Sprintf("https://xmldata.qrz.com/xml/1.34/?s=%s;dxcc=%s;agent=%s", key, dxcc, agent))
if err != nil {
return DXCC{}, err
}
var database QRZDatabase
xml.Unmarshal(xmlBytes, &database)
if database.Session.Error != "" {
return DXCC{}, errors.New(database.Session.Error)
}
return database.DXCC, nil
}
// from: https://gist.github.com/james2doyle/e2f05b5756e4ee46848a8d987405f152
func getXML(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return []byte{}, fmt.Errorf("GET error: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return []byte{}, fmt.Errorf("Status error: %v", resp.StatusCode)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{}, fmt.Errorf("Read body: %v", err)
}
return data, nil
}