-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlocations.go
95 lines (83 loc) · 2.62 KB
/
locations.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
// Copyright 2020 Eurac Research. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package snipeit
import (
"fmt"
"net/http"
)
// LocationOptions specifies a subset of optional query parameters for listing
// locations.
type LocationOptions struct {
Limit int `url:"limit,omitempty"`
Offset int `url:"offset,omitempty"`
Search string `url:"search,omitempty"`
Sort string `url:"sort,omitempty"`
Order string `url:"order,omitempty"`
}
// Location represents a Snipe-IT location.
type Location struct {
ID int64 `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Image string `json:"image,omitempty"`
Address string `json:"address,omitempty"`
Address2 string `json:"address2,omitempty"`
City string `json:"city,omitempty"`
State string `json:"state,omitempty"`
Country string `json:"country,omitempty"`
Zip string `json:"zip,omitempty"`
AssetsAssigned int64 `json:"assigned_assets_count,omitempty"`
Assets int64 `json:"assets_count,omitempty"`
Users int64 `json:"users_count,omitempty"`
Currency string `json:"currency,omitempty"`
CreatedAt Timestamp `json:"created_at,omitempty"`
UpdatedAt Timestamp `json:"updated_at,omitempty"`
Parent struct {
ID int64 `json:"id,omitempty"`
Name string `json:"name,omitempty"`
} `json:"parent,omitempty"`
Manager string `json:"manager,omitempty"`
Children []Location `json:"children,omitempty"`
Actions struct {
Update bool
Delete bool
} `json:"available_actions,omitempty"`
}
// Locations lists all locations.
//
// Snipe-IT API doc: https://snipe-it.readme.io/reference#locations
func (c *Client) Locations(opt *LocationOptions) ([]*Location, *http.Response, error) {
u, err := c.AddOptions("locations", opt)
if err != nil {
return nil, nil, err
}
req, err := c.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, nil, err
}
var response struct {
Total int64
Rows []*Location
}
resp, err := c.Do(req, &response)
if err != nil {
return nil, resp, err
}
return response.Rows, resp, nil
}
// Location by ID.
//
// Snipe-IT API doc: https://snipe-it.readme.io/reference#locations-1
func (c *Client) Location(id int64) (*Location, *http.Response, error) {
u := fmt.Sprintf("locations/%d", id)
req, err := c.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, nil, err
}
l := new(Location)
resp, err := c.Do(req, l)
if err != nil {
return nil, resp, err
}
return l, resp, nil
}