-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessions.go
179 lines (149 loc) · 4.27 KB
/
sessions.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) Bluebeam Inc. All rights reserved.
//
// Licensed under the MIT License. See LICENSE in the project root for license information.
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
type DefaultSessionPermissions struct {
Type string `json:"Type"`
Allow string `json:"Allow"`
}
type CreateSession struct {
Name string `json:"Name"`
Notification bool `json:"Notification"`
Restricted bool `json:"Restricted"`
SessionEndDate time.Time `json:"SessionEndDate"`
DefaultPermissions []DefaultSessionPermissions `json:"DefaultPermissions"`
}
type Session struct {
Name string `json:"Name,omitempty"`
Notification *bool `json:"Notification,omitempty"`
Restricted *bool `json:"Restricted,omitempty"`
SessionEndDate string `json:"SessionEndDate,omitempty"`
OwnerEmailOrID string `json:"OwnerEmailOrId,omitempty"`
Status string `json:"Status,omitempty"`
}
type CreateSessionResponse struct {
ID string `json:"Id"`
}
func createSession(client *http.Client, sessionName string) (*CreateSessionResponse, error) {
createSessionData := CreateSession{
Name: sessionName,
Notification: true,
Restricted: false,
SessionEndDate: time.Now().Add(time.Hour * 24 * 7 * time.Duration(4)),
DefaultPermissions: []DefaultSessionPermissions{
DefaultSessionPermissions{
Type: "SaveCopy",
Allow: "Allow",
},
DefaultSessionPermissions{
Type: "PrintCopy",
Allow: "Allow",
},
DefaultSessionPermissions{
Type: "Markup",
Allow: "Allow",
},
DefaultSessionPermissions{
Type: "MarkupAlert",
Allow: "Allow",
},
DefaultSessionPermissions{
Type: "AddDocuments",
Allow: "Allow",
},
},
}
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(createSessionData)
req, err := http.NewRequest("POST", "https://studioapi.bluebeam.com/publicapi/v1/sessions", b)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if ok, err := checkHTTPResponse(resp); !ok {
return nil, err
}
response := &CreateSessionResponse{}
json.NewDecoder(resp.Body).Decode(response)
return response, nil
}
func setSessionStatus(client *http.Client, sessionID, status string) (*SessionResponse, error) {
session := Session{Status: status}
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(session)
url := fmt.Sprintf("https://studioapi.bluebeam.com/publicapi/v1/sessions/%s", sessionID)
req, err := http.NewRequest("PUT", url, b)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if ok, err := checkHTTPResponse(resp); !ok {
return nil, err
}
response := &SessionResponse{}
json.NewDecoder(resp.Body).Decode(response)
return response, nil
}
func sessionDelete(client *http.Client, sessionID string) error {
url := fmt.Sprintf("https://studioapi.bluebeam.com/publicapi/v1/sessions/%s", sessionID)
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
resp, err := client.Do(req)
if err != nil {
return err
}
if ok, err := checkHTTPResponse(resp); !ok {
return err
}
return nil
}
func startSnapshot(client *http.Client, sessionID string, fileID int) error {
url := fmt.Sprintf("https://studioapi.bluebeam.com/publicapi/v1/sessions/%s/files/%v/snapshot", sessionID, fileID)
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return err
}
resp, err := client.Do(req)
if err != nil {
return err
}
if ok, err := checkHTTPResponse(resp); !ok {
return err
}
return nil
}
func getSnapshotStatus(client *http.Client, sessionID string, fileID int) (*SnapshotResponse, error) {
url := fmt.Sprintf("https://studioapi.bluebeam.com/publicapi/v1/sessions/%s/files/%v/snapshot", sessionID, fileID)
fmt.Println(url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if ok, err := checkHTTPResponse(resp); !ok {
return nil, err
}
response := &SnapshotResponse{}
json.NewDecoder(resp.Body).Decode(response)
return response, nil
}