-
Notifications
You must be signed in to change notification settings - Fork 6
/
api.go
74 lines (61 loc) · 2.25 KB
/
api.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
package api
import (
"context"
"time"
"github.com/spaceuptech/space-api-go/eventing"
"github.com/spaceuptech/space-api-go/realtime"
"github.com/spaceuptech/space-api-go/transport"
"github.com/spaceuptech/space-api-go/transport/websocket"
"github.com/spaceuptech/space-api-go/config"
"github.com/spaceuptech/space-api-go/db"
"github.com/spaceuptech/space-api-go/filestore"
"github.com/spaceuptech/space-api-go/types"
)
// API is the main API object to communicate with space cloud
type API struct {
config *config.Config
socket *websocket.Socket
realtime *realtime.Realtime
}
// New initialised a new instance of the API object
func New(project, url string, sslEnabled bool) *API {
c := &config.Config{Project: project, Transport: transport.New(url, sslEnabled)}
w := websocket.Init(url, c)
r := realtime.Init(project, w)
return &API{config: c, socket: w, realtime: r}
}
// Ping check if connection is established with space cloud
func (api *API) Ping() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return api.config.Transport.Ping(ctx, api.config.Token)
}
// SetToken sets the JWT token to be used in each request
func (api *API) SetToken(token string) {
api.config.Token = token
}
// SetProjectID sets the project id to be used by the API
func (api *API) SetProjectID(project string) {
api.config.Project = project
}
// DB creates a db client instance
func (api *API) DB(dbAlias string) *db.DB {
return db.New(dbAlias, api.config, api.realtime)
}
// Call invokes the specified function on the backend
func (api *API) Call(service, endpoint string, params interface{}, timeout int) (*types.Response, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()
return api.config.Transport.Call(ctx, api.config.Token, api.config.Project, service, endpoint, params, timeout)
}
// FileStore creates a FileStore instance
func (api *API) Filestore() *filestore.Filestore {
return filestore.New(api.config)
}
// QueueEvent creates a Eventing instance
func (api *API) QueueEvent(eventType string, payload map[string]interface{}) *eventing.Eventing {
if payload == nil {
payload = map[string]interface{}{}
}
return eventing.New(eventType, payload, api.config)
}