Commit 1cbdcfaf authored by endk-mirror-svc's avatar endk-mirror-svc

Tonghts work

parent adae708a
Pipeline #24856279 passed with stages
in 1 minute and 43 seconds
......@@ -2,6 +2,7 @@ package eds
import (
"context"
"fmt"
"net/url"
"time"
)
......@@ -59,11 +60,12 @@ type DatasetListOptions struct {
}
// List returns a list of datasets available from Energi Data Service
func (s *DatasetService) List(ctx context.Context, opt *DatasetListOptions) ([]*Dataset, error) {
action := "package_list"
//TODO: Add function (Preferably to eds.go so it can be reused) that takes
// a URL/string and a struct of options and converts those to query string.
req, err := s.client.NewRequest("GET", action, nil)
func (s *DatasetService) List(ctx context.Context, opt DatasetListOptions) ([]*Dataset, error) {
url, err := AppendOptions("package_list", opt)
if err != nil {
return nil, err
}
req, err := s.client.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
......@@ -73,6 +75,9 @@ func (s *DatasetService) List(ctx context.Context, opt *DatasetListOptions) ([]*
return nil, err
}
fmt.Printf("%+v\n", req)
fmt.Printf("%+v\n", data.Result)
//Placeholder
return nil, nil
}
package eds_test
import (
"context"
"testing"
eds "gitlab.com/MadsRC/go-eds"
)
func TestPackageShow(t *testing.T) {
client := eds.NewClient(nil)
opts := eds.DatasetListOptions{Limit: 2}
ctx := context.Background()
d, err := client.Dataset.List(ctx, opts)
t.Log(err)
t.Log(d)
}
......@@ -105,9 +105,9 @@ type Resource struct {
// APIResponse represents the base response from the Energi Data Service API.
type APIResponse struct {
Help *string `json:"help,omitempty"`
Success *bool `json:"success,omitempty"`
Result *interface{} `json:"result,omitempty"`
Help string `json:"help,omitempty"`
Success bool `json:"success,omitempty"`
Result interface{} `json:"result,omitempty"`
}
// Client is the object used to query the Energi Data Service API.
......@@ -118,7 +118,7 @@ type Client struct {
UserAgent string // User agent used when communicating with the EDS API.
common service // Reuse a single struct instead of allocating one for each service on the heap.
// Services used for talking to different parts of the EDS API
// Package *PackageService
Dataset *DatasetService
}
type service struct {
......@@ -134,7 +134,7 @@ func NewClient(httpClient *http.Client) *Client {
baseURL, _ := url.Parse(defaultBaseURL)
c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent}
c.common.client = c
// c.Package = (*PackageService)(&c.common)
c.Dataset = (*DatasetService)(&c.common)
return c
}
......
......@@ -39,6 +39,40 @@ func containsOption(tagOpts []string, option string) bool {
// parseOptions expects to be passed a struct and encode it into a URL
// query string.
func parseOptions(opts interface{}) (string, error) {
v := reflect.ValueOf(opts)
if v.Kind() != reflect.Struct {
return "", fmt.Errorf("Expected struct input. Got %v", v.Kind())
}
uv := url.Values{}
typ := v.Type()
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i).Name
value := fmt.Sprint(v.Field(i))
tagString := strings.Split(typ.Field(i).Tag.Get("url"), ",")
tagOpts := tagString[1:]
tag := tagString[0]
if containsOption(tagOpts, "omitempty") && isEmptyValue(v.Field(i)) {
continue
}
if tag == "-" {
continue
}
if tag == "" {
tag = field
}
uv.Add(tag, value)
}
return uv.Encode(), nil
}
// AppendOptions takes a options struct, encodes it into a query string and
// adds the querystring to a given URL.
//
// opts is encoded using the following rules:
//
......@@ -75,34 +109,16 @@ func containsOption(tagOpts []string, option string) bool {
// // Field appears as URL parameter "Field" (the default), but the field
// // is skipped if empty. Note the leading comma.
// Field int `url:",omitempty"`
func parseOptions(opts interface{}) (string, error) {
v := reflect.ValueOf(opts)
if v.Kind() != reflect.Struct {
return "", fmt.Errorf("Expected struct input. Got %v", v.Kind())
func AppendOptions(rawurl string, opts interface{}) (string, error) {
qs, err := parseOptions(opts)
if err != nil {
return "", err
}
uv := url.Values{}
typ := v.Type()
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i).Name
value := fmt.Sprint(v.Field(i))
tagString := strings.Split(typ.Field(i).Tag.Get("url"), ",")
tagOpts := tagString[1:]
tag := tagString[0]
if containsOption(tagOpts, "omitempty") && isEmptyValue(v.Field(i)) {
continue
}
if tag == "-" {
continue
}
if tag == "" {
tag = field
}
uv.Add(tag, value)
u, err := url.Parse(rawurl)
if err != nil {
return "", err
}
return uv.Encode(), nil
u.RawQuery = qs
return u.String(), nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment