Example #1
0
// BuildRequestFromHandle builds a *http.Request based off a handle.Handle
func BuildRequestFromHandle(c clients.RESTClient, method string, uri string, reqHandle handle.Handle) (*http.Request, error) {
	reqType := ""
	if reqHandle != nil {
		reqType = handle.FormatEnumToMimeType(reqHandle.GetFormat())
	}
	req, err := http.NewRequest(method, c.Base()+uri, reqHandle)
	if err == nil && reqType != "" {
		req.Header.Add("Content-Type", reqType)
	}
	return req, err
}
Example #2
0
// Execute uses a client to run a request and places the results in the
// response Handle
func Execute(c clients.RESTClient, req *http.Request, responseHandle handle.ResponseHandle) error {
	clients.ApplyAuth(c, req)
	respHandleNotNil := responseHandle != nil
	var respType string
	if respHandleNotNil {
		respType = handle.FormatEnumToMimeType(responseHandle.GetFormat())
	}
	req.Header.Add("Accept", respType)
	resp, err := c.HTTPClient().Do(req)
	if err != nil {
		return err
	} else if resp.StatusCode >= 400 {
		return fmt.Errorf("HTTP call returned status %v", resp.StatusCode)
	}
	if respHandleNotNil {
		return responseHandle.AcceptResponse(resp)
	}
	return nil
}