Commit ad2903ad authored by endk-mirror-svc's avatar endk-mirror-svc

Getting close to figuring out how I want this to look like

parent 1cbdcfaf
Pipeline #25067305 failed with stages
in 1 minute and 5 seconds
......@@ -2,7 +2,6 @@ package eds
import (
"context"
"fmt"
"net/url"
"time"
)
......@@ -60,7 +59,7 @@ 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) {
func (s *DatasetService) List(ctx context.Context, opt *DatasetListOptions) (*APIResponse, error) {
url, err := AppendOptions("package_list", opt)
if err != nil {
return nil, err
......@@ -69,15 +68,12 @@ func (s *DatasetService) List(ctx context.Context, opt DatasetListOptions) ([]*D
if err != nil {
return nil, err
}
var data APIResponse
_, err = s.client.Do(ctx, req, &data)
datasets := &APIResponse{}
_, err = s.client.Do(ctx, req, datasets)
if err != nil {
return nil, err
}
fmt.Printf("%+v\n", req)
fmt.Printf("%+v\n", data.Result)
//Placeholder
return nil, nil
return datasets, nil
}
......@@ -12,7 +12,7 @@ func TestPackageShow(t *testing.T) {
client := eds.NewClient(nil)
opts := eds.DatasetListOptions{Limit: 2}
ctx := context.Background()
d, err := client.Dataset.List(ctx, opts)
d, err := client.Dataset.List(ctx, &opts)
t.Log(err)
t.Log(d)
t.Logf("%+v\n", d)
}
......@@ -177,13 +177,11 @@ func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Requ
// Do sends and API request and returns the API response. The API response is
// JSON decoded and stored in the value pointed to by v, or returned as an
// error if an API error has occurred. If v implements the io.Writer
// interface, the raw response body will be written to v, without attempting to
// first decode it.
// error if an API error has occurred.
//
// The provided ctx must be non-nil. If it is canceled or times out,
// ctx.Err() will be returned.
func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*http.Response, error) {
func (c *Client) Do(ctx context.Context, req *http.Request, v *APIResponse) (*http.Response, error) {
req = req.WithContext(ctx)
resp, err := c.client.Do(req)
......@@ -199,16 +197,12 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*htt
}
defer resp.Body.Close()
if v != nil {
if w, ok := v.(io.Writer); ok {
io.Copy(w, resp.Body)
} else {
decErr := json.NewDecoder(resp.Body).Decode(v)
if decErr == io.EOF {
decErr = nil // ignore EOF errors caused by empty response body
}
if decErr != nil {
err = decErr
}
decErr := json.NewDecoder(resp.Body).Decode(v)
if decErr == io.EOF {
decErr = nil // ignore EOF errors caused by empty response body
}
if decErr != nil {
err = decErr
}
}
return resp, err
......
package eds
import (
"fmt"
"net/http"
)
type ErrorResponse struct {
Response *http.Response
Message string
}
func (r *ErrorResponse) Error() string {
return fmt.Sprintf("%v %v: %d %v",
r.Response.Request.Method, r.Response.Request.URL,
r.Response.StatusCode, r.Message)
}
......@@ -40,12 +40,18 @@ 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) {
uv := url.Values{}
v := reflect.ValueOf(opts)
for v.Kind() == reflect.Ptr {
if v.IsNil() {
return uv.Encode(), nil
}
v = v.Elem()
}
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
......
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