-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceExists.go
58 lines (46 loc) · 1.43 KB
/
serviceExists.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
/*
ESRI REST API implementation library.
ServiceExists function.
*/
package go_esri
import (
"encoding/json"
"net/url"
"github.com/go-resty/resty/v2"
)
type existsJSON struct {
Exists bool `json:"exists"`
}
// Returns true if service exists, false otherwise, for root services folder should be empty.
// serverName can be in the form: https://www.myserver.com/server/ or https://ags.myserver.com:6443/arcgis/
func ServiceExists(token, serverName, folder, serviceName, serviceType string) (bool, error) {
// ----------------------------------------- build and validate url
baseUrl, err := url.Parse(serverName)
if err != nil {
return false, err
}
baseUrl.Path += "/admin/services/exists"
// ----------------------------------------- build url encode string to be included in the header body
v := url.Values{}
v.Set("token", token)
v.Add("f", "json")
v.Add("folderName", folder)
v.Add("serviceName", serviceName)
v.Add("type", serviceType)
// ----------------------------------------- request
req := resty.New()
resp, err := req.R().
SetHeader("Content-type", "application/x-www-form-urlencoded").
SetBody(string(v.Encode())). // convert url encoding to string first
Post(baseUrl.String())
if err != nil {
return false, err
}
// ----------------------------------------- decode json response
var obj existsJSON
err = json.Unmarshal(resp.Body(), &obj)
if err != nil {
return false, err
}
return obj.Exists, nil
}