blob: e691cd8f4d117da7a462606ada9638b85ca2e0ec [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
8// Result acts as a base struct that other results can embed.
9type Result struct {
Ash Wilsond3dc2542014-10-20 10:10:48 -040010 // Body is the payload of the HTTP response from the server. In most cases, this will be the
11 // deserialized JSON structure.
12 Body interface{}
Ash Wilsoneab6a702014-10-20 08:18:30 -040013
Ash Wilson72e4d2c2014-10-20 10:27:30 -040014 // Header contains the HTTP header structure from the original response.
15 Header http.Header
Ash Wilsoneab6a702014-10-20 08:18:30 -040016
17 // Err is an error that occurred during the operation. It's deferred until extraction to make
18 // it easier to chain operations.
19 Err error
Jamie Hannafordb3120f52014-09-23 15:17:57 +020020}
Ash Wilsona6b08312014-10-02 15:27:45 -040021
Ash Wilsone8192ac2014-10-21 09:02:01 -040022// PrettyPrintJSON creates a string containing the full response body as pretty-printed JSON.
23func (r Result) PrettyPrintJSON() string {
24 pretty, err := json.MarshalIndent(r.Body, "", " ")
25 if err != nil {
26 panic(err.Error())
27 }
28 return string(pretty)
29}
30
Jamie Hannaford021b35c2014-10-27 14:01:53 +010031// ExtractErrResult represents results that only contain a potential error and
32// nothing else. Usually if the operation executed successfully, the Err field
33// will be nil; otherwise it will be stocked with a relevant error.
34type ExtractErrResult struct {
35 Err error
36}
37
38// Extract is a function that extracts error information from a result
39func (r ExtractErrResult) Extract() error {
40 return r.Err
41}
42
Jon Perrittd50f93e2014-10-27 14:19:27 -050043// HeaderResult represents a result that only contains an `error` (possibly nil)
44// and an http.Header. This is used, for example, by the `objectstorage` packages
45// in `openstack` because most of the operations don't return response bodies. This
46// object is a gopherclou.Result without the `Body` field.
47type HeaderResult struct {
48 Header http.Header
49 Err error
50}
51
52// ExtractHeader will return the http.Header and error from the HeaderResult.
53// Usage: header, err := objects.Create(client, "my_container", objects.CreateOpts{}).ExtractHeader()
54func (hr HeaderResult) ExtractHeader() (http.Header, error) {
55 return hr.Header, hr.Err
56}
57
Ash Wilsona6b08312014-10-02 15:27:45 -040058// RFC3339Milli describes a time format used by API responses.
59const RFC3339Milli = "2006-01-02T15:04:05.999999Z"
Jamie Hannaford369c9c62014-10-08 15:14:43 +020060
61// Link represents a structure that enables paginated collections how to
62// traverse backward or forward. The "Rel" field is usually either "next".
63type Link struct {
64 Href string `mapstructure:"href"`
65 Rel string `mapstructure:"rel"`
66}
67
68// ExtractNextURL attempts to extract the next URL from a JSON structure. It
Jamie Hannafordb280dea2014-10-24 15:14:06 +020069// follows the common convention of nesting back and next URLs in a "links"
70// JSON array.
Jamie Hannaford369c9c62014-10-08 15:14:43 +020071func ExtractNextURL(links []Link) (string, error) {
72 var url string
73
74 for _, l := range links {
75 if l.Rel == "next" {
76 url = l.Href
77 }
78 }
79
80 if url == "" {
81 return "", nil
82 }
83
84 return url, nil
85}