blob: 6585ebae296abe7f7b7e0a965428543003a7b777 [file] [log] [blame]
Jamie Hannaford4baa1232014-09-23 15:23:04 +02001package gophercloud
Jamie Hannafordb3120f52014-09-23 15:17:57 +02002
Ash Wilsone8192ac2014-10-21 09:02:01 -04003import (
4 "encoding/json"
5 "net/http"
6)
Ash Wilsoneab6a702014-10-20 08:18:30 -04007
Ash Wilson3ce1bd82014-10-31 12:20:00 -04008/*
Ash Wilson64ba49f2014-10-31 15:31:46 -04009Result is an internal type to be used by individual resource packages, but its
10methods will be available on a wide variety of user-facing embedding types.
Ash Wilson3ce1bd82014-10-31 12:20:00 -040011
12It acts as a base struct that other Result types, returned from request
13functions, can embed for convenience. All Results capture basic information
14from the HTTP transaction that was performed, including the response body,
15HTTP headers, and any errors that happened.
16
17Generally, each Result type will have an Extract method that can be used to
18further interpret the result's payload in a specific context. Extensions or
19providers can then provide additional extraction functions to pull out
20provider- or extension-specific information as well.
21*/
Ash Wilsoneab6a702014-10-20 08:18:30 -040022type Result struct {
Ash Wilson3ce1bd82014-10-31 12:20:00 -040023 // Body is the payload of the HTTP response from the server. In most cases,
24 // this will be the deserialized JSON structure.
Ash Wilsond3dc2542014-10-20 10:10:48 -040025 Body interface{}
Ash Wilsoneab6a702014-10-20 08:18:30 -040026
Ash Wilson72e4d2c2014-10-20 10:27:30 -040027 // Header contains the HTTP header structure from the original response.
28 Header http.Header
Ash Wilsoneab6a702014-10-20 08:18:30 -040029
Ash Wilson3ce1bd82014-10-31 12:20:00 -040030 // Err is an error that occurred during the operation. It's deferred until
31 // extraction to make it easier to chain the Extract call.
Ash Wilsoneab6a702014-10-20 08:18:30 -040032 Err error
Jamie Hannafordb3120f52014-09-23 15:17:57 +020033}
Ash Wilsona6b08312014-10-02 15:27:45 -040034
Ash Wilson3ce1bd82014-10-31 12:20:00 -040035// PrettyPrintJSON creates a string containing the full response body as
36// pretty-printed JSON. It's useful for capturing test fixtures and for
37// debugging extraction bugs.
Ash Wilsone8192ac2014-10-21 09:02:01 -040038func (r Result) PrettyPrintJSON() string {
39 pretty, err := json.MarshalIndent(r.Body, "", " ")
40 if err != nil {
41 panic(err.Error())
42 }
43 return string(pretty)
44}
45
Ash Wilson64ba49f2014-10-31 15:31:46 -040046// ErrResult is an internal type to be used by individual resource packages, but
47// its methods will be available on a wide variety of user-facing embedding
48// types.
49//
50// It represents results that only contain a potential error and
Ash Wilson3ce1bd82014-10-31 12:20:00 -040051// nothing else. Usually, if the operation executed successfully, the Err field
52// will be nil; otherwise it will be stocked with a relevant error. Use the
Ash Wilson64ba49f2014-10-31 15:31:46 -040053// ExtractErr method
54// to cleanly pull it out.
Jon Perrittba2395e2014-10-27 15:23:21 -050055type ErrResult struct {
Jon Perritt0c2b0372014-10-27 15:57:29 -050056 Result
Jamie Hannaford021b35c2014-10-27 14:01:53 +010057}
58
Ash Wilson3ce1bd82014-10-31 12:20:00 -040059// ExtractErr is a function that extracts error information, or nil, from a result.
Jon Perrittba2395e2014-10-27 15:23:21 -050060func (r ErrResult) ExtractErr() error {
Jamie Hannaford021b35c2014-10-27 14:01:53 +010061 return r.Err
62}
63
Ash Wilson3ce1bd82014-10-31 12:20:00 -040064/*
Ash Wilson64ba49f2014-10-31 15:31:46 -040065HeaderResult is an internal type to be used by individual resource packages, but
66its methods will be available on a wide variety of user-facing embedding types.
Ash Wilson3ce1bd82014-10-31 12:20:00 -040067
68It represents a result that only contains an error (possibly nil) and an
69http.Header. This is used, for example, by the objectstorage packages in
70openstack, because most of the operations don't return response bodies, but do
71have relevant information in headers.
72*/
Jon Perrittd50f93e2014-10-27 14:19:27 -050073type HeaderResult struct {
Jon Perritt0c2b0372014-10-27 15:57:29 -050074 Result
Jon Perrittd50f93e2014-10-27 14:19:27 -050075}
76
77// ExtractHeader will return the http.Header and error from the HeaderResult.
Ash Wilson3ce1bd82014-10-31 12:20:00 -040078//
79// header, err := objects.Create(client, "my_container", objects.CreateOpts{}).ExtractHeader()
Jon Perrittd50f93e2014-10-27 14:19:27 -050080func (hr HeaderResult) ExtractHeader() (http.Header, error) {
81 return hr.Header, hr.Err
82}
83
Ash Wilson3ce1bd82014-10-31 12:20:00 -040084// RFC3339Milli describes a common time format used by some API responses.
Ash Wilsona6b08312014-10-02 15:27:45 -040085const RFC3339Milli = "2006-01-02T15:04:05.999999Z"
Jamie Hannaford369c9c62014-10-08 15:14:43 +020086
Ash Wilson3ce1bd82014-10-31 12:20:00 -040087/*
88Link is an internal type to be used in packages of collection resources that are
89paginated in a certain way.
90
91It's a response substructure common to many paginated collection results that is
92used to point to related pages. Usually, the one we care about is the one with
93Rel field set to "next".
94*/
Jamie Hannaford369c9c62014-10-08 15:14:43 +020095type Link struct {
96 Href string `mapstructure:"href"`
97 Rel string `mapstructure:"rel"`
98}
99
Ash Wilson3ce1bd82014-10-31 12:20:00 -0400100/*
101ExtractNextURL is an internal function useful for packages of collection
102resources that are paginated in a certain way.
103
104It attempts attempts to extract the "next" URL from slice of Link structs, or
105"" if no such URL is present.
106*/
Jamie Hannaford369c9c62014-10-08 15:14:43 +0200107func ExtractNextURL(links []Link) (string, error) {
108 var url string
109
110 for _, l := range links {
111 if l.Rel == "next" {
112 url = l.Href
113 }
114 }
115
116 if url == "" {
117 return "", nil
118 }
119
120 return url, nil
121}