github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/common/stream/stream.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package stream
     5  
     6  import (
     7  	"github.com/google/go-querystring/query"
     8  	"github.com/juju/errors"
     9  
    10  	"github.com/juju/juju/api/base"
    11  )
    12  
    13  // Open opens a streaming connection to the endpoint path that conforms
    14  // to the provided config.
    15  func Open(conn base.StreamConnector, path string, cfg interface{}) (base.Stream, error) {
    16  	attrs, err := query.Values(cfg)
    17  	if err != nil {
    18  		return nil, errors.Annotate(err, "failed to generate URL query from config")
    19  	}
    20  	stream, err := conn.ConnectStream(path, attrs)
    21  	if err != nil {
    22  		return nil, errors.Annotatef(err, "cannot connect to %s", path)
    23  	}
    24  	return stream, nil
    25  }