mirror of
https://github.com/Luzifer/gpxhydrant.git
synced 2024-12-22 20:21:17 +00:00
Allow specifying API endpoint to contact
in order to enable testing against different API deployments like master, tomh, ... Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
094ff1d039
commit
33264df876
2 changed files with 25 additions and 6 deletions
10
main.go
10
main.go
|
@ -22,9 +22,10 @@ var (
|
||||||
MachRange int64 `flag:"match-range" default:"5" description:"Range of meters to match GPX hydrants to OSM nodes"`
|
MachRange int64 `flag:"match-range" default:"5" description:"Range of meters to match GPX hydrants to OSM nodes"`
|
||||||
NoOp bool `flag:"noop,n" default:"false" description:"Fetch data from OSM but do not write"`
|
NoOp bool `flag:"noop,n" default:"false" description:"Fetch data from OSM but do not write"`
|
||||||
OSM struct {
|
OSM struct {
|
||||||
|
APIURL string `flag:"osm-apiurl" default:"https://api.openstreetmap.org/api/0.6" description:"API base url to contact"`
|
||||||
Username string `flag:"osm-user" description:"Username to log into OSM"`
|
Username string `flag:"osm-user" description:"Username to log into OSM"`
|
||||||
Password string `flag:"osm-pass" description:"Password for osm-user"`
|
Password string `flag:"osm-pass" description:"Password for osm-user"`
|
||||||
UseDev bool `flag:"osm-dev" default:"false" description:"Switch to dev API"`
|
UseDev bool `flag:"osm-dev" default:"false" description:"Switch to dev API (Deprecated: Use --osm-apiurl)"`
|
||||||
}
|
}
|
||||||
Pressure int64 `flag:"pressure" default:"4" description:"Pressure of the water grid"`
|
Pressure int64 `flag:"pressure" default:"4" description:"Pressure of the water grid"`
|
||||||
VersionAndExit bool `flag:"version" default:"false" description:"Print version and exit"`
|
VersionAndExit bool `flag:"version" default:"false" description:"Print version and exit"`
|
||||||
|
@ -79,6 +80,11 @@ func init() {
|
||||||
if cfg.OSM.Password == "" || cfg.OSM.Username == "" {
|
if cfg.OSM.Password == "" || cfg.OSM.Username == "" {
|
||||||
log.Fatalf("osm-pass / osm-user are required parameters")
|
log.Fatalf("osm-pass / osm-user are required parameters")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cfg.OSM.UseDev {
|
||||||
|
// Migration for deprecated flag
|
||||||
|
cfg.OSM.APIURL = "https://api06.dev.openstreetmap.org/api/0.6"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func hydrantsFromGPXFile() ([]*hydrant, bounds) {
|
func hydrantsFromGPXFile() ([]*hydrant, bounds) {
|
||||||
|
@ -166,7 +172,7 @@ func main() {
|
||||||
// Convert waypoints from GPX file to hydrants
|
// Convert waypoints from GPX file to hydrants
|
||||||
hydrants, bds := hydrantsFromGPXFile()
|
hydrants, bds := hydrantsFromGPXFile()
|
||||||
|
|
||||||
osmClient, err := osm.New(cfg.OSM.Username, cfg.OSM.Password, cfg.OSM.UseDev)
|
osmClient, err := osm.NewWithAPIEndpoint(cfg.OSM.Username, cfg.OSM.Password, cfg.OSM.APIURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Unable to log into OSM: %s", err)
|
log.Fatalf("Unable to log into OSM: %s", err)
|
||||||
}
|
}
|
||||||
|
|
21
osm/osm.go
21
osm/osm.go
|
@ -3,6 +3,7 @@ package osm
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -29,20 +30,32 @@ type Client struct {
|
||||||
DebugHTTPRequests bool
|
DebugHTTPRequests bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// New instantiates a new client and retrieves information about the current user. Set useDevServer to true to change the API URL to the api06.dev.openstreetmap.org server.
|
// New instantiates a new client and retrieves information about the
|
||||||
|
// current user. Set useDevServer to true to change the API URL to the
|
||||||
|
// api06.dev.openstreetmap.org server.
|
||||||
func New(username, password string, useDevServer bool) (*Client, error) {
|
func New(username, password string, useDevServer bool) (*Client, error) {
|
||||||
|
if useDevServer {
|
||||||
|
return NewWithAPIEndpoint(username, password, devAPIBaseURL)
|
||||||
|
}
|
||||||
|
return NewWithAPIEndpoint(username, password, liveAPIBaseURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewWithAPIEndpoint instantiates a new client and retrieves
|
||||||
|
// information about the current user. Set apiEndpoint to your desired API
|
||||||
|
// endpoint (e.g. https://api06.dev.openstreetmap.org/api/0.6)
|
||||||
|
func NewWithAPIEndpoint(username, password, apiEndpoint string) (*Client, error) {
|
||||||
out := &Client{
|
out := &Client{
|
||||||
username: username,
|
username: username,
|
||||||
password: password,
|
password: password,
|
||||||
|
|
||||||
APIBaseURL: liveAPIBaseURL,
|
APIBaseURL: apiEndpoint,
|
||||||
HTTPClient: http.DefaultClient,
|
HTTPClient: http.DefaultClient,
|
||||||
|
|
||||||
DebugHTTPRequests: false,
|
DebugHTTPRequests: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
if useDevServer {
|
if apiEndpoint == "" {
|
||||||
out.APIBaseURL = devAPIBaseURL
|
return nil, errors.New("No API endpoint given")
|
||||||
}
|
}
|
||||||
|
|
||||||
u := &Wrap{User: &User{}}
|
u := &Wrap{User: &User{}}
|
||||||
|
|
Loading…
Reference in a new issue