-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathapi_result.go
95 lines (87 loc) · 2.42 KB
/
api_result.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
//
// Treasure Data API client for Go
//
// Copyright (C) 2014 Treasure Data, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package td_client
import (
"fmt"
"net/url"
)
type ListResultsResultElement struct {
Id int
UserId int
Name string
Url string
}
type ListResultsResult []ListResultsResultElement
var listResultsSchema = map[string]interface{}{
"results": []map[string]interface{}{
{
"name": "",
"organization": Optional{"", nil},
"url": "",
"id": Optional{0, 0},
"user_id": Optional{0, 0},
},
},
}
func (client *TDClient) ListResults() (*ListResultsResult, error) {
resp, err := client.get("/v3/result/list", nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, client.buildError(resp, -1, "List result tables failed", nil)
}
js, err := client.checkedJson(resp, listResultsSchema)
if err != nil {
return nil, err
}
results := js["results"].([]map[string]interface{})
retval := make(ListResultsResult, len(results))
for i, v := range results {
retval[i] = ListResultsResultElement{
Id: v["id"].(int),
UserId: v["user_id"].(int),
Name: v["name"].(string),
Url: v["url"].(string),
}
}
return &retval, nil
}
func (client *TDClient) CreateResult(name, url_ string) error {
resp, err := client.post(fmt.Sprintf("/v3/result/create/%s", url.QueryEscape(name)), url.Values{"url": {url_}})
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return client.buildError(resp, -1, "Create result table failed", nil)
}
return nil
}
func (client *TDClient) DeleteResult(name string) error {
resp, err := client.post(fmt.Sprintf("/v3/result/delete/%s", url.QueryEscape(name)), nil)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return client.buildError(resp, -1, "Delete result table failed", nil)
}
return nil
}