Files
woodpecker/yaml/transform/rpc.go
2017-01-12 09:42:43 +04:00

44 lines
769 B
Go

package transform
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/drone/drone/yaml"
)
// RemoteTransform makes remote transform requests.
func RemoteTransform(c *yaml.Config, url string) error {
if url == "" {
return nil
}
var buf bytes.Buffer
// encode yaml in json format
if err := json.NewEncoder(&buf).Encode(c); err != nil {
return err
}
resp, err := http.Post(url, "application/json", &buf)
if err != nil {
return err
}
defer resp.Body.Close()
// decode the updated yaml from the body
if resp.StatusCode == 200 {
err = json.NewDecoder(resp.Body).Decode(c)
return err
}
// handle error response
out, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return fmt.Errorf(string(out))
}