func (p *Page) Fetch(token string) error { u := url.URL{ Path: p.plan.Path, RawQuery: p.plan.Query.Encode(), } resp, err := newNetworkClient(p.config).MakeRequest(network.Request{ Method: "GET", Path: u.String(), Authorization: network.NewTokenAuthorization(token), AcceptableStatusCodes: []int{http.StatusOK}, }) if err != nil { return err } var response documents.PageResponse err = json.Unmarshal(resp.Body, &response) if err != nil { panic(err) } updatedPage := newPageFromResponse(p.config, p.plan, response) p.TotalResults = updatedPage.TotalResults p.TotalPages = updatedPage.TotalPages p.NextURL = updatedPage.NextURL p.PrevURL = updatedPage.PrevURL p.Resources = updatedPage.Resources return nil }
func (list *UsersList) Fetch(token string) error { u := url.URL{ Path: list.plan.Path, RawQuery: list.plan.Query.Encode(), } resp, err := newNetworkClient(list.config).MakeRequest(network.Request{ Method: "GET", Path: u.String(), Authorization: network.NewTokenAuthorization(token), AcceptableStatusCodes: []int{http.StatusOK}, }) if err != nil { return err } var response documents.UsersListResponse err = json.Unmarshal(resp.Body, &response) if err != nil { panic(err) } updatedList := newUsersListFromResponse(list.config, list.plan, response) list.TotalResults = updatedList.TotalResults list.TotalPages = updatedList.TotalPages list.NextURL = updatedList.NextURL list.PrevURL = updatedList.PrevURL list.Users = updatedList.Users return nil }
func (list UsersList) Associate(userGUID, token string) error { _, err := newNetworkClient(list.config).MakeRequest(network.Request{ Method: "PUT", Path: path.Join(list.plan.Path, userGUID), Authorization: network.NewTokenAuthorization(token), AcceptableStatusCodes: []int{http.StatusCreated}, }) return err }
func (service SpacesService) Delete(guid, token string) error { _, err := newNetworkClient(service.config).MakeRequest(network.Request{ Method: "DELETE", Path: fmt.Sprintf("/v2/spaces/%s", guid), Authorization: network.NewTokenAuthorization(token), AcceptableStatusCodes: []int{http.StatusNoContent}, }) if err != nil { return translateError(err) } return nil }
func (service *ServiceInstancesService) Get(guid, token string) (ServiceInstance, error) { resp, err := newNetworkClient(service.config).MakeRequest(network.Request{ Method: "GET", Path: fmt.Sprintf("/v2/service_instances/%s", guid), Authorization: network.NewTokenAuthorization(token), AcceptableStatusCodes: []int{http.StatusOK}, }) if err != nil { return ServiceInstance{}, err } var response documents.ServiceInstanceResponse err = json.Unmarshal(resp.Body, &response) if err != nil { panic(err) } return newServiceInstanceFromResponse(service.config, response), nil }
func (service UsersService) Get(guid, token string) (User, error) { resp, err := newNetworkClient(service.config).MakeRequest(network.Request{ Method: "GET", Path: "/v2/users/" + guid, Authorization: network.NewTokenAuthorization(token), AcceptableStatusCodes: []int{http.StatusOK}, }) if err != nil { return User{}, err } var response documents.UserResponse err = json.Unmarshal(resp.Body, &response) if err != nil { return User{}, translateError(err) } return newUserFromResponse(service.config, response), nil }
func (service ApplicationsService) Get(guid, token string) (Application, error) { resp, err := newNetworkClient(service.config).MakeRequest(network.Request{ Method: "GET", Path: fmt.Sprintf("/v2/apps/%s", guid), Authorization: network.NewTokenAuthorization(token), AcceptableStatusCodes: []int{http.StatusOK}, }) if err != nil { return Application{}, translateError(err) } var response documents.ApplicationResponse err = json.Unmarshal(resp.Body, &response) if err != nil { return Application{}, translateError(err) } return newApplicationFromResponse(service.config, response), nil }
func (list UsersList) Create(user User, token string) (User, error) { var document documents.UserResponse resp, err := newNetworkClient(list.config).MakeRequest(network.Request{ Method: "POST", Path: list.plan.Path, Authorization: network.NewTokenAuthorization(token), Body: network.NewJSONRequestBody(user), AcceptableStatusCodes: []int{http.StatusCreated}, }) if err != nil { return User{}, err } err = json.Unmarshal(resp.Body, &document) if err != nil { panic(err) } return newUserFromResponse(list.config, document), nil }
func (service UsersService) Create(guid, token string) (User, error) { resp, err := newNetworkClient(service.config).MakeRequest(network.Request{ Method: "POST", Path: "/v2/users", Body: network.NewJSONRequestBody(documents.CreateUserRequest{ GUID: guid, }), Authorization: network.NewTokenAuthorization(token), AcceptableStatusCodes: []int{http.StatusCreated}, }) if err != nil { return User{}, err } var document documents.UserResponse err = json.Unmarshal(resp.Body, &document) if err != nil { panic(err) } return newUserFromResponse(service.config, document), nil }
func (service OrganizationsService) Create(name string, token string) (Organization, error) { resp, err := newNetworkClient(service.config).MakeRequest(network.Request{ Method: "POST", Path: "/v2/organizations", Body: network.NewJSONRequestBody(documents.CreateOrganizationRequest{ Name: name, }), Authorization: network.NewTokenAuthorization(token), AcceptableStatusCodes: []int{http.StatusCreated}, }) if err != nil { return Organization{}, err } var response documents.OrganizationResponse err = json.Unmarshal(resp.Body, &response) if err != nil { panic(err) } return newOrganizationFromResponse(service.config, response), nil }
func (service ApplicationsService) Create(application Application, token string) (Application, error) { resp, err := newNetworkClient(service.config).MakeRequest(network.Request{ Method: "POST", Path: "/v2/apps", Body: network.NewJSONRequestBody(documents.CreateApplicationRequest{ Name: application.Name, SpaceGUID: application.SpaceGUID, Diego: application.Diego, }), Authorization: network.NewTokenAuthorization(token), AcceptableStatusCodes: []int{http.StatusCreated}, }) if err != nil { return Application{}, translateError(err) } var response documents.ApplicationResponse err = json.Unmarshal(resp.Body, &response) if err != nil { panic(err) } return newApplicationFromResponse(service.config, response), nil }
func (service *ServiceInstancesService) Create(name, planGUID, spaceGUID, token string) (ServiceInstance, error) { resp, err := newNetworkClient(service.config).MakeRequest(network.Request{ Method: "POST", Path: "/v2/service_instances", Body: network.NewJSONRequestBody(documents.CreateServiceInstanceRequest{ Name: name, PlanGUID: planGUID, SpaceGUID: spaceGUID, }), Authorization: network.NewTokenAuthorization(token), AcceptableStatusCodes: []int{http.StatusCreated}, }) if err != nil { return ServiceInstance{}, err } var response documents.ServiceInstanceResponse err = json.Unmarshal(resp.Body, &response) if err != nil { panic(err) } return newServiceInstanceFromResponse(service.config, response), nil }
}) AfterEach(func() { fakeServer.Close() }) Describe("makeRequest", func() { It("can make requests", func() { jsonBody := map[string]interface{}{ "hello": "goodbye", } _, err := client.MakeRequest(network.Request{ Method: "GET", Path: "/path", Authorization: network.NewTokenAuthorization(token), Body: network.NewJSONRequestBody(jsonBody), AcceptableStatusCodes: []int{http.StatusOK}, }) Expect(err).NotTo(HaveOccurred()) Expect(receivedRequest.Body).To(MatchJSON(`{"hello": "goodbye"}`)) Expect(receivedRequest.Header).To(HaveKeyWithValue("Accept", []string{"application/json"})) Expect(receivedRequest.Header).To(HaveKeyWithValue("Accept-Encoding", []string{"gzip"})) Expect(receivedRequest.Header).To(HaveKeyWithValue("Authorization", []string{"Bearer TOKEN"})) Expect(receivedRequest.Header).To(HaveKeyWithValue("Content-Length", []string{"19"})) Expect(receivedRequest.Header).To(HaveKeyWithValue("Content-Type", []string{"application/json"})) }) It("can make more requests than the total allowed number of open files", func() { var fdCount int64
package network_test import ( "github.com/pivotal-cf-experimental/rainmaker/internal/network" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("RequestAuthorization", func() { Describe("TokenAuthorization", func() { It("returns a bearer token given a token value", func() { auth := network.NewTokenAuthorization("TOKEN") Expect(auth.Authorization()).To(Equal("Bearer TOKEN")) }) }) Describe("BasicAuthorization", func() { It("returns a basic auth header given a username and password", func() { auth := network.NewBasicAuthorization("username", "password") Expect(auth.Authorization()).To(Equal("Basic dXNlcm5hbWU6cGFzc3dvcmQ=")) }) }) })