response.go raw

   1  /*
   2   * Copyright 2017 Baidu, Inc.
   3   *
   4   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
   5   * except in compliance with the License. You may obtain a copy of the License at
   6   *
   7   * http://www.apache.org/licenses/LICENSE-2.0
   8   *
   9   * Unless required by applicable law or agreed to in writing, software distributed under the
  10   * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  11   * either express or implied. See the License for the specific language governing permissions
  12   * and limitations under the License.
  13   */
  14  
  15  // response.go - the custom HTTP response for BCE
  16  
  17  package http
  18  
  19  import (
  20  	"io"
  21  	"net/http"
  22  	"time"
  23  )
  24  
  25  // Response defines the general http response structure for accessing the BCE services.
  26  type Response struct {
  27  	httpResponse *http.Response // the standard libaray http.Response object
  28  	elapsedTime  time.Duration  // elapsed time just from sending request to receiving response
  29  }
  30  
  31  func (r *Response) StatusText() string {
  32  	return r.httpResponse.Status
  33  }
  34  
  35  func (r *Response) StatusCode() int {
  36  	return r.httpResponse.StatusCode
  37  }
  38  
  39  func (r *Response) Protocol() string {
  40  	return r.httpResponse.Proto
  41  }
  42  
  43  func (r *Response) HttpResponse() *http.Response {
  44  	return r.httpResponse
  45  }
  46  
  47  func (r *Response) SetHttpResponse(response *http.Response) {
  48  	r.httpResponse = response
  49  }
  50  
  51  func (r *Response) ElapsedTime() time.Duration {
  52  	return r.elapsedTime
  53  }
  54  
  55  func (r *Response) GetHeader(name string) string {
  56  	return r.httpResponse.Header.Get(name)
  57  }
  58  
  59  func (r *Response) GetHeaders() map[string]string {
  60  	header := r.httpResponse.Header
  61  	ret := make(map[string]string, len(header))
  62  	for k, v := range header {
  63  		ret[k] = v[0]
  64  	}
  65  	return ret
  66  }
  67  
  68  func (r *Response) ContentLength() int64 {
  69  	return r.httpResponse.ContentLength
  70  }
  71  
  72  func (r *Response) Body() io.ReadCloser {
  73  	return r.httpResponse.Body
  74  }
  75