generated from RTradeLtd/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
76 lines (67 loc) · 1.85 KB
/
types.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
package swampi
import "fmt"
// APICall represents a type string indicating various APIs and their paths
type APICall string
// String implements the stringer interface
// but also returns the http path for this api
func (a APICall) String() string {
return string(a)
}
// Method returns the http method associated with the call
func (a APICall) Method() string {
switch a {
// this case also matches TarStreamUpload
case SingleFileUpload:
return "POST"
default:
return ""
}
}
// ContentType returns the content type associated with this request
func (a APICall) ContentType(isTar bool) string {
switch a {
case SingleFileUpload:
// can't have multiple cases of the same string content
// so we need to short circuit specific uploads with manual checks
if isTar {
return "application/x-tar"
}
return "text/plain"
default:
return ""
}
}
// ParseArgs is used to format request arguments
func (a APICall) ParseArgs(args ...interface{}) string {
switch a {
case SingleFileUpload:
return a.String()
case SingleFileDownload, ListFiles:
return fmt.Sprintf(a.String(), args...)
default:
return ""
}
}
// Response returns the object associated with the api calls response
func (a APICall) Response() interface{} {
switch a {
case ListFiles:
return &BZZList{}
default:
return nil
}
}
const (
// SingleFileUpload is an api to upload a singular file
SingleFileUpload = APICall("/bzz:/")
// SingleFileDownload is used to download a singular file from swarm
SingleFileDownload = APICall("/bzz:/%s/")
// ListFiles is used to list files in a particular manifest
ListFiles = APICall("/bzz-list:/%s/")
// TarStreamUpload is used to upload tar files
TarStreamUpload = APICall("/bzz:/")
)
var (
// APICalls is just a helper slice containing all known API calls
APICalls = []APICall{SingleFileUpload, SingleFileDownload, TarStreamUpload, ListFiles}
)