-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper.go
63 lines (55 loc) · 1.52 KB
/
wrapper.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
package qrz
import (
"context"
"errors"
"github.com/antihax/optional"
)
const agent = "k0swe-go-1.0"
var cachedUser = ""
var cachedSession = ""
func Lookup(ctx context.Context, user *string, pw *string, call *string) (*QrzDatabase, error) {
config := NewConfiguration()
config.UserAgent = agent
client := NewAPIClient(config)
sessionKey, err := login(ctx, user, pw, client)
if err != nil {
return nil, err
}
lookupResp, err := lookupInner(ctx, sessionKey, call, client)
if err != nil {
cachedSession = ""
return nil, err
}
return lookupResp, nil
}
func login(ctx context.Context, user *string, pw *string, client *APIClient) (string, error) {
if cachedUser == *user && cachedSession != "" {
return cachedSession, nil
}
req := new(RootGetOpts)
req.Username = optional.NewString(*user)
req.Password = optional.NewString(*pw)
req.Agent = optional.NewString(agent)
sessResp, _, err := client.DefaultApi.RootGet(ctx, req)
if err != nil {
return "", err
}
sessionKey := sessResp.Session.Key
if sessionKey == "" {
return "", errors.New(sessResp.Session.Error)
}
cachedUser = *user
cachedSession = sessionKey
return sessionKey, err
}
func lookupInner(ctx context.Context, sessionKey string, call *string, client *APIClient) (*QrzDatabase, error) {
req := new(RootGetOpts)
req.S = optional.NewString(sessionKey)
req.Agent = optional.NewString(agent)
req.Callsign = optional.NewString(*call)
lookupResp, _, err := client.DefaultApi.RootGet(ctx, req)
if err != nil {
return nil, err
}
return &lookupResp, nil
}