func (m *MinepkgAPI) decorate(req *http.Request) {
	req.Header.Set("User-Agent", "minepkg (https://github.com/minepkg/minepkg)")
	if req.Header.Get("Content-Type") == "" {
		req.Header.Set("Content-Type", "application/json")
	}
	switch {
	case m.JWT != "":
		req.Header.Set("Authorization", "Bearer "+m.JWT)
	case m.APIKey != "":
		req.Header.Set("Authorization", "api-key "+m.APIKey)
	}
}
decorate decorates a request with the User-Agent header and a auth header if set
package api
import (
	"bytes"
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
)
var (
	ErrNotFound = errors.New("resource not found")
	ErrBadRequest = errors.New("bad Request")
	DefaultURL = "https://api.preview.minepkg.io/v1"
)
type MinepkgAPI struct {
	HTTP *http.Client
	APIUrl string
	APIKey string
	JWT    string
	User   *User
}
func New() *MinepkgAPI {
	return &MinepkgAPI{
		HTTP:   http.DefaultClient,
		APIUrl: DefaultURL,
	}
}
func NewWithClient(client *http.Client) *MinepkgAPI {
	return &MinepkgAPI{
		HTTP:   client,
		APIUrl: DefaultURL,
	}
}
func (m *MinepkgAPI) HasCredentials() bool {
	return m.JWT != "" || m.APIKey != ""
}
func (m *MinepkgAPI) GetAccount(ctx context.Context) (*User, error) {
	res, err := m.get(ctx, m.APIUrl+"/account")
	if err != nil {
		return nil, err
	}
	if err := checkResponse(res); err != nil {
		return nil, err
	}
	user := User{}
	if err := parseJSON(res, &user); err != nil {
		return nil, err
	}
	return &user, nil
}
func (m *MinepkgAPI) PutRelease(project string, version string, reader io.Reader) (*Release, error) {
	req, err := http.NewRequest("PUT", m.APIUrl+"/projects/"+project+"@"+version, reader)
	if err != nil {
		return nil, err
	}
	m.decorate(req)
	req.Header.Set("Content-Type", "application/java-archive")
	res, err := m.HTTP.Do(req)
	if err != nil {
		return nil, err
	}
	if err := checkResponse(res); err != nil {
		return nil, err
	}
	release := Release{}
	if err := parseJSON(res, &release); err != nil {
		return nil, err
	}
	return &release, nil
}
func (m *MinepkgAPI) PostCrashReport(ctx context.Context, report *CrashReport) error {
	res, err := m.postJSON(context.TODO(), m.APIUrl+"/crash-reports", report)
	if err != nil {
		return err
	}
	if err := checkResponse(res); err != nil {
		return err
	}
	return err
}
func (m *MinepkgAPI) PostProjectMedia(ctx context.Context, project string, content io.Reader) error {
	req, err := http.NewRequest("POST", m.APIUrl+"/projects/"+project+"/media", content)
	req.Header.Add("Content-Type", "image/png")
	req = req.WithContext(ctx)
	if err != nil {
		return err
	}
	m.decorate(req)
	res, err := m.HTTP.Do(req)
	if err != nil {
		fmt.Println(err)
		return err
	}
	b, _ := ioutil.ReadAll(res.Body)
	fmt.Println(string(b))
	if err := checkResponse(res); err != nil {
		return err
	}
	return err
}
func (m *MinepkgAPI) get(ctx context.Context, url string) (*http.Response, error) {
	req, err := http.NewRequest("GET", url, nil)
	req = req.WithContext(ctx)
	if err != nil {
		return nil, err
	}
	m.decorate(req)
	return m.HTTP.Do(req)
}
func (m *MinepkgAPI) delete(ctx context.Context, url string) (*http.Response, error) {
	req, err := http.NewRequest("DELETE", url, nil)
	req = req.WithContext(ctx)
	if err != nil {
		return nil, err
	}
	m.decorate(req)
	return m.HTTP.Do(req)
}
func (m *MinepkgAPI) postJSON(ctx context.Context, url string, data interface{}) (*http.Response, error) {
	jsonData, err := json.Marshal(data)
	if err != nil {
		return nil, err
	}
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
	req = req.WithContext(ctx)
	if err != nil {
		return nil, err
	}
	m.decorate(req)
	return m.HTTP.Do(req)
}
func checkResponse(res *http.Response) error {
	switch {
	case res.StatusCode == http.StatusNotFound:
		return ErrNotFound
	case res.StatusCode >= 200 && res.StatusCode < 400:
		return nil
	case res.StatusCode >= 400:
		minepkgErr := &MinepkgError{}
		if err := parseJSON(res, minepkgErr); err != nil {
			return errors.New("minepkg API did respond with unexpected error format. code: " + res.Status)
		}
		return minepkgErr
	default:
		return errors.New("minepkg API did respond with unexpected status code " + res.Status)
	}
}