Skip to content

Commit c0c797b

Browse files
committed
Tidy up remaining XML (#11)
* Remove XML tags from structs and remove XML HTTP functions * Remove XML tags from Account struct * Remove extra storage tests
1 parent 22b6e99 commit c0c797b

File tree

18 files changed

+202
-409
lines changed

18 files changed

+202
-409
lines changed

upcloud/account.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import "encoding/json"
44

55
// Account represents an account
66
type Account struct {
7-
Credits float64 `xml:"credits"`
8-
UserName string `xml:"username"`
7+
Credits float64 `json:"credits"`
8+
UserName string `json:"username"`
99
}
1010

1111
// UnmarshalJSON is a custom unmarshaller that deals with

upcloud/client/client.go

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,6 @@ func (c *Client) CreateRequestURL(location string) string {
6868
return fmt.Sprintf("%s%s", c.getBaseURL(), location)
6969
}
7070

71-
// PerformGetRequest performs a GET request to the specified URL and returns the response body and eventual errors
72-
func (c *Client) PerformGetRequest(url string) ([]byte, error) {
73-
request, err := http.NewRequest(http.MethodGet, url, nil)
74-
75-
if err != nil {
76-
return nil, err
77-
}
78-
79-
return c.performRequest(request)
80-
}
81-
8271
// PerformJSONGetRequest performs a GET request to the specified URL and returns the response body and eventual errors
8372
func (c *Client) PerformJSONGetRequest(url string) ([]byte, error) {
8473
request, err := http.NewRequest(http.MethodGet, url, nil)
@@ -90,23 +79,6 @@ func (c *Client) PerformJSONGetRequest(url string) ([]byte, error) {
9079
return c.performJSONRequest(request)
9180
}
9281

93-
// PerformPostRequest performs a POST request to the specified URL and returns the response body and eventual errors
94-
func (c *Client) PerformPostRequest(url string, requestBody []byte) ([]byte, error) {
95-
var bodyReader io.Reader
96-
97-
if requestBody != nil {
98-
bodyReader = bytes.NewBuffer(requestBody)
99-
}
100-
101-
request, err := http.NewRequest(http.MethodPost, url, bodyReader)
102-
103-
if err != nil {
104-
return nil, err
105-
}
106-
107-
return c.performRequest(request)
108-
}
109-
11082
// PerformJSONPostRequest performs a POST request to the specified URL and returns the response body and eventual errors
11183
func (c *Client) PerformJSONPostRequest(url string, requestBody []byte) ([]byte, error) {
11284
var bodyReader io.Reader
@@ -124,23 +96,6 @@ func (c *Client) PerformJSONPostRequest(url string, requestBody []byte) ([]byte,
12496
return c.performJSONRequest(request)
12597
}
12698

127-
// PerformPutRequest performs a PUT request to the specified URL and returns the response body and eventual errors
128-
func (c *Client) PerformPutRequest(url string, requestBody []byte) ([]byte, error) {
129-
var bodyReader io.Reader
130-
131-
if requestBody != nil {
132-
bodyReader = bytes.NewBuffer(requestBody)
133-
}
134-
135-
request, err := http.NewRequest(http.MethodPut, url, bodyReader)
136-
137-
if err != nil {
138-
return nil, err
139-
}
140-
141-
return c.performRequest(request)
142-
}
143-
14499
// PerformJSONPutRequest performs a PUT request to the specified URL and returns the response body and eventual errors
145100
func (c *Client) PerformJSONPutRequest(url string, requestBody []byte) ([]byte, error) {
146101
var bodyReader io.Reader
@@ -158,18 +113,6 @@ func (c *Client) PerformJSONPutRequest(url string, requestBody []byte) ([]byte,
158113
return c.performJSONRequest(request)
159114
}
160115

161-
// PerformDeleteRequest performs a DELETE request to the specified URL and returns the response body and eventual errors
162-
func (c *Client) PerformDeleteRequest(url string) error {
163-
request, err := http.NewRequest(http.MethodDelete, url, nil)
164-
165-
if err != nil {
166-
return err
167-
}
168-
169-
_, err = c.performRequest(request)
170-
return err
171-
}
172-
173116
// PerformJSONDeleteRequest performs a DELETE request to the specified URL and returns the response body and eventual errors
174117
func (c *Client) PerformJSONDeleteRequest(url string) error {
175118
request, err := http.NewRequest(http.MethodDelete, url, nil)
@@ -182,15 +125,6 @@ func (c *Client) PerformJSONDeleteRequest(url string) error {
182125
return err
183126
}
184127

185-
// Adds common headers to the specified request
186-
func (c *Client) addRequestHeaders(request *http.Request) *http.Request {
187-
request.SetBasicAuth(c.userName, c.password)
188-
request.Header.Add("Accept", "application/xml")
189-
request.Header.Add("Content-Type", "application/xml")
190-
191-
return request
192-
}
193-
194128
// Adds common headers to the specified request
195129
func (c *Client) addJSONRequestHeaders(request *http.Request) *http.Request {
196130
request.SetBasicAuth(c.userName, c.password)
@@ -200,18 +134,6 @@ func (c *Client) addJSONRequestHeaders(request *http.Request) *http.Request {
200134
return request
201135
}
202136

203-
// Performs the specified HTTP request and returns the response through handleResponse()
204-
func (c *Client) performRequest(request *http.Request) ([]byte, error) {
205-
c.addRequestHeaders(request)
206-
response, err := c.httpClient.Do(request)
207-
208-
if err != nil {
209-
return nil, err
210-
}
211-
212-
return handleResponse(response)
213-
}
214-
215137
// Performs the specified HTTP request and returns the response through handleResponse()
216138
func (c *Client) performJSONRequest(request *http.Request) ([]byte, error) {
217139
c.addJSONRequestHeaders(request)

upcloud/error.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77

88
// Error represents an error
99
type Error struct {
10-
ErrorCode string `xml:"error_code" json:"error_code"`
11-
ErrorMessage string `xml:"error_message" json:"error_message"`
10+
ErrorCode string `json:"error_code"`
11+
ErrorMessage string `json:"error_message"`
1212
}
1313

1414
// UnmarshalJSON is a custom unmarshaller that deals with

upcloud/firewall.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const (
1818

1919
// FirewallRules represents a list of firewall rules
2020
type FirewallRules struct {
21-
FirewallRules []FirewallRule `xml:"firewall_rule" json:"firewall_rules"`
21+
FirewallRules []FirewallRule `json:"firewall_rules"`
2222
}
2323

2424
// UnmarshalJSON is a custom unmarshaller that deals with
@@ -46,21 +46,21 @@ func (s *FirewallRules) UnmarshalJSON(b []byte) error {
4646

4747
// FirewallRule represents a single firewall rule. Note that most integer values are represented as strings
4848
type FirewallRule struct {
49-
Action string `xml:"action" json:"action"`
50-
Comment string `xml:"comment,omitempty" json:"comment,omitempty"`
51-
DestinationAddressStart string `xml:"destination_address_start,omitempty" json:"destination_address_start,omitempty"`
52-
DestinationAddressEnd string `xml:"destination_address_end,omitempty" json:"destination_address_end,omitempty"`
53-
DestinationPortStart string `xml:"destination_port_start,omitempty" json:"destination_port_start,omitempty"`
54-
DestinationPortEnd string `xml:"destination_port_end,omitempty" json:"destination_port_end,omitempty"`
55-
Direction string `xml:"direction" json:"direction"`
56-
Family string `xml:"family" json:"family"`
57-
ICMPType string `xml:"icmp_type,omitempty" json:"icmp_type,omitempty"`
58-
Position int `xml:"position" json:"position,string"`
59-
Protocol string `xml:"protocol,omitempty" json:"protocol,omitempty"`
60-
SourceAddressStart string `xml:"source_address_start,omitempty" json:"source_address_start,omitempty"`
61-
SourceAddressEnd string `xml:"source_address_end,omitempty" json:"source_address_end,omitempty"`
62-
SourcePortStart string `xml:"source_port_start,omitempty" json:"source_port_start,omitempty"`
63-
SourcePortEnd string `xml:"source_port_end,omitempty" json:"source_port_end,omitempty"`
49+
Action string `json:"action"`
50+
Comment string `json:"comment,omitempty"`
51+
DestinationAddressStart string `json:"destination_address_start,omitempty"`
52+
DestinationAddressEnd string `json:"destination_address_end,omitempty"`
53+
DestinationPortStart string `json:"destination_port_start,omitempty"`
54+
DestinationPortEnd string `json:"destination_port_end,omitempty"`
55+
Direction string `json:"direction"`
56+
Family string `json:"family"`
57+
ICMPType string `json:"icmp_type,omitempty"`
58+
Position int `json:"position,string"`
59+
Protocol string `json:"protocol,omitempty"`
60+
SourceAddressStart string `json:"source_address_start,omitempty"`
61+
SourceAddressEnd string `json:"source_address_end,omitempty"`
62+
SourcePortStart string `json:"source_port_start,omitempty"`
63+
SourcePortEnd string `json:"source_port_end,omitempty"`
6464
}
6565

6666
// UnmarshalJSON is a custom unmarshaller that deals with

upcloud/ip_address.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const (
1313

1414
// IPAddresses represents a /ip_address response
1515
type IPAddresses struct {
16-
IPAddresses []IPAddress `xml:"ip_address" json:"ip_addresses"`
16+
IPAddresses []IPAddress `json:"ip_addresses"`
1717
}
1818

1919
// UnmarshalJSON is a custom unmarshaller that deals with
@@ -41,13 +41,13 @@ func (s *IPAddresses) UnmarshalJSON(b []byte) error {
4141

4242
// IPAddress represents an IP address
4343
type IPAddress struct {
44-
Access string `xml:"access" json:"access"`
45-
Address string `xml:"address" json:"address"`
46-
Family string `xml:"family" json:"family"`
44+
Access string `json:"access"`
45+
Address string `json:"address"`
46+
Family string `json:"family"`
4747
// TODO: Convert to boolean
48-
PartOfPlan string `xml:"part_of_plan" json:"part_of_plan"`
49-
PTRRecord string `xml:"ptr_record" json:"ptr_record"`
50-
ServerUUID string `xml:"server" json:"server"`
48+
PartOfPlan string `json:"part_of_plan"`
49+
PTRRecord string `json:"ptr_record"`
50+
ServerUUID string `json:"server"`
5151
}
5252

5353
// UnmarshalJSON is a custom unmarshaller that deals with

upcloud/plan.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import "encoding/json"
44

55
// Plans represents a /plan response
66
type Plans struct {
7-
Plans []Plan `xml:"plan" json:"plans"`
7+
Plans []Plan `json:"plans"`
88
}
99

1010
// UnmarshalJSON is a custom unmarshaller that deals with
@@ -29,10 +29,10 @@ func (s *Plans) UnmarshalJSON(b []byte) error {
2929

3030
// Plan represents a pre-configured server configuration plan
3131
type Plan struct {
32-
CoreNumber int `xml:"core_number" json:"core_number"`
33-
MemoryAmount int `xml:"memory_amount" json:"memory_amount"`
34-
Name string `xml:"name" json:"name"`
35-
PublicTrafficOut int `xml:"public_traffic_out" json:"public_traffic_out"`
36-
StorageSize int `xml:"storage_size" json:"storage_size"`
37-
StorageTier string `xml:"storage_tier" json:"storage_tier"`
32+
CoreNumber int `json:"core_number"`
33+
MemoryAmount int `json:"memory_amount"`
34+
Name string `json:"name"`
35+
PublicTrafficOut int `json:"public_traffic_out"`
36+
StorageSize int `json:"storage_size"`
37+
StorageTier string `json:"storage_tier"`
3838
}

upcloud/price.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import "encoding/json"
44

55
// PriceZones represents a /price response
66
type PriceZones struct {
7-
PriceZones []PriceZone `xml:"prices" json:"prices"`
7+
PriceZones []PriceZone `json:"prices"`
88
}
99

1010
// UnmarshalJSON is a custom unmarshaller that deals with
@@ -29,30 +29,30 @@ func (s *PriceZones) UnmarshalJSON(b []byte) error {
2929

3030
// PriceZone represents a price zone. A prize zone consists of multiple items that each have a price.
3131
type PriceZone struct {
32-
Name string `xml:"name" json:"name"`
33-
34-
Firewall *Price `xml:"firewall" json:"firewall"`
35-
IORequestBackup *Price `xml:"io_request_backup" json:"io_request_backup"`
36-
IORequestMaxIOPS *Price `xml:"io_request_maxiops" json:"io_request_maxiops"`
37-
IPv4Address *Price `xml:"ipv4_address" json:"ipv4_address"`
38-
IPv6Address *Price `xml:"ipv6_address" json:"ipv6_address"`
39-
PublicIPv4BandwidthIn *Price `xml:"public_ipv4_bandwidth_in" json:"public_ipv4_bandwidth_in"`
40-
PublicIPv4BandwidthOut *Price `xml:"public_ipv4_bandwidth_out" json:"public_ipv4_bandwidth_out"`
41-
PublicIPv6BandwidthIn *Price `xml:"public_ipv6_bandwidth_in" json:"public_ipv6_bandwidth_in"`
42-
PublicIPv6BandwidthOut *Price `xml:"public_ipv6_bandwidth_out" json:"public_ipv6_bandwidth_out"`
43-
ServerCore *Price `xml:"server_core" json:"server_core"`
44-
ServerMemory *Price `xml:"server_memory" json:"server_memory"`
45-
ServerPlan1xCPU1GB *Price `xml:"server_plan_1xCPU-1GB" json:"server_plan_1xCPU-1GB"`
46-
ServerPlan2xCPU2GB *Price `xml:"server_plan_2xCPU-2GB" json:"server_plan_1xCPU-2GB"`
47-
ServerPlan4xCPU4GB *Price `xml:"server_plan_4xCPU-4GB" json:"server_plan_4xCPU-4GB"`
48-
ServerPlan6xCPU8GB *Price `xml:"server_plan_6xCPU-8GB" json:"server_plan_6xCPU-8GB"`
49-
StorageBackup *Price `xml:"storage_backup" json:"storage_backup"`
50-
StorageMaxIOPS *Price `xml:"storage_maxiops" json:"storage_maxiops"`
51-
StorageTemplate *Price `xml:"storage_template" json:"storage_template"`
32+
Name string `json:"name"`
33+
34+
Firewall *Price `json:"firewall"`
35+
IORequestBackup *Price `json:"io_request_backup"`
36+
IORequestMaxIOPS *Price `json:"io_request_maxiops"`
37+
IPv4Address *Price `json:"ipv4_address"`
38+
IPv6Address *Price `json:"ipv6_address"`
39+
PublicIPv4BandwidthIn *Price `json:"public_ipv4_bandwidth_in"`
40+
PublicIPv4BandwidthOut *Price `json:"public_ipv4_bandwidth_out"`
41+
PublicIPv6BandwidthIn *Price `json:"public_ipv6_bandwidth_in"`
42+
PublicIPv6BandwidthOut *Price `json:"public_ipv6_bandwidth_out"`
43+
ServerCore *Price `json:"server_core"`
44+
ServerMemory *Price `json:"server_memory"`
45+
ServerPlan1xCPU1GB *Price `json:"server_plan_1xCPU-1GB"`
46+
ServerPlan2xCPU2GB *Price `json:"server_plan_1xCPU-2GB"`
47+
ServerPlan4xCPU4GB *Price `json:"server_plan_4xCPU-4GB"`
48+
ServerPlan6xCPU8GB *Price `json:"server_plan_6xCPU-8GB"`
49+
StorageBackup *Price `json:"storage_backup"`
50+
StorageMaxIOPS *Price `json:"storage_maxiops"`
51+
StorageTemplate *Price `json:"storage_template"`
5252
}
5353

5454
// Price represents a price
5555
type Price struct {
56-
Amount int `xml:"amount" json:"amount"`
57-
Price float64 `xml:"price" json:"price"`
56+
Amount int `json:"amount"`
57+
Price float64 `json:"price"`
5858
}

upcloud/request/firewall.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package request
22

33
import (
44
"encoding/json"
5-
"encoding/xml"
65
"fmt"
76

87
"github.com/UpCloudLtd/upcloud-go-api/upcloud"
@@ -33,8 +32,7 @@ func (r *GetFirewallRuleDetailsRequest) RequestURL() string {
3332
type CreateFirewallRuleRequest struct {
3433
upcloud.FirewallRule
3534

36-
XMLName xml.Name `xml:"firewall_rule" json:"-"`
37-
ServerUUID string `xml:"-" json:"-"`
35+
ServerUUID string `json:"-"`
3836
}
3937

4038
// RequestURL implements the Request interface

upcloud/request/ip_address.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package request
22

33
import (
44
"encoding/json"
5-
"encoding/xml"
65
"fmt"
76
)
87

@@ -18,11 +17,9 @@ func (r *GetIPAddressDetailsRequest) RequestURL() string {
1817

1918
// AssignIPAddressRequest represents a request to assign a new IP address to a server
2019
type AssignIPAddressRequest struct {
21-
XMLName xml.Name `xml:"ip_address" json:"-"`
22-
23-
Access string `xml:"access" json:"access"`
24-
Family string `xml:"family,omitempty" json:"family,omitempty"`
25-
ServerUUID string `xml:"server" json:"server"`
20+
Access string `json:"access"`
21+
Family string `json:"family,omitempty"`
22+
ServerUUID string `json:"server"`
2623
}
2724

2825
// RequestURL implements the Request interface
@@ -44,10 +41,9 @@ func (r AssignIPAddressRequest) MarshalJSON() ([]byte, error) {
4441

4542
// ModifyIPAddressRequest represents a request to modify the PTR DNS record of a specific IP address
4643
type ModifyIPAddressRequest struct {
47-
XMLName xml.Name `xml:"ip_address" json:"-"`
48-
IPAddress string `xml:"-" json:"-"`
44+
IPAddress string `json:"-"`
4945

50-
PTRRecord string `xml:"ptr_record" json:"ptr_record"`
46+
PTRRecord string `json:"ptr_record"`
5147
}
5248

5349
// RequestURL implements the Request interface

0 commit comments

Comments
 (0)