diff --git a/pkg/source/loriot/api/api.go b/pkg/source/loriot/api/api.go index da375e9..047deb7 100644 --- a/pkg/source/loriot/api/api.go +++ b/pkg/source/loriot/api/api.go @@ -41,8 +41,8 @@ func SetURLPrefix(insecure bool) { } func SetAPIURL(url string) { - if len(strings.Split(url, "://")) <= 1 { - url = urlPrefix + url + if s := strings.Split(url, "://"); len(s) > 1 { + url = strings.Join(s[1:], "") } apiURL = url } @@ -52,7 +52,15 @@ func SetAPIKey(key string) { } func NewRequest(method, path string, body io.Reader) (*http.Request, error) { - req, err := http.NewRequest(method, apiURL+path, body) + switch { + case apiURL == "": + return nil, errNoAPIURL.New() + + case apiKey == "": + return nil, errNoAPIKey.New() + } + + req, err := http.NewRequest(method, urlPrefix+apiURL+path, body) if err != nil { return nil, err } @@ -143,6 +151,10 @@ func GetPaginatedDevices(appID string, page int) (*PaginatedDevices, error) { } defer resp.Body.Close() + if sc := resp.StatusCode; sc < 200 || sc >= 300 { + return nil, errInvalidStatusCode.WithAttributes("code", sc) + } + body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err diff --git a/pkg/source/loriot/config/config.go b/pkg/source/loriot/config/config.go index 4772b1f..a131566 100644 --- a/pkg/source/loriot/config/config.go +++ b/pkg/source/loriot/config/config.go @@ -15,7 +15,6 @@ package config import ( - "flag" "os" "github.com/spf13/pflag" @@ -31,18 +30,18 @@ func New() (*Config, *pflag.FlagSet) { flags.StringVar(&cfg.APIKey, "api-key", - os.Getenv("API_KEY"), + os.Getenv("LORIOT_API_KEY"), "Loriot API Key") flags.StringVar(&cfg.URL, "api-url", - os.Getenv("API_URL"), + os.Getenv("LORIOT_API_URL"), "Loriot API URL") flags.StringVar(&cfg.AppID, "app-id", - os.Getenv("APP_ID"), + os.Getenv("LORIOT_APP_ID"), "Loriot APP ID") - flag.BoolVar(&cfg.Insecure, - "api-insecure", - os.Getenv("API_INSECURE") == "1", + flags.BoolVar(&cfg.Insecure, + "insecure", + os.Getenv("LORIOT_INSECURE") == "1", "Do not connect to Loriot over TLS") return cfg, flags diff --git a/pkg/source/loriot/loriot.go b/pkg/source/loriot/loriot.go index 734e07f..fc1c568 100644 --- a/pkg/source/loriot/loriot.go +++ b/pkg/source/loriot/loriot.go @@ -16,17 +16,12 @@ package loriot import ( "go.thethings.network/lorawan-stack-migrate/pkg/source" - "go.thethings.network/lorawan-stack-migrate/pkg/source/loriot/api" "go.thethings.network/lorawan-stack-migrate/pkg/source/loriot/config" ) func init() { cfg, flags := config.New() - api.SetURLPrefix(cfg.Insecure) - api.SetAPIURL(cfg.URL) - api.SetAPIKey(cfg.APIKey) - source.RegisterSource(source.Registration{ Name: "loriot", Description: "Migrate from Loriot LoRaWAN Network Server", diff --git a/pkg/source/loriot/source.go b/pkg/source/loriot/source.go index 4e58034..576db40 100644 --- a/pkg/source/loriot/source.go +++ b/pkg/source/loriot/source.go @@ -32,7 +32,11 @@ type Source struct { func createNewSource(cfg *config.Config) source.CreateSource { return func(ctx context.Context, rootCfg source.Config) (source.Source, error) { - return Source{}, nil + api.SetURLPrefix(cfg.Insecure) + api.SetAPIURL(cfg.URL) + api.SetAPIKey(cfg.APIKey) + + return Source{Config: cfg}, nil } }