Skip to content

Commit

Permalink
Plumb through Context
Browse files Browse the repository at this point in the history
  • Loading branch information
xylo04 committed Jun 27, 2021
1 parent cc48531 commit b252601
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,38 @@ import (
"github.com/antihax/optional"
)

const agent = "xylo-go-1.0"
const agent = "k0swe-go-1.0"

var cachedUser = ""
var cachedSession = ""

func Lookup(user *string, pw *string, call *string) (*QrzDatabase, error) {
func Lookup(ctx context.Context, user *string, pw *string, call *string) (*QrzDatabase, error) {
config := NewConfiguration()
config.UserAgent = agent
client := NewAPIClient(config)

sessionKey, err := login(user, pw, client)
sessionKey, err := login(ctx, user, pw, client)
if err != nil {
return nil, err
}

lookupResp, err := lookupInner(sessionKey, call, client)
lookupResp, err := lookupInner(ctx, sessionKey, call, client)
if err != nil {
cachedSession = ""
// TODO: maybe session key expired; retry login?
return nil, err
}
return lookupResp, nil
}

func login(user *string, pw *string, client *APIClient) (string, error) {
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(context.TODO(), req)
sessResp, _, err := client.DefaultApi.RootGet(ctx, req)
if err != nil {
return "", err
}
Expand All @@ -51,12 +50,12 @@ func login(user *string, pw *string, client *APIClient) (string, error) {
return sessionKey, err
}

func lookupInner(sessionKey string, call *string, client *APIClient) (*QrzDatabase, error) {
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(context.TODO(), req)
lookupResp, _, err := client.DefaultApi.RootGet(ctx, req)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit b252601

Please sign in to comment.