Example #1
0
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
}
Example #2
0
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
}
Example #6
0
	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
Example #7
0
	"github.com/pivotal-cf-experimental/rainmaker/internal/network"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("RequestBodyEncoder", func() {
	Describe("JSONRequestBody", func() {
		Describe("Encode", func() {
			It("returns a JSON encoded representation of the given object with proper content type", func() {
				var object struct {
					Hello string `json:"hello"`
				}
				object.Hello = "goodbye"

				body, contentType, err := network.NewJSONRequestBody(object).Encode()
				Expect(err).NotTo(HaveOccurred())
				Expect(ioutil.ReadAll(body)).To(MatchJSON(`{
					"hello": "goodbye"
				}`))
				Expect(contentType).To(Equal("application/json"))
			})

			It("returns an error when the JSON cannot be encoded", func() {
				_, _, err := network.NewJSONRequestBody(func() {}).Encode()
				Expect(err).To(HaveOccurred())
			})
		})
	})

	Describe("FormRequestBody", func() {