From 6bf95b6133423dc945fe8ab2c5ed275a4beaaf9c Mon Sep 17 00:00:00 2001 From: Jason Hancock Date: Sat, 12 Dec 2020 14:54:14 -0800 Subject: [PATCH] Add context-aware query function. (#8) I've noticed that sometimes the LotW API takes quite a while to respond and being able to time out or cancel the queries by using a context would be nice. --- cmd/lotw-qsl/main.go | 3 ++- wrapper.go | 12 ++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/cmd/lotw-qsl/main.go b/cmd/lotw-qsl/main.go index 1f04d67..6c45b10 100644 --- a/cmd/lotw-qsl/main.go +++ b/cmd/lotw-qsl/main.go @@ -3,9 +3,10 @@ package main import ( "flag" "fmt" + "os" + "github.com/antihax/optional" "github.com/k0swe/lotw-qsl" - "os" ) func main() { diff --git a/wrapper.go b/wrapper.go index 6126669..36fc90e 100644 --- a/wrapper.go +++ b/wrapper.go @@ -8,14 +8,18 @@ import ( "context" ) -/* -Query LotW and return an ADIF string with the results. -*/ +// Query LotW and return an ADIF string with the results. func Query(user string, pw string, opts *QueryOpts) (string, error) { + return QueryContext(context.Background(), user, pw, opts) +} + +// QueryContext performs a context aware LotW query and returns an ADIF string +// with the results. +func QueryContext(ctx context.Context, user string, pw string, opts *QueryOpts) (string, error) { config := NewConfiguration() config.UserAgent = "k0swe/lotw-qsl 0.0.1" client := NewAPIClient(config) - queryResp, _, err := client.DefaultApi.Query(context.TODO(), user, pw, 1, opts) + queryResp, _, err := client.DefaultApi.Query(ctx, user, pw, 1, opts) if err != nil { return "", err }