Repository navigation
Expand file tree
/
Copy pathsuppliers.go
More file actions
138 lines (114 loc) · 4 KB
/
Copy pathsuppliers.go
File metadata and controls
138 lines (114 loc) · 4 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
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
package snipeit
import (
"context"
"fmt"
"net/http"
)
// SuppliersService handles communication with the supplier-related endpoints
// of the Snipe-IT API.
//
// Snipe-IT API docs: https://snipe-it.readme.io/reference/suppliers
type SuppliersService struct {
client *Client
}
// SupplierResponse represents the API response for a single supplier.
type SupplierResponse struct {
Response
Payload Supplier `json:"payload"`
}
// SuppliersResponse represents the API response for multiple suppliers.
type SuppliersResponse struct {
Response
Rows []Supplier `json:"rows"`
}
// List returns a list of suppliers with pagination options.
func (s *SuppliersService) List(opts *ListOptions) (*SuppliersResponse, *http.Response, error) {
return s.ListContext(context.Background(), opts)
}
// ListContext returns a list of suppliers with the provided context and pagination options.
func (s *SuppliersService) ListContext(ctx context.Context, opts *ListOptions) (*SuppliersResponse, *http.Response, error) {
u := "api/v1/suppliers"
if opts != nil {
var err error
u, err = s.client.AddOptions(u, opts)
if err != nil {
return nil, nil, err
}
}
req, err := s.client.newRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return nil, nil, err
}
var suppliers SuppliersResponse
resp, err := s.client.Do(req, &suppliers)
if err != nil {
return nil, resp, err
}
return &suppliers, resp, nil
}
// Get fetches a single supplier by its ID.
func (s *SuppliersService) Get(id int) (*SupplierResponse, *http.Response, error) {
return s.GetContext(context.Background(), id)
}
// GetContext fetches a single supplier by its ID with the provided context.
func (s *SuppliersService) GetContext(ctx context.Context, id int) (*SupplierResponse, *http.Response, error) {
u := fmt.Sprintf("api/v1/suppliers/%d", id)
req, err := s.client.newRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return nil, nil, err
}
var supplier SupplierResponse
resp, err := s.client.Do(req, &supplier)
if err != nil {
return nil, resp, err
}
return &supplier, resp, nil
}
// Create creates a new supplier in Snipe-IT.
func (s *SuppliersService) Create(supplier Supplier) (*SupplierResponse, *http.Response, error) {
return s.CreateContext(context.Background(), supplier)
}
// CreateContext creates a new supplier in Snipe-IT with the provided context.
func (s *SuppliersService) CreateContext(ctx context.Context, supplier Supplier) (*SupplierResponse, *http.Response, error) {
req, err := s.client.newRequestWithContext(ctx, http.MethodPost, "api/v1/suppliers", supplier)
if err != nil {
return nil, nil, err
}
var response SupplierResponse
resp, err := s.client.Do(req, &response)
if err != nil {
return nil, resp, err
}
return &response, resp, nil
}
// Update updates an existing supplier in Snipe-IT.
func (s *SuppliersService) Update(id int, supplier Supplier) (*SupplierResponse, *http.Response, error) {
return s.UpdateContext(context.Background(), id, supplier)
}
// UpdateContext updates an existing supplier in Snipe-IT with the provided context.
func (s *SuppliersService) UpdateContext(ctx context.Context, id int, supplier Supplier) (*SupplierResponse, *http.Response, error) {
u := fmt.Sprintf("api/v1/suppliers/%d", id)
req, err := s.client.newRequestWithContext(ctx, http.MethodPut, u, supplier)
if err != nil {
return nil, nil, err
}
var response SupplierResponse
resp, err := s.client.Do(req, &response)
if err != nil {
return nil, resp, err
}
return &response, resp, nil
}
// Delete deletes a supplier from Snipe-IT.
func (s *SuppliersService) Delete(id int) (*http.Response, error) {
return s.DeleteContext(context.Background(), id)
}
// DeleteContext deletes a supplier from Snipe-IT with the provided context.
func (s *SuppliersService) DeleteContext(ctx context.Context, id int) (*http.Response, error) {
u := fmt.Sprintf("api/v1/suppliers/%d", id)
req, err := s.client.newRequestWithContext(ctx, http.MethodDelete, u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}