package app

import (
	"strings"

	"github.com/materials-commons/config"
	"github.com/materials-commons/gohandy/ezhttp"
)

type mcapi struct{}

var MCApi mcapi

func (a mcapi) MCUrl() string {
	return config.GetString("mcurl")
}

func (a mcapi) MCClient() *ezhttp.EzClient {
	mcurl := a.MCUrl()
	if strings.HasPrefix(mcurl, "https") {
		return ezhttp.NewSSLClient()
	}
	return ezhttp.NewClient()
}

package app

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/url"
	"strings"

	"github.com/gtarcea/1DevDayTalk2014/app"
	"github.com/materials-commons/config"
	"github.com/materials-commons/gohandy/ezhttp"
	"gnd.la/net/urlutil"
)

type mcapi struct{}

var MCApi mcapi

func (a mcapi) MCUrl() string {
	return config.GetString("mcurl")
}

func (a mcapi) MCClient() *ezhttp.EzClient {
	mcurl := a.MCUrl()
	if strings.HasPrefix(mcurl, "https") {
		return ezhttp.NewSSLClient()
	}
	return ezhttp.NewClient()
}

func (a mcapi) APIUrl(path string) string {
	values := url.Values{}
	values.Add("apikey", config.GetString("apikey"))
	mcurl := urlutil.MustJoin(a.MCUrl(), path)
	mcurl = urlutil.AppendQuery(mcurl, values)
	return mcurl
}

func (a mcapi) APIError(resp *http.Response, errs []error) error {
	switch {
	case len(errs) != 0:
		return app.ErrInvalid
	case resp.StatusCode > 299:
		return fmt.Errorf("HTTP Error: %s", resp.Status)
	default:
		return nil
	}
}

func (a mcapi) ToJSON(from string, to interface{}) error {
	err := json.Unmarshal([]byte(from), to)
	return err
}

Add utility functions for dealing with the api.
Add utility functions for dealing with the api.
