diff --git a/main.go b/main.go index ebe69f2..2c18d98 100644 --- a/main.go +++ b/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"` NoOp bool `flag:"noop,n" default:"false" description:"Fetch data from OSM but do not write"` 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"` 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"` VersionAndExit bool `flag:"version" default:"false" description:"Print version and exit"` @@ -79,6 +80,11 @@ func init() { if cfg.OSM.Password == "" || cfg.OSM.Username == "" { 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) { @@ -166,7 +172,7 @@ func main() { // Convert waypoints from GPX file to hydrants 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 { log.Fatalf("Unable to log into OSM: %s", err) } diff --git a/osm/osm.go b/osm/osm.go index febb3fc..a5ed675 100644 --- a/osm/osm.go +++ b/osm/osm.go @@ -3,6 +3,7 @@ package osm import ( "bytes" "encoding/xml" + "errors" "fmt" "io" "io/ioutil" @@ -29,20 +30,32 @@ type Client struct { 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) { + 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{ username: username, password: password, - APIBaseURL: liveAPIBaseURL, + APIBaseURL: apiEndpoint, HTTPClient: http.DefaultClient, DebugHTTPRequests: false, } - if useDevServer { - out.APIBaseURL = devAPIBaseURL + if apiEndpoint == "" { + return nil, errors.New("No API endpoint given") } u := &Wrap{User: &User{}}