Skip to content

Commit

Permalink
Handle query params in koji url
Browse files Browse the repository at this point in the history
This checks for and properly preserves the query params that should be used
when querying for the project's feature collections..

Fixes #51
  • Loading branch information
comstud committed Jul 23, 2024
1 parent f5f346b commit 5726b21
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 31 deletions.
9 changes: 6 additions & 3 deletions areas/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ type Config struct {
CacheDir string `koanf:"cache_dir"`
CacheFilename string `koanf:"cache_filename"`

// computed during validation
KojiBaseUrl string `koanf:"-"`
KojiProject string `koanf:"-"`
// computed during validation:
KojiBaseUrl string `koanf:"-"`
KojiProject string `koanf:"-"`
KojiProjectParams url.Values `koanf:"-"`
}

func (cfg *Config) Validate() error {
Expand All @@ -44,7 +45,9 @@ func (cfg *Config) Validate() error {
return fmt.Errorf("'areas.koji_url' looks malformed: the project is missing")
}

cfg.KojiProjectParams = uri.Query()
uri.Path = ""
uri.RawQuery = ""
cfg.KojiBaseUrl = uri.String()

return nil
Expand Down
19 changes: 11 additions & 8 deletions areas/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/url"
"os"
"path/filepath"
"sync/atomic"
Expand Down Expand Up @@ -48,13 +49,14 @@ func (cache *AreasCache) GetArea(areaName string) *geojson.Feature {
}

type AreasLoader struct {
logger *logrus.Logger
kojiClient *koji_client.APIClient
kojiProject string
filename string
cacheDir string
cacheFilename string
areasCache atomic.Pointer[AreasCache]
logger *logrus.Logger
kojiClient *koji_client.APIClient
kojiProject string
kojiProjectParams url.Values
filename string
cacheDir string
cacheFilename string
areasCache atomic.Pointer[AreasCache]
}

func (loader *AreasLoader) FullCachePath() string {
Expand Down Expand Up @@ -145,7 +147,7 @@ func (loader *AreasLoader) ReloadAreas(ctx context.Context) (err error) {
loader.logger.Infof("Reloading areas from koji project '%s'", loader.kojiProject)

var fc *geojson.FeatureCollection
fc, err = loader.kojiClient.GetFeatureCollection(ctx, loader.kojiProject)
fc, err = loader.kojiClient.GetFeatureCollection(ctx, loader.kojiProject, loader.kojiProjectParams)
if err != nil {
return
}
Expand Down Expand Up @@ -183,6 +185,7 @@ func NewAreasLoader(logger *logrus.Logger, config Config) (*AreasLoader, error)

if config.Filename == "" {
loader.kojiProject = config.KojiProject
loader.kojiProjectParams = config.KojiProjectParams
loader.kojiClient, err = koji_client.NewAPIClient(
logger,
config.KojiBaseUrl,
Expand Down
19 changes: 11 additions & 8 deletions exporters/koji.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package exporters

import (
"context"
"net/url"

"github.com/paulmach/orb/geojson"
"github.com/sirupsen/logrus"
Expand All @@ -10,17 +11,18 @@ import (
)

type KojiExporter struct {
logger *logrus.Logger
kojiCli *koji_client.APIClient
projectName string
logger *logrus.Logger
kojiCli *koji_client.APIClient
projectName string
projectParams url.Values
}

func (*KojiExporter) ExporterName() string {
return "koji"
}

func (exporter *KojiExporter) ExportFeatures(ctx context.Context) ([]*geojson.Feature, error) {
fc, err := exporter.kojiCli.GetFeatureCollection(ctx, exporter.projectName)
fc, err := exporter.kojiCli.GetFeatureCollection(ctx, exporter.projectName, exporter.projectParams)
if err != nil {
return nil, err
}
Expand All @@ -47,11 +49,12 @@ func (exporter *KojiExporter) ExportFeatures(ctx context.Context) ([]*geojson.Fe

}

func NewKojiExporter(logger *logrus.Logger, kojiCli *koji_client.APIClient, projectName string) (*KojiExporter, error) {
func NewKojiExporter(logger *logrus.Logger, kojiCli *koji_client.APIClient, projectName string, projectParams url.Values) (*KojiExporter, error) {
exporter := &KojiExporter{
logger: logger,
kojiCli: kojiCli,
projectName: projectName,
logger: logger,
kojiCli: kojiCli,
projectName: projectName,
projectParams: projectParams,
}
return exporter, nil
}
9 changes: 7 additions & 2 deletions koji_client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ func (cli *APIClient) makePublicRequest(ctx context.Context, method, url_str str
return resp, nil
}

func (cli *APIClient) GetFeatureCollection(ctx context.Context, project string) (*geojson.FeatureCollection, error) {
resp, err := cli.makePublicRequest(ctx, "GET", "/geofence/feature-collection/"+project, nil)
func (cli *APIClient) GetFeatureCollection(ctx context.Context, project string, params url.Values) (*geojson.FeatureCollection, error) {
urlPath := "/geofence/feature-collection/" + project
if paramsStr := params.Encode(); paramsStr != "" {
urlPath += "?" + paramsStr
}

resp, err := cli.makePublicRequest(ctx, "GET", urlPath, nil)
if err != nil {
return nil, err
}
Expand Down
23 changes: 13 additions & 10 deletions processor/nest_loader/koji.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nest_loader
import (
"context"
"fmt"
"net/url"

"github.com/sirupsen/logrus"

Expand All @@ -12,18 +13,19 @@ import (
)

type KojiNestLoader struct {
logger *logrus.Logger
kojiCli *koji_client.APIClient
projectName string
nestsDBStore *db_store.NestsDBStore
logger *logrus.Logger
kojiCli *koji_client.APIClient
projectName string
projectParams url.Values
nestsDBStore *db_store.NestsDBStore
}

func (*KojiNestLoader) LoaderName() string {
return "koji"
}

func (loader *KojiNestLoader) LoadNests(ctx context.Context) ([]*models.Nest, error) {
fc, err := loader.kojiCli.GetFeatureCollection(ctx, loader.projectName)
fc, err := loader.kojiCli.GetFeatureCollection(ctx, loader.projectName, loader.projectParams)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -69,12 +71,13 @@ func (loader *KojiNestLoader) LoadNests(ctx context.Context) ([]*models.Nest, er
return kojiNests, nil
}

func NewKojiNestLoader(logger *logrus.Logger, kojiCli *koji_client.APIClient, projectName string, nestsDBStore *db_store.NestsDBStore) *KojiNestLoader {
func NewKojiNestLoader(logger *logrus.Logger, kojiCli *koji_client.APIClient, projectName string, projectParams url.Values, nestsDBStore *db_store.NestsDBStore) *KojiNestLoader {
st := &KojiNestLoader{
logger: logger,
kojiCli: kojiCli,
projectName: projectName,
nestsDBStore: nestsDBStore,
logger: logger,
kojiCli: kojiCli,
projectName: projectName,
projectParams: projectParams,
nestsDBStore: nestsDBStore,
}
return st
}

0 comments on commit 5726b21

Please sign in to comment.