-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
134 lines (116 loc) · 4.3 KB
/
main.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package meocloudcli
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"github.com/joho/godotenv"
"encoding/json"
"net/http"
"net/url"
"github.com/mrjones/oauth"
)
// Language: go
// Path: main.go
// Returns an http.Client ready to be used to interact with the MeoCloud API
// Requires an .env file with the following variables:
// CONSUMER_KEY CONSUMER_SECRET OAUTH_TOKEN OAUTH_SECRET
// This file should be placed in the main dir
// Note: at this time, the process of obtaining an access token is not yet implemented
func get_meo_client() *http.Client {
godotenv.Load()
accessToken := &oauth.AccessToken{
Token: os.Getenv("OAUTH_TOKEN"),
Secret: os.Getenv("OAUTH_TOKEN_SECRET"),
}
consumer := oauth.NewConsumer(
os.Getenv("CONSUMER_KEY"),
os.Getenv("CONSUMER_SECRET"),
oauth.ServiceProvider{
RequestTokenUrl: "https://meocloud.pt/oauth/request_token",
AuthorizeTokenUrl: "https://meocloud.pt/oauth/authorize",
AccessTokenUrl: "https://meocloud.pt/oauth/access_token"},
)
meo, _ := consumer.MakeHttpClient(accessToken)
return meo
}
// Given an url, returns a map of the json response
// To be used by API calls that return JSON
func fetch_json(url string) (map[string]interface{}, int) {
bodybytes, status := fetch(url)
var bodymap map[string]interface{}
json.Unmarshal(bodybytes, &bodymap)
return bodymap, status
}
// Generic function to fetch a url and return the body as a byte array
func fetch(url string) ([]byte, int) {
meo := get_meo_client()
httpResponse, _ := meo.Get(url)
bodybytes, _ := io.ReadAll(httpResponse.Body)
return bodybytes, httpResponse.StatusCode
}
// Get the account information of the current user (https://meocloud.pt/documentation#accountinfo)
func Account_info() (map[string]interface{}, int) {
return fetch_json("https://api.meocloud.pt/1/Account/Info")
}
// for debugging purposes, outputs the content of a map obtained from a JSON response
func dump_json(bodymap map[string]interface{}) {
for key, value := range bodymap {
fmt.Println(key, "->", value)
}
}
// get the metadata of a file or folder, used of directory listings (https://meocloud.pt/documentation#metadata)
func Get_metadata(path string) (map[string]interface{}, int) {
bodymap, status := fetch_json(fmt.Sprintf("https://api.meocloud.pt/1/Metadata/meocloud%s", path))
/* for key,value := range bodymap["contents"].([]interface{}) {
fmt.Println(key,"->",value.(map[string]interface{})["name"])
}
*/
return bodymap, status
}
// Get the contents of a file given its path in the cloud (https://meocloud.pt/documentation#files)
func Get_file(filepath string) ([]byte, int) {
meo := get_meo_client()
httpResponse, _ := meo.Get(fmt.Sprintf("https://api-content.meocloud.pt/1/Files/meocloud/%s", filepath))
bodybytes, _ := io.ReadAll(httpResponse.Body)
return bodybytes, httpResponse.StatusCode
}
// Send byte array to the cloud (https://meocloud.pt/documentation#files) giving it a name specified in newfilepath
// returns the status code
func Send(newfilepath string, data []byte) int {
meo := get_meo_client()
putReq, _ := http.NewRequest(
"PUT",
fmt.Sprintf("https://api-content.meocloud.pt/1/Files/meocloud/%s", newfilepath),
bytes.NewReader(data))
httpResponse, _ := meo.Do(putReq)
return httpResponse.StatusCode
}
// send a file to the cloud (https://meocloud.pt/documentation#files)
func Send_file(filepath string) int {
contentBytes, _ := os.ReadFile(filepath)
return Send(filepath, contentBytes)
}
// delete a file in the cloud (https://meocloud.pt/documentation#delete)
// returns the status code (200 on success, 404 if the file does not exist and 406 if many files are deleted)
func Delete_file(filepath string) int {
meo := get_meo_client()
httpResponse, _ := meo.PostForm("https://api.meocloud.pt/1/Fileops/Delete",
url.Values{
"root": {"meocloud"},
"path": {strings.Join([]string{"/", filepath}, "")},
})
return httpResponse.StatusCode
}
// creates a directory in the cloud (https://meocloud.pt/documentation#createfolder)
// returns the status code (200 if successful, 403 if the directory already exists)
func Create_dir(folderpath string) int {
meo := get_meo_client()
httpResponse, _ := meo.PostForm("https://api.meocloud.pt/1/Fileops/CreateFolder",
url.Values{
"root": {"meocloud"},
"path": {strings.Join([]string{"/", folderpath}, "")},
})
return httpResponse.StatusCode
}