Add updating devices to apiv1.
We needed a way to be able to update devices after they were created. This is
supported in the devices package, we just needed to expose it using apiv1
endpoints.
In doing so, it became apparent that allowing users to change the Owner of their
Devices wasn't properly thought through, and pending a reason to use it, I'm
just removing it. The biggest issue came when trying to return usable error
messages; we couldn't distinguish between "you don't own the device you're
trying to update" and "you're not allowed to change the owner of the device". I
also couldn't figure out _who should be able to_ change the owner of the device,
which is generally an indication that I'm building a feature before I have a use
case for it.
To support this change, the apiv1.DeviceChange type needed its Owner property
removed.
I also needed to add deviceFromAPI and devicesFromAPI helpers to return
devices.Device types from apiv1.Device types.
There's now a new validateDeviceUpdate helper that checks to ensure that a
device update request is valid and the user has the appropriate permissions.
The createRequest type now accepts a slice of Devices, not a slice of
DeviceChanges, because we want to pass the Owner in.
A new updateRequest type is created, which accepts a DeviceChange to apply.
A new handleUpdateDevice handler is created, which is assigned to the endpoint
for PATCH requests against a device ID. It checks that the user is logged in,
the Device they're trying to update exists, and that it's a valid update. If all
of that is true, the device is updated and the updated device is returned.
Finally, we had to add two new scopes to support new functionality:
ScopeUpdateOtherUserDevices allows a user to update other user's devices, and
ScopeUpdateLastSeen allows a user to update the LastSeen property of a device.
Pending some better error messages, this should be a full implementation of
updating a device, which leaves only the deletion endpoint to deal with.
2 HTTP Content-Type Autonegotiation.
4 The functions in this package implement the behaviour specified in
5 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
7 Copyright (c) 2011, Open Knowledge Foundation Ltd.
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are
14 Redistributions of source code must retain the above copyright
15 notice, this list of conditions and the following disclaimer.
17 Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in
19 the documentation and/or other materials provided with the
22 Neither the name of the Open Knowledge Foundation Ltd. nor the
23 names of its contributors may be used to endorse or promote
24 products derived from this software without specific prior written
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49 // Structure to represent a clause in an HTTP Accept Header
53 Params map[string]string
56 // For internal use, so that we can use the sort interface
57 type accept_slice []Accept
59 func (accept accept_slice) Len() int {
60 slice := []Accept(accept)
64 func (accept accept_slice) Less(i, j int) bool {
65 slice := []Accept(accept)
66 ai, aj := slice[i], slice[j]
70 if ai.Type != "*" && aj.Type == "*" {
73 if ai.SubType != "*" && aj.SubType == "*" {
79 func (accept accept_slice) Swap(i, j int) {
80 slice := []Accept(accept)
81 slice[i], slice[j] = slice[j], slice[i]
84 // Parse an Accept Header string returning a sorted list
86 func ParseAccept(header string) (accept []Accept) {
87 parts := strings.Split(header, ",")
88 accept = make([]Accept, 0, len(parts))
89 for _, part := range parts {
90 part := strings.Trim(part, " ")
93 a.Params = make(map[string]string)
96 mrp := strings.Split(part, ";")
99 sp := strings.Split(media_range, "/")
100 a.Type = strings.Trim(sp[0], " ")
103 case len(sp) == 1 && a.Type == "*":
106 a.SubType = strings.Trim(sp[1], " ")
112 accept = append(accept, a)
116 for _, param := range mrp[1:] {
117 sp := strings.SplitN(param, "=", 2)
121 token := strings.Trim(sp[0], " ")
123 a.Q, _ = strconv.ParseFloat(sp[1], 32)
125 a.Params[token] = strings.Trim(sp[1], " ")
129 accept = append(accept, a)
132 slice := accept_slice(accept)
138 // Negotiate the most appropriate content_type given the accept header
139 // and a list of alternatives.
140 func Negotiate(header string, alternatives []string) (content_type string) {
141 asp := make([][]string, 0, len(alternatives))
142 for _, ctype := range alternatives {
143 asp = append(asp, strings.SplitN(ctype, "/", 2))
145 for _, clause := range ParseAccept(header) {
146 for i, ctsp := range asp {
147 if clause.Type == ctsp[0] && clause.SubType == ctsp[1] {
148 content_type = alternatives[i]
151 if clause.Type == ctsp[0] && clause.SubType == "*" {
152 content_type = alternatives[i]
155 if clause.Type == "*" && clause.SubType == "*" {
156 content_type = alternatives[i]