Skip to content

Commit

Permalink
Merge pull request #11 from senseyeio/context-support-and-cleanup
Browse files Browse the repository at this point in the history
Context support and cleanup
  • Loading branch information
smotes authored Jul 27, 2019
2 parents 95bddbf + 0874c35 commit 0e7cd30
Show file tree
Hide file tree
Showing 10 changed files with 331 additions and 158 deletions.
39 changes: 21 additions & 18 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package mbgo

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -65,14 +66,14 @@ func (cli *Client) decodeError(body io.ReadCloser) error {
//
// See more information on this resource at:
// http://www.mbtest.org/docs/api/overview#post-imposters.
func (cli *Client) Create(imp Imposter) (*Imposter, error) {
func (cli *Client) Create(ctx context.Context, imp Imposter) (*Imposter, error) {
p := "/imposters"
b, err := json.Marshal(&imp)
if err != nil {
return nil, err
}

req, err := cli.restCli.NewRequest(http.MethodPost, p, bytes.NewReader(b), nil)
req, err := cli.restCli.NewRequest(ctx, http.MethodPost, p, bytes.NewReader(b), nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -101,12 +102,12 @@ func (cli *Client) Create(imp Imposter) (*Imposter, error) {
//
// See more information about this resource at:
// http://www.mbtest.org/docs/api/overview#get-imposter.
func (cli *Client) Imposter(port int, replay bool) (*Imposter, error) {
func (cli *Client) Imposter(ctx context.Context, port int, replay bool) (*Imposter, error) {
p := fmt.Sprintf("/imposters/%d", port)
vs := url.Values{}
vs.Add("replayable", strconv.FormatBool(replay))

req, err := cli.restCli.NewRequest(http.MethodGet, p, nil, vs)
req, err := cli.restCli.NewRequest(ctx, http.MethodGet, p, nil, vs)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -134,12 +135,12 @@ func (cli *Client) Imposter(port int, replay bool) (*Imposter, error) {
//
// See more information about this resource at:
// http://www.mbtest.org/docs/api/overview#delete-imposter.
func (cli *Client) Delete(port int, replay bool) (*Imposter, error) {
func (cli *Client) Delete(ctx context.Context, port int, replay bool) (*Imposter, error) {
p := fmt.Sprintf("/imposters/%d", port)
vs := url.Values{}
vs.Add("replayable", strconv.FormatBool(replay))

req, err := cli.restCli.NewRequest(http.MethodDelete, p, nil, vs)
req, err := cli.restCli.NewRequest(ctx, http.MethodDelete, p, nil, vs)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -167,10 +168,10 @@ func (cli *Client) Delete(port int, replay bool) (*Imposter, error) {
//
// See more information about this resource at:
// http://www.mbtest.org/docs/api/overview#delete-imposter-requests.
func (cli *Client) DeleteRequests(port int) (*Imposter, error) {
func (cli *Client) DeleteRequests(ctx context.Context, port int) (*Imposter, error) {
p := fmt.Sprintf("/imposters/%d/requests", port)

req, err := cli.restCli.NewRequest(http.MethodDelete, p, nil, nil)
req, err := cli.restCli.NewRequest(ctx, http.MethodDelete, p, nil, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -202,7 +203,7 @@ type imposterListWrapper struct {
//
// See more information about this resource at:
// http://www.mbtest.org/docs/api/overview#put-imposters.
func (cli *Client) Overwrite(imps []Imposter) ([]Imposter, error) {
func (cli *Client) Overwrite(ctx context.Context, imps []Imposter) ([]Imposter, error) {
p := "/imposters"

b, err := json.Marshal(&struct {
Expand All @@ -214,7 +215,7 @@ func (cli *Client) Overwrite(imps []Imposter) ([]Imposter, error) {
return nil, err
}

req, err := cli.restCli.NewRequest(http.MethodPut, p, bytes.NewReader(b), nil)
req, err := cli.restCli.NewRequest(ctx, http.MethodPut, p, bytes.NewReader(b), nil)
if err != nil {
return nil, err
}
Expand All @@ -239,12 +240,12 @@ func (cli *Client) Overwrite(imps []Imposter) ([]Imposter, error) {
//
// See more information about this resource at:
// http://www.mbtest.org/docs/api/overview#get-imposters.
func (cli *Client) Imposters(replay bool) ([]Imposter, error) {
func (cli *Client) Imposters(ctx context.Context, replay bool) ([]Imposter, error) {
p := "/imposters"
vs := url.Values{}
vs.Add("replayable", strconv.FormatBool(replay))

req, err := cli.restCli.NewRequest(http.MethodGet, p, nil, vs)
req, err := cli.restCli.NewRequest(ctx, http.MethodGet, p, nil, vs)
if err != nil {
return nil, err
}
Expand All @@ -271,12 +272,12 @@ func (cli *Client) Imposters(replay bool) ([]Imposter, error) {
//
// See more information about this resource at:
// http://www.mbtest.org/docs/api/overview#delete-imposters.
func (cli *Client) DeleteAll(replay bool) ([]Imposter, error) {
func (cli *Client) DeleteAll(ctx context.Context, replay bool) ([]Imposter, error) {
p := "/imposters"
vs := url.Values{}
vs.Add("replayable", strconv.FormatBool(replay))

req, err := cli.restCli.NewRequest(http.MethodDelete, p, nil, vs)
req, err := cli.restCli.NewRequest(ctx, http.MethodDelete, p, nil, vs)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -305,6 +306,7 @@ func (cli *Client) DeleteAll(replay bool) ([]Imposter, error) {
type Config struct {
// Version represents the mountebank version in semantic M.m.p format.
Version string `json:"version"`

// Options represent runtime options of the mountebank server process.
Options struct {
Help bool `json:"help"`
Expand All @@ -320,6 +322,7 @@ type Config struct {
LogLevel string `json:"loglevel"`
IPWhitelist []string `json:"ipWhitelist"`
} `json:"options"`

// Process represents information about the mountebank server NodeJS runtime.
Process struct {
NodeVersion string `json:"nodeVersion"`
Expand All @@ -338,10 +341,10 @@ type Config struct {
//
// See more information on this resource at:
// http://www.mbtest.org/docs/api/overview#get-config.
func (cli *Client) Config() (*Config, error) {
func (cli *Client) Config(ctx context.Context) (*Config, error) {
p := "/config"

req, err := cli.restCli.NewRequest(http.MethodGet, p, nil, nil)
req, err := cli.restCli.NewRequest(ctx, http.MethodGet, p, nil, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -379,7 +382,7 @@ type Log struct {
//
// See more information on this resource at:
// http://www.mbtest.org/docs/api/overview#get-logs.
func (cli *Client) Logs(start, end int) ([]Log, error) {
func (cli *Client) Logs(ctx context.Context, start, end int) ([]Log, error) {
p := "/logs"
vs := url.Values{}
if start >= 0 {
Expand All @@ -389,7 +392,7 @@ func (cli *Client) Logs(start, end int) ([]Log, error) {
vs.Add("endIndex", strconv.Itoa(end))
}

req, err := cli.restCli.NewRequest(http.MethodGet, p, nil, vs)
req, err := cli.restCli.NewRequest(ctx, http.MethodGet, p, nil, vs)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 0e7cd30

Please sign in to comment.