/
public.go
55 lines (52 loc) · 1.14 KB
/
public.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Code generated by goagen v1.3.1, DO NOT EDIT.
//
// API "cellar": public Resource Client
//
// Command:
// $ goagen
// --design=github.com/Gys/goa-cellar/design
// --out=$(GOPATH)/src/github.com/Gys/goa-cellar
// --version=v1.3.1
package client
import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
)
// DownloadUI downloads index.html and writes it to the file dest.
// It returns the number of bytes downloaded in case of success.
func (c *Client) DownloadUI(ctx context.Context, dest string) (int64, error) {
scheme := c.Scheme
if scheme == "" {
scheme = "http"
}
u := url.URL{Host: c.Host, Scheme: scheme, Path: "/ui"}
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return 0, err
}
resp, err := c.Client.Do(ctx, req)
if err != nil {
return 0, err
}
if resp.StatusCode != 200 {
var body string
if b, err := ioutil.ReadAll(resp.Body); err != nil {
if len(b) > 0 {
body = ": " + string(b)
}
}
return 0, fmt.Errorf("%s%s", resp.Status, body)
}
defer resp.Body.Close()
out, err := os.Create(dest)
if err != nil {
return 0, err
}
defer out.Close()
return io.Copy(out, resp.Body)
}