Skip to content

Commit

Permalink
dev: Fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
happyRip committed Aug 4, 2023
1 parent 8ad5102 commit e8f656c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
18 changes: 15 additions & 3 deletions pkg/source/loriot/api/api.go
Expand Up @@ -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
}
Expand All @@ -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()

Check failure on line 57 in pkg/source/loriot/api/api.go

View workflow job for this annotation

GitHub Actions / Go Code Quality

undefined: errNoAPIURL

Check failure on line 57 in pkg/source/loriot/api/api.go

View workflow job for this annotation

GitHub Actions / Go Code Quality

undefined: errNoAPIURL

Check failure on line 57 in pkg/source/loriot/api/api.go

View workflow job for this annotation

GitHub Actions / Go Tests

undefined: errNoAPIURL

case apiKey == "":
return nil, errNoAPIKey.New()

Check failure on line 60 in pkg/source/loriot/api/api.go

View workflow job for this annotation

GitHub Actions / Go Code Quality

undefined: errNoAPIKey

Check failure on line 60 in pkg/source/loriot/api/api.go

View workflow job for this annotation

GitHub Actions / Go Code Quality

undefined: errNoAPIKey

Check failure on line 60 in pkg/source/loriot/api/api.go

View workflow job for this annotation

GitHub Actions / Go Tests

undefined: errNoAPIKey
}

req, err := http.NewRequest(method, urlPrefix+apiURL+path, body)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -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)

Check failure on line 155 in pkg/source/loriot/api/api.go

View workflow job for this annotation

GitHub Actions / Go Code Quality

undefined: errInvalidStatusCode

Check failure on line 155 in pkg/source/loriot/api/api.go

View workflow job for this annotation

GitHub Actions / Go Code Quality

undefined: errInvalidStatusCode

Check failure on line 155 in pkg/source/loriot/api/api.go

View workflow job for this annotation

GitHub Actions / Go Tests

undefined: errInvalidStatusCode
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
Expand Down
13 changes: 6 additions & 7 deletions pkg/source/loriot/config/config.go
Expand Up @@ -15,7 +15,6 @@
package config

import (
"flag"
"os"

"github.com/spf13/pflag"
Expand All @@ -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
Expand Down
5 changes: 0 additions & 5 deletions pkg/source/loriot/loriot.go
Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion pkg/source/loriot/source.go
Expand Up @@ -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
}
}

Expand Down

0 comments on commit e8f656c

Please sign in to comment.