diff --git a/elevation_test.go b/elevation_test.go index 8818dfd..0e1ca02 100644 --- a/elevation_test.go +++ b/elevation_test.go @@ -35,10 +35,8 @@ func TestGetElevationSRTM1ReturnsDataOnValidInput(t *testing.T) { ts := newElevationTestServer(srtm1, wantReqURI, t) defer ts.Close() - client, err := geonames.NewClient("DummyUser", geonames.WithBaseURL(ts.URL)) - if err != nil { - t.Fatal(err) - } + client := geonames.NewClient("DummyUser") + client.BaseURL = ts.URL want := geonames.Elevation{ Type: "srtm1", @@ -66,10 +64,8 @@ func TestGetElevationSRTM3ReturnsDataOnValidInput(t *testing.T) { ts := newElevationTestServer(srtm3, wantReqURI, t) defer ts.Close() - client, err := geonames.NewClient("DummyUser", geonames.WithBaseURL(ts.URL)) - if err != nil { - t.Fatal(err) - } + client := geonames.NewClient("DummyUser") + client.BaseURL = ts.URL want := geonames.Elevation{ Type: "srtm3", @@ -97,10 +93,8 @@ func TestGetElevationAstergdemReturnsDataOnValidInput(t *testing.T) { ts := newElevationTestServer(astergdem, wantReqURI, t) defer ts.Close() - client, err := geonames.NewClient("DummyUser", geonames.WithBaseURL(ts.URL)) - if err != nil { - t.Fatal(err) - } + client := geonames.NewClient("DummyUser") + client.BaseURL = ts.URL want := geonames.Elevation{ Type: "astergdem", @@ -128,10 +122,8 @@ func TestGetElevationGTOPO30ReturnsDataOnValidInput(t *testing.T) { ts := newElevationTestServer(gtopo30, wantReqURI, t) defer ts.Close() - client, err := geonames.NewClient("DummyUser", geonames.WithBaseURL(ts.URL)) - if err != nil { - t.Fatal(err) - } + client := geonames.NewClient("DummyUser") + client.BaseURL = ts.URL want := geonames.Elevation{ Type: "gtopo30", diff --git a/examples/postal/go.mod b/examples/postal/go.mod deleted file mode 100644 index cd3f7e9..0000000 --- a/examples/postal/go.mod +++ /dev/null @@ -1,5 +0,0 @@ -module example-postal - -go 1.19 - -require github.com/qba73/geonames v0.0.2 diff --git a/examples/postal/go.sum b/examples/postal/go.sum deleted file mode 100644 index e0c9d62..0000000 --- a/examples/postal/go.sum +++ /dev/null @@ -1,3 +0,0 @@ -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/qba73/geonames v0.0.2 h1:MWYZ+YbG5uEdzFyG0ZMo0xLBU0jCQT/P2635Q2S598s= -github.com/qba73/geonames v0.0.2/go.mod h1:2YIjYC03dZXrYz8D2OZCMRwGLKn/6qaSEJ0yPShv6E4= diff --git a/examples/postal/main.go b/examples/postal/main.go index e86489a..631253e 100644 --- a/examples/postal/main.go +++ b/examples/postal/main.go @@ -3,19 +3,13 @@ package main import ( "fmt" "log" - "os" "github.com/qba73/geonames" ) func main() { // We exported valid "GEONAMES_USER" env var - geo, err := geonames.NewClient(os.Getenv("GEONAMES_USER")) - if err != nil { - panic(err) - } - - codes, err := geo.GetPostCode("Fort William", "UK") + codes, err := geonames.GetPostCode("Fort William", "UK") if err != nil { log.Fatal(err) } diff --git a/examples/wikipedia/go.mod b/examples/wikipedia/go.mod deleted file mode 100644 index bd45b56..0000000 --- a/examples/wikipedia/go.mod +++ /dev/null @@ -1,5 +0,0 @@ -module example-wikipedia - -go 1.19 - -require github.com/qba73/geonames v0.0.2 diff --git a/examples/wikipedia/go.sum b/examples/wikipedia/go.sum deleted file mode 100644 index e0c9d62..0000000 --- a/examples/wikipedia/go.sum +++ /dev/null @@ -1,3 +0,0 @@ -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/qba73/geonames v0.0.2 h1:MWYZ+YbG5uEdzFyG0ZMo0xLBU0jCQT/P2635Q2S598s= -github.com/qba73/geonames v0.0.2/go.mod h1:2YIjYC03dZXrYz8D2OZCMRwGLKn/6qaSEJ0yPShv6E4= diff --git a/examples/wikipedia/main.go b/examples/wikipedia/main.go index e86489a..631253e 100644 --- a/examples/wikipedia/main.go +++ b/examples/wikipedia/main.go @@ -3,19 +3,13 @@ package main import ( "fmt" "log" - "os" "github.com/qba73/geonames" ) func main() { // We exported valid "GEONAMES_USER" env var - geo, err := geonames.NewClient(os.Getenv("GEONAMES_USER")) - if err != nil { - panic(err) - } - - codes, err := geo.GetPostCode("Fort William", "UK") + codes, err := geonames.GetPostCode("Fort William", "UK") if err != nil { log.Fatal(err) } diff --git a/geonames.go b/geonames.go index 83b9580..aea3d02 100644 --- a/geonames.go +++ b/geonames.go @@ -3,53 +3,13 @@ package geonames import ( "context" "encoding/json" - "errors" "fmt" "io" "net/http" + "os" "time" ) -const ( - libraryVersion = "0.1" - userAgent = "geonames/" + libraryVersion -) - -type option func(*Client) error - -// WithHTTPClient configures the Geonames HTTP Client. -func WithHTTPClient(h *http.Client) option { - return func(c *Client) error { - if h == nil { - return errors.New("nil http Client") - } - c.HTTPClient = h - return nil - } -} - -// WithBaseURL configures a base URL for the Geonames client. -func WithBaseURL(url string) option { - return func(c *Client) error { - if url == "" { - return errors.New("nil baseURL") - } - c.BaseURL = url - return nil - } -} - -// WithHTTPHeader configures custom HTTP Headers used by the Geonames client. -func WithHTTPHeaders(header http.Header) option { - return func(c *Client) error { - if header == nil { - return errors.New("nil HTTP headers") - } - c.Headers = header - return nil - } -} - // Client holds data required for communicating with the Geonames Web Services. type Client struct { UserName string @@ -61,24 +21,16 @@ type Client struct { Headers map[string][]string } -var DefaultClient = Client{ - UserName: "demo", - BaseURL: "http://api.geonames.org", - HTTPClient: http.DefaultClient, - Headers: map[string][]string{"Content-Type": {"application/json"}}, -} - -// NewDemoClient creates a demo client. Demo client -// does not require username registration at geonames.org website. -func NewDemoClient() *Client { - return &DefaultClient -} +const ( + libraryVersion = "0.1" + userAgent = "geonames/" + libraryVersion +) // NewClient creates a new client for GeoNames Web service. // // The username has to be registered at the GeoNames.org website. // HTTP requests without a valid username will return 403 HTTP errors. -func NewClient(username string, options ...option) (*Client, error) { +func NewClient(username string) *Client { c := Client{ UserName: username, UserAgent: userAgent, @@ -91,12 +43,7 @@ func NewClient(username string, options ...option) (*Client, error) { "Content-Type": {"application/json"}, }, } - for _, opt := range options { - if err := opt(&c); err != nil { - return nil, fmt.Errorf("creating geonames client: %w", err) - } - } - return &c, nil + return &c } func (c Client) get(ctx context.Context, url string, data any) error { @@ -130,3 +77,23 @@ type Position struct { Lat float64 Lng float64 } + +var DemoClient = &Client{ + UserName: "demo", + BaseURL: "http://api.geonames.org", + HTTPClient: http.DefaultClient, + Headers: map[string][]string{ + "Content-Type": {"application/json"}, + }, +} + +var ClientFromEnv = &Client{ + UserName: os.Getenv("GEONAMES_USER"), + BaseURL: "http://api.geonames.org", + HTTPClient: &http.Client{ + Timeout: 10 * time.Second, + }, + Headers: map[string][]string{ + "Content-Type": {"application/json"}, + }, +} diff --git a/geonames_test.go b/geonames_test.go index 4f10647..9a7a0ae 100644 --- a/geonames_test.go +++ b/geonames_test.go @@ -70,10 +70,6 @@ func TestWikipediaResolvesGeoNameOnValidInput(t *testing.T) { defer ts.Close() name, country := "Castlebar", "IE" - client, err := geonames.NewClient("DummyUser", geonames.WithBaseURL(ts.URL)) - if err != nil { - t.Fatal(err) - } - - client.GetPlace(context.Background(), name, country, 1) + c := geonames.NewClient("DummyUser") + c.GetPlace(context.Background(), name, country, 1) } diff --git a/go.mod b/go.mod index 4dc8c0b..838e1fc 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module github.com/qba73/geonames -go 1.21 +go 1.22 require github.com/google/go-cmp v0.6.0 diff --git a/postal.go b/postal.go index e8fa899..880005d 100644 --- a/postal.go +++ b/postal.go @@ -72,3 +72,8 @@ func (c Client) buildPostalURL(placeName, countryCode string) (string, error) { u.RawQuery = params.Encode() return u.String(), nil } + +// GetPostalCode takes place and country and returns postal codes. +func GetPostCode(place, country string) ([]PostalCode, error) { + return ClientFromEnv.GetPostCode(context.Background(), place, country) +} diff --git a/postal_test.go b/postal_test.go index b9baf4a..063643d 100644 --- a/postal_test.go +++ b/postal_test.go @@ -19,10 +19,8 @@ func TestGetPostalCodes_ReturnsSingleValueOnValidInput(t *testing.T) { ) defer ts.Close() - client, err := geonames.NewClient("DummyUser", geonames.WithBaseURL(ts.URL)) - if err != nil { - t.Fatal(err) - } + client := geonames.NewClient("DummyUser") + client.BaseURL = ts.URL place, country := "Castlebar", "IE" got, err := client.GetPostCode(context.Background(), place, country) @@ -63,10 +61,8 @@ func TestGetPostalCodes_ReturnsMultipleValuesOnValidInput(t *testing.T) { ) defer ts.Close() - client, err := geonames.NewClient("DummyUser", geonames.WithBaseURL(ts.URL)) - if err != nil { - t.Fatal(err) - } + client := geonames.NewClient("DummyUser") + client.BaseURL = ts.URL place, country := "Dublin", "IE" got, err := client.GetPostCode(context.Background(), place, country) diff --git a/wikipedia.go b/wikipedia.go index 1b6fd46..ed70a7d 100644 --- a/wikipedia.go +++ b/wikipedia.go @@ -90,3 +90,9 @@ func (c Client) buildWikiURL(place, country string, maxResults int) (string, err u.RawQuery = params.Encode() return u.String(), nil } + +// GetPlace takes place name, country and max results and returns +// a list of names associated with the place. +func GetPlace(name, country string, maxResults int) ([]Geoname, error) { + return ClientFromEnv.GetPlace(context.Background(), name, country, maxResults) +} diff --git a/wikipedia_test.go b/wikipedia_test.go index 390dc88..26ece71 100644 --- a/wikipedia_test.go +++ b/wikipedia_test.go @@ -17,10 +17,8 @@ func TestGetPlace_RetrievesSingleGeoNameOnValidInput(t *testing.T) { ts := newTestServer(testFile, wantReqURI, t) defer ts.Close() - client, err := geonames.NewClient("DummyUser", geonames.WithBaseURL(ts.URL)) - if err != nil { - t.Fatal(err) - } + client := geonames.NewClient("DummyUser") + client.BaseURL = ts.URL name := "Castlebar" country := "IE"