github.com/openshift-online/ocm-sdk-go@v0.1.473/statusboard/v1/metadata_client.go (about)

     1  /*
     2  Copyright (c) 2020 Red Hat, Inc.
     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  // IMPORTANT: This file has been generated automatically, refrain from modifying it manually as all
    18  // your changes will be lost when the file is generated again.
    19  
    20  package v1 // github.com/openshift-online/ocm-sdk-go/statusboard/v1
    21  
    22  import (
    23  	"bufio"
    24  	"context"
    25  	"io"
    26  	"net/http"
    27  	"net/url"
    28  
    29  	"github.com/openshift-online/ocm-sdk-go/errors"
    30  	"github.com/openshift-online/ocm-sdk-go/helpers"
    31  )
    32  
    33  // MetadataRequest is the request to retrieve the metadata.
    34  type MetadataRequest struct {
    35  	transport http.RoundTripper
    36  	path      string
    37  	query     url.Values
    38  	header    http.Header
    39  }
    40  
    41  // MetadataResponse is the response for the metadata request.
    42  type MetadataResponse struct {
    43  	status int
    44  	header http.Header
    45  	err    *errors.Error
    46  	body   *Metadata
    47  }
    48  
    49  // Parameter adds a query parameter.
    50  func (r *MetadataRequest) Parameter(name string, value interface{}) *MetadataRequest {
    51  	helpers.AddValue(&r.query, name, value)
    52  	return r
    53  }
    54  
    55  // Header adds a request header.
    56  func (r *MetadataRequest) Header(name string, value interface{}) *MetadataRequest {
    57  	helpers.AddHeader(&r.header, name, value)
    58  	return r
    59  }
    60  
    61  // Send sends the metadata request, waits for the response, and returns it.
    62  //
    63  // This is a potentially lengthy operation, as it requires network communication.
    64  // Consider using a context and the SendContext method.
    65  func (r *MetadataRequest) Send() (result *MetadataResponse, err error) {
    66  	return r.SendContext(context.Background())
    67  }
    68  
    69  // SendContext sends the metadata request, waits for the response, and returns it.
    70  func (r *MetadataRequest) SendContext(ctx context.Context) (result *MetadataResponse, err error) {
    71  	query := helpers.CopyQuery(r.query)
    72  	header := helpers.CopyHeader(r.header)
    73  	uri := &url.URL{
    74  		Path:     r.path,
    75  		RawQuery: query.Encode(),
    76  	}
    77  	request := &http.Request{
    78  		Method: http.MethodGet,
    79  		URL:    uri,
    80  		Header: header,
    81  	}
    82  	if ctx != nil {
    83  		request = request.WithContext(ctx)
    84  	}
    85  	response, err := r.transport.RoundTrip(request)
    86  	if err != nil {
    87  		return
    88  	}
    89  	defer response.Body.Close()
    90  	result = &MetadataResponse{
    91  		status: response.StatusCode,
    92  		header: response.Header,
    93  	}
    94  	reader := bufio.NewReader(response.Body)
    95  	_, err = reader.Peek(1)
    96  	if err == io.EOF {
    97  		return
    98  	}
    99  	if result.status >= 400 {
   100  		result.err, err = errors.UnmarshalErrorStatus(reader, result.status)
   101  		if err != nil {
   102  			return
   103  		}
   104  		err = result.err
   105  		return
   106  	}
   107  	result.body, err = UnmarshalMetadata(reader)
   108  	if err != nil {
   109  		return
   110  	}
   111  	return
   112  }
   113  
   114  // Status returns the response status code.
   115  func (r *MetadataResponse) Status() int {
   116  	if r == nil {
   117  		return 0
   118  	}
   119  	return r.status
   120  }
   121  
   122  // Header returns header of the response.
   123  func (r *MetadataResponse) Header() http.Header {
   124  	if r == nil {
   125  		return nil
   126  	}
   127  	return r.header
   128  }
   129  
   130  // Error returns the response error.
   131  func (r *MetadataResponse) Error() *errors.Error {
   132  	if r == nil {
   133  		return nil
   134  	}
   135  	return r.err
   136  }
   137  
   138  // Body returns the response body.
   139  func (r *MetadataResponse) Body() *Metadata {
   140  	return r.body
   141  }