github.com/holochain/holochain-proto@v0.1.0-alpha-26.0.20200915073418-5c83169c9b5b/action_bridge.go (about)

     1  package holochain
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net/http"
     8  )
     9  
    10  //------------------------------------------------------------
    11  // Bridge
    12  
    13  type APIFnBridge struct {
    14  	token    string
    15  	url      string
    16  	zome     string
    17  	function string
    18  	args     interface{}
    19  }
    20  
    21  func (fn *APIFnBridge) Name() string {
    22  	return "bridge"
    23  }
    24  
    25  func (fn *APIFnBridge) Args() []Arg {
    26  	return []Arg{{Name: "app", Type: HashArg}, {Name: "zome", Type: StringArg}, {Name: "function", Type: StringArg}, {Name: "args", Type: ArgsArg}}
    27  }
    28  
    29  func (fn *APIFnBridge) Call(h *Holochain) (response interface{}, err error) {
    30  	body := bytes.NewBuffer([]byte(fn.args.(string)))
    31  	var resp *http.Response
    32  	resp, err = http.Post(fmt.Sprintf("%s/bridge/%s/%s/%s", fn.url, fn.token, fn.zome, fn.function), "", body)
    33  	if err != nil {
    34  		return
    35  	}
    36  	defer resp.Body.Close()
    37  	var b []byte
    38  	b, err = ioutil.ReadAll(resp.Body)
    39  	response = string(b)
    40  	return
    41  }