...

Source file src/github.com/grpc-ecosystem/grpc-gateway/examples/internal/clients/abe/client.go

Documentation: github.com/grpc-ecosystem/grpc-gateway/examples/internal/clients/abe

     1  /*
     2   * A Bit of Everything
     3   *
     4   * No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
     5   *
     6   * API version: 1.0
     7   * Contact: none@example.com
     8   * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
     9   */
    10  
    11  package abe
    12  
    13  import (
    14  	"bytes"
    15  	"context"
    16  	"encoding/json"
    17  	"encoding/xml"
    18  	"errors"
    19  	"fmt"
    20  	"io"
    21  	"mime/multipart"
    22  	"net/http"
    23  	"net/url"
    24  	"os"
    25  	"path/filepath"
    26  	"reflect"
    27  	"regexp"
    28  	"strconv"
    29  	"strings"
    30  	"time"
    31  	"unicode/utf8"
    32  
    33  	"golang.org/x/oauth2"
    34  )
    35  
    36  var (
    37  	jsonCheck = regexp.MustCompile("(?i:[application|text]/json)")
    38  	xmlCheck  = regexp.MustCompile("(?i:[application|text]/xml)")
    39  )
    40  
    41  // APIClient manages communication with the A Bit of Everything API v1.0
    42  // In most cases there should be only one, shared, APIClient.
    43  type APIClient struct {
    44  	cfg    *Configuration
    45  	common service // Reuse a single struct instead of allocating one for each service on the heap.
    46  
    47  	// API Services
    48  
    49  	ABitOfEverythingServiceApi *ABitOfEverythingServiceApiService
    50  
    51  	CamelCaseServiceNameApi *CamelCaseServiceNameApiService
    52  
    53  	EchoRpcApi *EchoRpcApiService
    54  }
    55  
    56  type service struct {
    57  	client *APIClient
    58  }
    59  
    60  // NewAPIClient creates a new API client. Requires a userAgent string describing your application.
    61  // optionally a custom http.Client to allow for advanced features such as caching.
    62  func NewAPIClient(cfg *Configuration) *APIClient {
    63  	if cfg.HTTPClient == nil {
    64  		cfg.HTTPClient = http.DefaultClient
    65  	}
    66  
    67  	c := &APIClient{}
    68  	c.cfg = cfg
    69  	c.common.client = c
    70  
    71  	// API Services
    72  	c.ABitOfEverythingServiceApi = (*ABitOfEverythingServiceApiService)(&c.common)
    73  	c.CamelCaseServiceNameApi = (*CamelCaseServiceNameApiService)(&c.common)
    74  	c.EchoRpcApi = (*EchoRpcApiService)(&c.common)
    75  
    76  	return c
    77  }
    78  
    79  func atoi(in string) (int, error) {
    80  	return strconv.Atoi(in)
    81  }
    82  
    83  // selectHeaderContentType select a content type from the available list.
    84  func selectHeaderContentType(contentTypes []string) string {
    85  	if len(contentTypes) == 0 {
    86  		return ""
    87  	}
    88  	if contains(contentTypes, "application/json") {
    89  		return "application/json"
    90  	}
    91  	return contentTypes[0] // use the first content type specified in 'consumes'
    92  }
    93  
    94  // selectHeaderAccept join all accept types and return
    95  func selectHeaderAccept(accepts []string) string {
    96  	if len(accepts) == 0 {
    97  		return ""
    98  	}
    99  
   100  	if contains(accepts, "application/json") {
   101  		return "application/json"
   102  	}
   103  
   104  	return strings.Join(accepts, ",")
   105  }
   106  
   107  // contains is a case insenstive match, finding needle in a haystack
   108  func contains(haystack []string, needle string) bool {
   109  	for _, a := range haystack {
   110  		if strings.ToLower(a) == strings.ToLower(needle) {
   111  			return true
   112  		}
   113  	}
   114  	return false
   115  }
   116  
   117  // Verify optional parameters are of the correct type.
   118  func typeCheckParameter(obj interface{}, expected string, name string) error {
   119  	// Make sure there is an object.
   120  	if obj == nil {
   121  		return nil
   122  	}
   123  
   124  	// Check the type is as expected.
   125  	if reflect.TypeOf(obj).String() != expected {
   126  		return fmt.Errorf("Expected %s to be of type %s but received %s.", name, expected, reflect.TypeOf(obj).String())
   127  	}
   128  	return nil
   129  }
   130  
   131  // parameterToString convert interface{} parameters to string, using a delimiter if format is provided.
   132  func parameterToString(obj interface{}, collectionFormat string) string {
   133  	var delimiter string
   134  
   135  	switch collectionFormat {
   136  	case "pipes":
   137  		delimiter = "|"
   138  	case "ssv":
   139  		delimiter = " "
   140  	case "tsv":
   141  		delimiter = "\t"
   142  	case "csv":
   143  		delimiter = ","
   144  	}
   145  
   146  	if reflect.TypeOf(obj).Kind() == reflect.Slice {
   147  		return strings.Trim(strings.Replace(fmt.Sprint(obj), " ", delimiter, -1), "[]")
   148  	}
   149  
   150  	return fmt.Sprintf("%v", obj)
   151  }
   152  
   153  // callAPI do the request.
   154  func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) {
   155  	return c.cfg.HTTPClient.Do(request)
   156  }
   157  
   158  // Change base path to allow switching to mocks
   159  func (c *APIClient) ChangeBasePath(path string) {
   160  	c.cfg.BasePath = path
   161  }
   162  
   163  // prepareRequest build the request
   164  func (c *APIClient) prepareRequest(
   165  	ctx context.Context,
   166  	path string, method string,
   167  	postBody interface{},
   168  	headerParams map[string]string,
   169  	queryParams url.Values,
   170  	formParams url.Values,
   171  	fileName string,
   172  	fileBytes []byte) (localVarRequest *http.Request, err error) {
   173  
   174  	var body *bytes.Buffer
   175  
   176  	// Detect postBody type and post.
   177  	if postBody != nil {
   178  		contentType := headerParams["Content-Type"]
   179  		if contentType == "" {
   180  			contentType = detectContentType(postBody)
   181  			headerParams["Content-Type"] = contentType
   182  		}
   183  
   184  		body, err = setBody(postBody, contentType)
   185  		if err != nil {
   186  			return nil, err
   187  		}
   188  	}
   189  
   190  	// add form parameters and file if available.
   191  	if len(formParams) > 0 || (len(fileBytes) > 0 && fileName != "") {
   192  		if body != nil {
   193  			return nil, errors.New("Cannot specify postBody and multipart form at the same time.")
   194  		}
   195  		body = &bytes.Buffer{}
   196  		w := multipart.NewWriter(body)
   197  
   198  		for k, v := range formParams {
   199  			for _, iv := range v {
   200  				if strings.HasPrefix(k, "@") { // file
   201  					err = addFile(w, k[1:], iv)
   202  					if err != nil {
   203  						return nil, err
   204  					}
   205  				} else { // form value
   206  					w.WriteField(k, iv)
   207  				}
   208  			}
   209  		}
   210  		if len(fileBytes) > 0 && fileName != "" {
   211  			w.Boundary()
   212  			//_, fileNm := filepath.Split(fileName)
   213  			part, err := w.CreateFormFile("file", filepath.Base(fileName))
   214  			if err != nil {
   215  				return nil, err
   216  			}
   217  			_, err = part.Write(fileBytes)
   218  			if err != nil {
   219  				return nil, err
   220  			}
   221  			// Set the Boundary in the Content-Type
   222  			headerParams["Content-Type"] = w.FormDataContentType()
   223  		}
   224  
   225  		// Set Content-Length
   226  		headerParams["Content-Length"] = fmt.Sprintf("%d", body.Len())
   227  		w.Close()
   228  	}
   229  
   230  	// Setup path and query parameters
   231  	url, err := url.Parse(path)
   232  	if err != nil {
   233  		return nil, err
   234  	}
   235  
   236  	// Adding Query Param
   237  	query := url.Query()
   238  	for k, v := range queryParams {
   239  		for _, iv := range v {
   240  			query.Add(k, iv)
   241  		}
   242  	}
   243  
   244  	// Encode the parameters.
   245  	url.RawQuery = query.Encode()
   246  
   247  	// Generate a new request
   248  	if body != nil {
   249  		localVarRequest, err = http.NewRequest(method, url.String(), body)
   250  	} else {
   251  		localVarRequest, err = http.NewRequest(method, url.String(), nil)
   252  	}
   253  	if err != nil {
   254  		return nil, err
   255  	}
   256  
   257  	// add header parameters, if any
   258  	if len(headerParams) > 0 {
   259  		headers := http.Header{}
   260  		for h, v := range headerParams {
   261  			headers.Set(h, v)
   262  		}
   263  		localVarRequest.Header = headers
   264  	}
   265  
   266  	// Override request host, if applicable
   267  	if c.cfg.Host != "" {
   268  		localVarRequest.Host = c.cfg.Host
   269  	}
   270  
   271  	// Add the user agent to the request.
   272  	localVarRequest.Header.Add("User-Agent", c.cfg.UserAgent)
   273  
   274  	if ctx != nil {
   275  		// add context to the request
   276  		localVarRequest = localVarRequest.WithContext(ctx)
   277  
   278  		// Walk through any authentication.
   279  
   280  		// OAuth2 authentication
   281  		if tok, ok := ctx.Value(ContextOAuth2).(oauth2.TokenSource); ok {
   282  			// We were able to grab an oauth2 token from the context
   283  			var latestToken *oauth2.Token
   284  			if latestToken, err = tok.Token(); err != nil {
   285  				return nil, err
   286  			}
   287  
   288  			latestToken.SetAuthHeader(localVarRequest)
   289  		}
   290  
   291  		// Basic HTTP Authentication
   292  		if auth, ok := ctx.Value(ContextBasicAuth).(BasicAuth); ok {
   293  			localVarRequest.SetBasicAuth(auth.UserName, auth.Password)
   294  		}
   295  
   296  		// AccessToken Authentication
   297  		if auth, ok := ctx.Value(ContextAccessToken).(string); ok {
   298  			localVarRequest.Header.Add("Authorization", "Bearer "+auth)
   299  		}
   300  	}
   301  
   302  	for header, value := range c.cfg.DefaultHeader {
   303  		localVarRequest.Header.Add(header, value)
   304  	}
   305  
   306  	return localVarRequest, nil
   307  }
   308  
   309  func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err error) {
   310  		if strings.Contains(contentType, "application/xml") {
   311  			if err = xml.Unmarshal(b, v); err != nil {
   312  				return err
   313  			}
   314  			return nil
   315  		} else if strings.Contains(contentType, "application/json") {
   316  			if err = json.Unmarshal(b, v); err != nil {
   317  				return err
   318  			}
   319  			return nil
   320  		}
   321  	return errors.New("undefined response type")
   322  }
   323  
   324  // Add a file to the multipart request
   325  func addFile(w *multipart.Writer, fieldName, path string) error {
   326  	file, err := os.Open(path)
   327  	if err != nil {
   328  		return err
   329  	}
   330  	defer file.Close()
   331  
   332  	part, err := w.CreateFormFile(fieldName, filepath.Base(path))
   333  	if err != nil {
   334  		return err
   335  	}
   336  	_, err = io.Copy(part, file)
   337  
   338  	return err
   339  }
   340  
   341  // Prevent trying to import "fmt"
   342  func reportError(format string, a ...interface{}) error {
   343  	return fmt.Errorf(format, a...)
   344  }
   345  
   346  // Set request body from an interface{}
   347  func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err error) {
   348  	if bodyBuf == nil {
   349  		bodyBuf = &bytes.Buffer{}
   350  	}
   351  
   352  	if reader, ok := body.(io.Reader); ok {
   353  		_, err = bodyBuf.ReadFrom(reader)
   354  	} else if b, ok := body.([]byte); ok {
   355  		_, err = bodyBuf.Write(b)
   356  	} else if s, ok := body.(string); ok {
   357  		_, err = bodyBuf.WriteString(s)
   358  	} else if s, ok := body.(*string); ok {
   359  		_, err = bodyBuf.WriteString(*s)
   360  	} else if jsonCheck.MatchString(contentType) {
   361  		err = json.NewEncoder(bodyBuf).Encode(body)
   362  	} else if xmlCheck.MatchString(contentType) {
   363  		xml.NewEncoder(bodyBuf).Encode(body)
   364  	}
   365  
   366  	if err != nil {
   367  		return nil, err
   368  	}
   369  
   370  	if bodyBuf.Len() == 0 {
   371  		err = fmt.Errorf("Invalid body type %s\n", contentType)
   372  		return nil, err
   373  	}
   374  	return bodyBuf, nil
   375  }
   376  
   377  // detectContentType method is used to figure out `Request.Body` content type for request header
   378  func detectContentType(body interface{}) string {
   379  	contentType := "text/plain; charset=utf-8"
   380  	kind := reflect.TypeOf(body).Kind()
   381  
   382  	switch kind {
   383  	case reflect.Struct, reflect.Map, reflect.Ptr:
   384  		contentType = "application/json; charset=utf-8"
   385  	case reflect.String:
   386  		contentType = "text/plain; charset=utf-8"
   387  	default:
   388  		if b, ok := body.([]byte); ok {
   389  			contentType = http.DetectContentType(b)
   390  		} else if kind == reflect.Slice {
   391  			contentType = "application/json; charset=utf-8"
   392  		}
   393  	}
   394  
   395  	return contentType
   396  }
   397  
   398  // Ripped from https://github.com/gregjones/httpcache/blob/master/httpcache.go
   399  type cacheControl map[string]string
   400  
   401  func parseCacheControl(headers http.Header) cacheControl {
   402  	cc := cacheControl{}
   403  	ccHeader := headers.Get("Cache-Control")
   404  	for _, part := range strings.Split(ccHeader, ",") {
   405  		part = strings.Trim(part, " ")
   406  		if part == "" {
   407  			continue
   408  		}
   409  		if strings.ContainsRune(part, '=') {
   410  			keyval := strings.Split(part, "=")
   411  			cc[strings.Trim(keyval[0], " ")] = strings.Trim(keyval[1], ",")
   412  		} else {
   413  			cc[part] = ""
   414  		}
   415  	}
   416  	return cc
   417  }
   418  
   419  // CacheExpires helper function to determine remaining time before repeating a request.
   420  func CacheExpires(r *http.Response) time.Time {
   421  	// Figure out when the cache expires.
   422  	var expires time.Time
   423  	now, err := time.Parse(time.RFC1123, r.Header.Get("date"))
   424  	if err != nil {
   425  		return time.Now()
   426  	}
   427  	respCacheControl := parseCacheControl(r.Header)
   428  
   429  	if maxAge, ok := respCacheControl["max-age"]; ok {
   430  		lifetime, err := time.ParseDuration(maxAge + "s")
   431  		if err != nil {
   432  			expires = now
   433  		}
   434  		expires = now.Add(lifetime)
   435  	} else {
   436  		expiresHeader := r.Header.Get("Expires")
   437  		if expiresHeader != "" {
   438  			expires, err = time.Parse(time.RFC1123, expiresHeader)
   439  			if err != nil {
   440  				expires = now
   441  			}
   442  		}
   443  	}
   444  	return expires
   445  }
   446  
   447  func strlen(s string) int {
   448  	return utf8.RuneCountInString(s)
   449  }
   450  
   451  // GenericSwaggerError Provides access to the body, error and model on returned errors.
   452  type GenericSwaggerError struct {
   453  	body  []byte
   454  	error string
   455  	model interface{}
   456  }
   457  
   458  // Error returns non-empty string if there was an error.
   459  func (e GenericSwaggerError) Error() string {
   460  	return e.error
   461  }
   462  
   463  // Body returns the raw bytes of the response
   464  func (e GenericSwaggerError) Body() []byte {
   465  	return e.body
   466  }
   467  
   468  // Model returns the unpacked model of the error
   469  func (e GenericSwaggerError) Model() interface{} {
   470  	return e.model
   471  }

View as plain text