-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpipe.go
More file actions
90 lines (75 loc) · 1.71 KB
/
Copy pathpipe.go
File metadata and controls
90 lines (75 loc) · 1.71 KB
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
package tinybird
import (
"fmt"
"net/url"
"strings"
"github.com/the-hotels-network/go-tinybird/internal/env"
)
const (
Format string = "json"
JSON = "json"
NDJSON = "ndjson"
CSV = "csv"
)
// Pipe is a object on tinybird, contains one or more SQL queries (Nodes) that
// result in either an API endpoint or a Materialized View.
type Pipe struct {
// Name of pipe. Define by user.
Name string
// Alias of pipe. Optional.
Alias string
// Is for internal purpose and is generated by code.
URL string
// Parameters use on pipe. Define by user.
Parameters url.Values
// Is an area that contains a set of Tinybird resources, including Pipes,
// Nodes, APIs, Data Sources & Auth Tokens. Define by user.
Workspace Workspace
// Return format.
Format string
}
func (p *Pipe) GetParameters() string {
for key, value := range p.Parameters {
if len(value) > 1 {
p.Parameters.Del(key)
p.Parameters.Add(key, strings.Join(value, ","))
}
}
return strings.Replace(p.Parameters.Encode(), "+", "%20", -1)
}
// Build and return the pipe URL.
func (p Pipe) GetURL() string {
baseUrl := URL_BASE
if p.URL != "" {
baseUrl = p.URL
}
return baseUrl
}
func (p Pipe) GetURI() string {
qs, _ := url.QueryUnescape(p.GetParameters())
if qs == "" {
return fmt.Sprintf(
"%s/v0/pipes/%s.%s",
p.GetURL(),
p.Name,
p.GetFormat(),
)
}
return fmt.Sprintf(
"%s/v0/pipes/%s.%s?%s",
p.GetURL(),
p.Name,
p.GetFormat(),
qs,
)
}
// Verify the variable Format value to return json, ndjson or csv.
func (p Pipe) GetFormat() string {
if p.Format == JSON || p.Format == NDJSON || p.Format == CSV {
return p.Format
}
if env.GetBool("TB_NDJSON", false) {
return NDJSON
}
return JSON
}