Пример #1
0
func NewClient(uri string, opts *Options) (client *Client, err error) {

	url, err := url.Parse(uri)
	if err != nil {
		return
	}
	url.Path = path.Join("/socket.io", url.Path)
	url.Path = url.EscapedPath()
	if strings.HasSuffix(url.Path, "socket.io") {
		url.Path += "/"
	}
	q := url.Query()
	for k, v := range opts.Query {
		q.Set(k, v)
	}
	url.RawQuery = q.Encode()

	socket, err := engineio.NewClientConn(opts.Transport, url)
	if err != nil {
		return
	}

	client = &Client{
		opts: opts,
		conn: socket,

		events: make(map[string]*caller),
		acks:   make(map[int]*caller),
	}

	go client.readLoop()

	return
}
Пример #2
0
func (i *Image) FileExtension() (string, error) {
	url, err := url.Parse(i.url)
	if err != nil {
		return "", err
	}

	path := url.EscapedPath()
	parts := strings.Split(path, ".")
	return parts[len(parts)-1], nil
}
Пример #3
0
// send executes a request at the path passed as argument.
// It returns a channel where the extracted content of each batch is returned.
func (db *DB) send(description, method, path string, body []byte) (chan interface{}, error) {
	in := make(chan interface{}, 16)
	out := make(chan interface{}, 16)

	url, err := url.Parse(path)
	if err != nil {
		return nil, err
	}

	db.l.LogBegin(description, method, db.url+db.dbPath()+path, body)
	start := time.Now()
	path = url.EscapedPath()

	req := db.conn.Request().
		Method(method).
		AddPath(path).
		SetQueryParams(db.queryParams(url))

	if body != nil {
		req.Body(bytes.NewBuffer(body))
	}

	res, err := req.Send()
	if err != nil {
		db.l.LogError(err.Error(), start)
		return nil, err
	}

	if !res.Ok && len(res.Bytes()) == 0 {
		err := errors.New("the database returned a " + strconv.Itoa(res.StatusCode))

		switch res.StatusCode {
		case http.StatusUnauthorized:
			err = errors.New("unauthorized: invalid credentials")
		case http.StatusTemporaryRedirect:
			err = errors.New("the database returned a 307 to " + res.Header.Get("Location"))
		}

		db.l.LogError(err.Error(), start)

		return nil, err
	}

	result := &result{}
	json.Unmarshal(res.Bytes(), result)

	if result.Error {
		db.l.LogError(result.ErrorMessage, start)
		switch {
		case strings.Contains(result.ErrorMessage, "unique constraint violated"):
			return nil, &ErrUnique{result.ErrorMessage}
		case strings.Contains(result.ErrorMessage, "not found"):
			return nil, &ErrNotFound{result.ErrorMessage}
		case strings.Contains(result.ErrorMessage, "unknown collection"):
			return nil, &ErrNotFound{result.ErrorMessage}
		case strings.Contains(result.ErrorMessage, "duplicate name"):
			return nil, &ErrDuplicate{result.ErrorMessage}
		default:
			return nil, errors.New(result.ErrorMessage)
		}
	}

	go db.l.LogResult(result.Cached, start, in, out)

	if len(result.Content) != 0 {
		in <- result.Content
	} else {
		in <- json.RawMessage(res.Bytes())
	}

	if result.HasMore {
		go db.followCursor(path+"/"+result.ID, in)
	} else {
		in <- nil
	}

	return out, nil
}