ducky/devices
ducky/devices/vendor/code.secondbit.org/uuid.hg/uuid.go
Add endpoint for retrieving devices. Add an endpoint for retrieving devices, either as a list or by ID. Stub endpoints for updating and deleting devices., along with TODOs marking them as things to still be completed. (Right now, accessing those endpoints is an insta-panic.) Simplify our handleCreateDevices by returning StatusUnauthorized if AuthUser fails, so we can reserve StatusForbidden for when auth succeeds but access is still denied. Also, delay the instantiation and allocation of a Response variable until we're actually going to use it. Create a handleGetDevices handler that authenticates the user, and if no ID is set, returns a list of all their Devices. If one or more IDs are set, only those Devices are returned. If ScopeViewPushToken is one of the scopes associated with the request, the push tokens for each Device will be included in the response. Otherwise, they will be omitted.
| paddy@0 | 1 package uuid |
| paddy@0 | 2 |
| paddy@0 | 3 import ( |
| paddy@0 | 4 "database/sql/driver" |
| paddy@0 | 5 "errors" |
| paddy@0 | 6 |
| paddy@16 | 7 "github.com/pborman/uuid" |
| paddy@0 | 8 ) |
| paddy@0 | 9 |
| paddy@0 | 10 var InvalidIDError = errors.New("Invalid ID format.") |
| paddy@0 | 11 |
| paddy@0 | 12 type ID uuid.UUID |
| paddy@0 | 13 |
| paddy@0 | 14 func NewID() ID { |
| paddy@0 | 15 return ID(uuid.NewRandom()) |
| paddy@0 | 16 } |
| paddy@0 | 17 |
| paddy@0 | 18 func (id ID) String() string { |
| paddy@0 | 19 return uuid.UUID(id).String() |
| paddy@0 | 20 } |
| paddy@0 | 21 |
| paddy@0 | 22 func (id ID) IsZero() bool { |
| paddy@0 | 23 if id == nil { |
| paddy@0 | 24 return true |
| paddy@0 | 25 } |
| paddy@0 | 26 if len(id) == 0 { |
| paddy@0 | 27 return true |
| paddy@0 | 28 } |
| paddy@0 | 29 return false |
| paddy@0 | 30 } |
| paddy@0 | 31 |
| paddy@0 | 32 func (id ID) Copy() ID { |
| paddy@0 | 33 resp, _ := Parse(id.String()) |
| paddy@0 | 34 // ignore the error because they asked for a copy of the ID, they |
| paddy@0 | 35 // never asked if it was valid or not. |
| paddy@0 | 36 // This is, overall, not the most efficient way to do this (we're |
| paddy@0 | 37 // essentially converting to a string and then back again) but the |
| paddy@0 | 38 // computational complexity involved is pretty minor, and it allows |
| paddy@0 | 39 // us to respect the boundaries between the packages, using only the |
| paddy@0 | 40 // exported interfaces to perform a copy. And that seems pretty |
| paddy@0 | 41 // valuable. |
| paddy@0 | 42 return resp |
| paddy@0 | 43 } |
| paddy@0 | 44 |
| paddy@0 | 45 func (id ID) MarshalJSON() ([]byte, error) { |
| paddy@16 | 46 return uuid.UUID(id).MarshalJSON() |
| paddy@0 | 47 } |
| paddy@0 | 48 |
| paddy@0 | 49 func (id ID) Value() (driver.Value, error) { |
| paddy@0 | 50 return id.String(), nil |
| paddy@0 | 51 } |
| paddy@0 | 52 |
| paddy@0 | 53 func (id *ID) Scan(src interface{}) error { |
| paddy@16 | 54 return (*uuid.UUID)(id).Scan(src) |
| paddy@0 | 55 } |
| paddy@0 | 56 |
| paddy@0 | 57 func (id *ID) UnmarshalJSON(in []byte) error { |
| paddy@16 | 58 return (*uuid.UUID)(id).UnmarshalJSON(in) |
| paddy@0 | 59 } |
| paddy@0 | 60 |
| paddy@0 | 61 func Parse(in string) (ID, error) { |
| paddy@0 | 62 id := ID(uuid.Parse(in)) |
| paddy@0 | 63 if id == nil { |
| paddy@0 | 64 return id, InvalidIDError |
| paddy@0 | 65 } |
| paddy@0 | 66 return id, nil |
| paddy@0 | 67 } |
| paddy@0 | 68 |
| paddy@0 | 69 func (id ID) Equal(other ID) bool { |
| paddy@0 | 70 return uuid.Equal(uuid.UUID(id), uuid.UUID(other)) |
| paddy@0 | 71 } |