1
0
Fork 0
mirror of https://github.com/Luzifer/gpxhydrant.git synced 2024-11-08 15:30:00 +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:
Knut Ahlers 2018-06-17 12:46:02 +02:00
parent 094ff1d039
commit 33264df876
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
2 changed files with 25 additions and 6 deletions

10
main.go
View file

@ -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)
}

View file

@ -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{}}