Skip to content

Commit

Permalink
Drop unnecessary types.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsnowcurrently committed Oct 28, 2014
1 parent 67698e8 commit dbe0360
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 36 deletions.
24 changes: 16 additions & 8 deletions api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
apihttp "github.com/juju/juju/api/http"
)

var _ apihttp.Client = (*State)(nil)

var newHTTPClient = func(s *State) apihttp.HTTPClient {
return s.NewHTTPClient()
}
Expand All @@ -35,7 +33,7 @@ func (s *State) NewHTTPClient() *http.Client {
}

// NewHTTPRequest returns a new API-supporting HTTP request based on State.
func (s *State) NewHTTPRequest(method, path string) (*apihttp.Request, error) {
func (s *State) NewHTTPRequest(method, path string) (*http.Request, error) {
baseURL, err := url.Parse(s.serverRoot)
if err != nil {
return nil, errors.Annotatef(err, "while parsing base URL (%s)", s.serverRoot)
Expand All @@ -51,12 +49,22 @@ func (s *State) NewHTTPRequest(method, path string) (*apihttp.Request, error) {
return req, errors.Trace(err)
}

// SendHTTPRequest sends the request using the HTTP client derived from State.
func (s *State) SendHTTPRequest(req *apihttp.Request) (*http.Response, error) {
// SendHTTPRequest sends a request using the HTTP client derived from State.
func (s *State) SendHTTPRequest(method, path string, args interface{}) (*http.Request, *http.Response, error) {
req, err := s.NewHTTPRequest(method, path)
if err != nil {
return nil, nil, errors.Trace(err)
}

err = apihttp.SetRequestArgs(req, args)
if err != nil {
return nil, nil, errors.Annotate(err, "while setting request body")
}

httpclient := newHTTPClient(s)
resp, err := httpclient.Do(&req.Request)
resp, err := httpclient.Do(req)
if err != nil {
return nil, errors.Annotate(err, "while sending HTTP request")
return nil, nil, errors.Annotate(err, "while sending HTTP request")
}
return resp, nil
return req, resp, nil
}
18 changes: 0 additions & 18 deletions api/http/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,8 @@ import (
"net/http"
)

// Request is a wrapper around an HTTP request that has been
// prepared for use in API HTTP calls.
type Request struct {
http.Request
}

// HTTPClient is an API-specific HTTP client.
type HTTPClient interface {
// Do sends the HTTP request, returning the subsequent response.
Do(req *http.Request) (*http.Response, error)
}

// Client exposes direct HTTP request functionality for the juju state
// API. This is significant for upload and download of files, which the
// websockets-based RPC does not support.
type Client interface {
// NewHTTPRequest returns a new API-specific HTTP request. Callers
// should finish the request (setting headers, body) before sending.
NewHTTPRequest(method, path string) (*Request, error)
// SendHTTPRequest returns the HTTP response from the API server.
// The caller is then responsible for handling the response.
SendHTTPRequest(req *Request) (*http.Response, error)
}
22 changes: 20 additions & 2 deletions api/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
package http

import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"path"
Expand All @@ -12,12 +15,27 @@ import (
)

// NewRequest returns a new HTTP request suitable for the API.
func NewRequest(method string, baseURL *url.URL, pth, uuid, tag, pw string) (*Request, error) {
func NewRequest(method string, baseURL *url.URL, pth, uuid, tag, pw string) (*http.Request, error) {
baseURL.Path = path.Join("/environment", uuid, pth)

req, err := http.NewRequest(method, baseURL.String(), nil)
if err != nil {
return nil, errors.Annotate(err, "while building HTTP request")
}

req.SetBasicAuth(tag, pw)
return &Request{*req}, nil

return req, nil
}

// SetRequestArgs JSON-encodes the args and sets them as the request body.
func SetRequestArgs(req *http.Request, args interface{}) error {
data, err := json.Marshal(args)
if err != nil {
return errors.Annotate(err, "while serializing args")
}

req.Header.Set("Content-Type", "application/json")
req.Body = ioutil.NopCloser(bytes.NewBuffer(data))
return nil
}
4 changes: 2 additions & 2 deletions api/http/testing/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ package testing

import (
"encoding/base64"
"net/http"

gc "gopkg.in/check.v1"

apihttp "github.com/juju/juju/api/http"
"github.com/juju/juju/testing"
)

Expand All @@ -27,7 +27,7 @@ func (s *BaseSuite) SetUpTest(c *gc.C) {

// CheckRequest verifies that the HTTP request matches the args
// as an API request should.
func (s *BaseSuite) CheckRequest(c *gc.C, req *apihttp.Request, method, user, pw, host, pth string) {
func (s *BaseSuite) CheckRequest(c *gc.C, req *http.Request, method, user, pw, host, pth string) {
// Only check API-related request fields.

c.Check(req.Method, gc.Equals, method)
Expand Down
10 changes: 4 additions & 6 deletions api/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (s *httpSuite) SetUpTest(c *gc.C) {
)
}

func (s *httpSuite) checkRequest(c *gc.C, req *apihttp.Request, method, pth string) {
func (s *httpSuite) checkRequest(c *gc.C, req *http.Request, method, pth string) {
username := dummy.AdminUserTag().String()
password := jujutesting.AdminSecret
hostname := "localhost"
Expand All @@ -61,17 +61,15 @@ func (s *httpSuite) TestNewHTTPClientCorrectTransport(c *gc.C) {
func (s *httpSuite) TestNewHTTPClientValidatesCert(c *gc.C) {
req, err := s.APIState.NewHTTPRequest("GET", "somefacade")
httpClient := s.APIState.NewHTTPClient()
resp, err := httpClient.Do(&req.Request)
resp, err := httpClient.Do(req)
c.Assert(err, gc.IsNil)

c.Check(resp.StatusCode, gc.Equals, http.StatusNotFound)
}

func (s *httpSuite) TestSendHTTPRequestSuccess(c *gc.C) {
req, err := s.APIState.NewHTTPRequest("GET", "somefacade")
c.Assert(err, gc.IsNil)
resp, err := s.APIState.SendHTTPRequest(req)
req, resp, err := s.APIState.SendHTTPRequest("GET", "somefacade", nil)
c.Assert(err, gc.IsNil)

s.Fake.CheckCalled(c, &req.Request, resp)
s.Fake.CheckCalled(c, req, resp)
}

0 comments on commit dbe0360

Please sign in to comment.