client.go 3.45 KB
Newer Older
1 2 3
package http

import (
4
	"bytes"
5
	"encoding/json"
6
	"errors"
7
	"fmt"
8
	"io"
9
	"net/http"
10
	"net/url"
11 12 13 14 15
	"strings"

	cmds "github.com/jbenet/go-ipfs/commands"
)

16 17
var castError = errors.New("cast error")

18
const (
19
	ApiUrlFormat = "http://%s%s/%s?%s"
20 21
	ApiPath      = "/api/v0" // TODO: make configurable
)
22

23 24 25 26 27 28 29 30
// Client is the commands HTTP client interface.
type Client interface {
	Send(req cmds.Request) (cmds.Response, error)
}

type client struct {
	serverAddress string
}
31

32 33 34
func NewClient(address string) Client {
	return &client{address}
}
35

36
func (c *client) Send(req cmds.Request) (cmds.Response, error) {
37 38
	var userEncoding string
	if enc, found := req.Option(cmds.EncShort); found {
39 40 41 42 43
		var ok bool
		userEncoding, ok = enc.(string)
		if !ok {
			return nil, castError
		}
44 45
		req.SetOption(cmds.EncShort, cmds.JSON)
	} else {
46
		var ok bool
47
		enc, _ := req.Option(cmds.EncLong)
48 49 50 51
		userEncoding, ok = enc.(string)
		if !ok {
			return nil, castError
		}
52 53 54
		req.SetOption(cmds.EncLong, cmds.JSON)
	}

55
	query, inputStream, err := getQuery(req)
56 57 58 59 60 61 62
	if err != nil {
		return nil, err
	}

	path := strings.Join(req.Path(), "/")
	url := fmt.Sprintf(ApiUrlFormat, c.serverAddress, ApiPath, path, query)

63
	httpRes, err := http.Post(url, "application/octet-stream", inputStream)
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	if err != nil {
		return nil, err
	}

	res, err := getResponse(httpRes, req)
	if err != nil {
		return nil, err
	}

	if len(userEncoding) > 0 {
		req.SetOption(cmds.EncShort, userEncoding)
		req.SetOption(cmds.EncLong, userEncoding)
	}

	return res, nil
}

func getQuery(req cmds.Request) (string, io.Reader, error) {
82
	// TODO: handle multiple files with multipart
83
	var inputStream io.Reader
84

85
	query := url.Values{}
86
	for k, v := range req.Options() {
87 88 89 90 91
		str, ok := v.(string)
		if !ok {
			return "", nil, castError
		}
		query.Set(k, str)
92
	}
93 94

	args := req.Arguments()
95 96 97
	argDefs := req.Command().Arguments
	var argDef cmds.Argument

98
	for i, arg := range args {
99 100 101 102 103
		if i < len(argDefs) {
			argDef = argDefs[i]
		}

		if argDef.Type == cmds.ArgString {
104 105 106 107 108
			str, ok := arg.(string)
			if !ok {
				return "", nil, castError
			}
			query.Add("arg", str)
109 110 111

		} else {
			// TODO: multipart
112
			if inputStream != nil {
113
				return "", nil, fmt.Errorf("Currently, only one file stream is possible per request")
114
			}
115 116 117 118 119
			var ok bool
			inputStream, ok = arg.(io.Reader)
			if !ok {
				return "", nil, castError
			}
120
		}
121
	}
122

123
	return query.Encode(), inputStream, nil
124
}
125

126 127 128
// getResponse decodes a http.Response to create a cmds.Response
func getResponse(httpRes *http.Response, req cmds.Request) (cmds.Response, error) {
	var err error
129 130 131 132 133 134
	res := cmds.NewResponse(req)

	contentType := httpRes.Header["Content-Type"][0]
	contentType = strings.Split(contentType, ";")[0]

	if contentType == "application/octet-stream" {
135
		res.SetOutput(httpRes.Body)
136 137 138 139 140 141 142
		return res, nil
	}

	dec := json.NewDecoder(httpRes.Body)

	if httpRes.StatusCode >= http.StatusBadRequest {
		e := cmds.Error{}
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

		if httpRes.StatusCode == http.StatusNotFound {
			// handle 404s
			e.Message = "Command not found."
			e.Code = cmds.ErrClient

		} else if contentType == "text/plain" {
			// handle non-marshalled errors
			buf := bytes.NewBuffer(nil)
			io.Copy(buf, httpRes.Body)
			e.Message = string(buf.Bytes())
			e.Code = cmds.ErrNormal

		} else {
			// handle marshalled errors
			err = dec.Decode(&e)
			if err != nil {
				return nil, err
			}
162 163 164 165 166
		}

		res.SetError(e, e.Code)

	} else {
167
		v := req.Command().Type
168 169 170 171 172
		err = dec.Decode(&v)
		if err != nil {
			return nil, err
		}

173
		res.SetOutput(v)
174 175
	}

176 177
	return res, nil
}