github.com/vmware/govmomi@v0.37.2/vapi/rest/resource.go (about)

     1  /*
     2  Copyright (c) 2019 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package rest
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/json"
    22  	"io"
    23  	"net/http"
    24  	"net/url"
    25  )
    26  
    27  const (
    28  	Path = "/rest"
    29  )
    30  
    31  // Resource wraps url.URL with helpers
    32  type Resource struct {
    33  	u *url.URL
    34  }
    35  
    36  func (r *Resource) String() string {
    37  	return r.u.String()
    38  }
    39  
    40  // WithSubpath appends the provided subpath to the URL.Path
    41  func (r *Resource) WithSubpath(subpath string) *Resource {
    42  	r.u.Path += "/" + subpath
    43  	return r
    44  }
    45  
    46  // WithID appends id to the URL.Path
    47  func (r *Resource) WithID(id string) *Resource {
    48  	r.u.Path += "/id:" + id
    49  	return r
    50  }
    51  
    52  // WithAction sets adds action to the URL.RawQuery
    53  func (r *Resource) WithAction(action string) *Resource {
    54  	return r.WithParam("~action", action)
    55  }
    56  
    57  // WithParam adds one parameter on the URL.RawQuery
    58  func (r *Resource) WithParam(name string, value string) *Resource {
    59  	// ParseQuery handles empty case, and we control access to query string so shouldn't encounter an error case
    60  	params, err := url.ParseQuery(r.u.RawQuery)
    61  	if err != nil {
    62  		panic(err)
    63  	}
    64  	params[name] = append(params[name], value)
    65  	r.u.RawQuery = params.Encode()
    66  	return r
    67  }
    68  
    69  // WithPathEncodedParam appends a parameter on the URL.RawQuery,
    70  // For special cases where URL Path-style encoding is needed
    71  func (r *Resource) WithPathEncodedParam(name string, value string) *Resource {
    72  	t := &url.URL{Path: value}
    73  	encodedValue := t.String()
    74  	t = &url.URL{Path: name}
    75  	encodedName := t.String()
    76  	// ParseQuery handles empty case, and we control access to query string so shouldn't encounter an error case
    77  	params, err := url.ParseQuery(r.u.RawQuery)
    78  	if err != nil {
    79  		panic(err)
    80  	}
    81  	// Values.Encode() doesn't escape exactly how we want, so we need to build the query string ourselves
    82  	if len(params) >= 1 {
    83  		r.u.RawQuery = r.u.RawQuery + "&" + encodedName + "=" + encodedValue
    84  	} else {
    85  		r.u.RawQuery = r.u.RawQuery + encodedName + "=" + encodedValue
    86  	}
    87  	return r
    88  }
    89  
    90  // Request returns a new http.Request for the given method.
    91  // An optional body can be provided for POST and PATCH methods.
    92  func (r *Resource) Request(method string, body ...interface{}) *http.Request {
    93  	rdr := io.MultiReader() // empty body by default
    94  	if len(body) != 0 {
    95  		rdr = encode(body[0])
    96  	}
    97  	req, err := http.NewRequest(method, r.u.String(), rdr)
    98  	if err != nil {
    99  		panic(err)
   100  	}
   101  	return req
   102  }
   103  
   104  type errorReader struct {
   105  	e error
   106  }
   107  
   108  func (e errorReader) Read([]byte) (int, error) {
   109  	return -1, e.e
   110  }
   111  
   112  // encode body as JSON, deferring any errors until io.Reader is used.
   113  func encode(body interface{}) io.Reader {
   114  	var b bytes.Buffer
   115  	err := json.NewEncoder(&b).Encode(body)
   116  	if err != nil {
   117  		return errorReader{err}
   118  	}
   119  	return &b
   120  }