forked from openpitrix/openpitrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
89 lines (70 loc) · 2 KB
/
http.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2018 The OpenPitrix Authors. All rights reserved.
// Use of this source code is governed by a Apache license
// that can be found in the LICENSE file.
package repoiface
import (
"context"
"fmt"
"io/ioutil"
neturl "net/url"
"openpitrix.io/openpitrix/pkg/util/httputil"
"openpitrix.io/openpitrix/pkg/util/yamlutil"
)
type HttpInterface struct {
url *neturl.URL
}
func NewHttpInterface(ctx context.Context, url *neturl.URL) (*HttpInterface, error) {
return &HttpInterface{
url: url,
}, nil
}
func (i *HttpInterface) CheckFile(ctx context.Context, filename string) (bool, error) {
u := URLJoin(i.url.String(), GetFileName(filename))
resp, err := httputil.HttpGet(u)
if err != nil {
return false, err
}
defer resp.Body.Close()
if 200 <= resp.StatusCode || resp.StatusCode <= 299 {
return false, fmt.Errorf("http status code is %d", resp.StatusCode)
}
return true, nil
}
func (i *HttpInterface) ReadFile(ctx context.Context, filename string) ([]byte, error) {
u := URLJoin(i.url.String(), GetFileName(filename))
resp, err := httputil.HttpGet(u)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("http status code is not 200")
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
func (i *HttpInterface) DeleteFile(ctx context.Context, filename string) error {
return ErrWriteIsUnsupported
}
func (i *HttpInterface) WriteFile(ctx context.Context, filename string, data []byte) error {
return ErrWriteIsUnsupported
}
type indexYaml struct {
ApiVersion string `yaml:"apiVersion"`
Entries map[string]interface{} `yaml:"entries"`
Generated string `yaml:"generated"`
}
func (i *HttpInterface) CheckRead(ctx context.Context) error {
data, err := i.ReadFile(ctx, "index.yaml")
if err != nil {
return err
}
var y indexYaml
err = yamlutil.Decode(data, &y)
return err
}
func (i *HttpInterface) CheckWrite(ctx context.Context) error {
return ErrWriteIsUnsupported
}