当前位置: 首页>>代码示例>>Golang>>正文


Golang net.NewApiResponseWithError函数代码示例

本文整理汇总了Golang中cf/net.NewApiResponseWithError函数的典型用法代码示例。如果您正苦于以下问题:Golang NewApiResponseWithError函数的具体用法?Golang NewApiResponseWithError怎么用?Golang NewApiResponseWithError使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了NewApiResponseWithError函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Request

func (repo CloudControllerCurlRepository) Request(method, path, headerString, body string) (resHeaders, resBody string, apiResponse net.ApiResponse) {
	url := fmt.Sprintf("%s/%s", repo.config.Target, strings.TrimLeft(path, "/"))

	req, apiResponse := repo.gateway.NewRequest(method, url, repo.config.AccessToken, strings.NewReader(body))
	if apiResponse.IsNotSuccessful() {
		return
	}

	err := mergeHeaders(req.HttpReq.Header, headerString)
	if err != nil {
		apiResponse = net.NewApiResponseWithError("Error parsing headers", err)
		return
	}

	res, apiResponse := repo.gateway.PerformRequestForResponse(req)
	bytes, err := ioutil.ReadAll(res.Body)

	if err != nil {
		apiResponse = net.NewApiResponseWithError("Error reading response", err)
	}
	resBody = string(bytes)

	headerBytes, _ := httputil.DumpResponse(res, false)
	resHeaders = string(headerBytes)

	return
}
开发者ID:pmuellr,项目名称:cli,代码行数:27,代码来源:curl.go

示例2: uploadBits

func (repo CloudControllerApplicationBitsRepository) uploadBits(appGuid string, zipFile *os.File, presentResourcesJson []byte) (apiResponse net.ApiResponse) {
	url := fmt.Sprintf("%s/v2/apps/%s/bits", repo.config.ApiEndpoint(), appGuid)
	fileutils.TempFile("requests", func(requestFile *os.File, err error) {
		if err != nil {
			apiResponse = net.NewApiResponseWithError("Error creating tmp file: %s", err)
			return
		}

		boundary, err := repo.writeUploadBody(zipFile, requestFile, presentResourcesJson)
		if err != nil {
			apiResponse = net.NewApiResponseWithError("Error writing to tmp file: %s", err)
			return
		}

		var request *net.Request
		request, apiResponse = repo.gateway.NewRequest("PUT", url, repo.config.AccessToken(), requestFile)
		if apiResponse.IsNotSuccessful() {
			return
		}

		contentType := fmt.Sprintf("multipart/form-data; boundary=%s", boundary)
		request.HttpReq.Header.Set("Content-Type", contentType)

		response := &Resource{}
		_, apiResponse = repo.gateway.PerformPollingRequestForJSONResponse(request, response, 5*time.Minute)
		if apiResponse.IsNotSuccessful() {
			return
		}
	})

	return
}
开发者ID:rodney-vin,项目名称:cli,代码行数:32,代码来源:application_bits.go

示例3: UploadBuildpack

func (repo CloudControllerBuildpackBitsRepository) UploadBuildpack(buildpack models.Buildpack, buildpackLocation string) (apiResponse net.ApiResponse) {
	fileutils.TempFile("buildpack-upload", func(zipFileToUpload *os.File, err error) {
		if err != nil {
			apiResponse = net.NewApiResponseWithError("Couldn't create temp file for upload", err)
			return
		}

		var buildpackFileName string
		if isWebURL(buildpackLocation) {
			buildpackFileName = path.Base(buildpackLocation)
			downloadBuildpack(buildpackLocation, func(downloadFile *os.File, downloadErr error) {
				if downloadErr != nil {
					err = downloadErr
					return
				}

				err = normalizeBuildpackArchive(downloadFile, zipFileToUpload)
			})
		} else {
			buildpackFileName = filepath.Base(buildpackLocation)

			stats, err := os.Stat(buildpackLocation)
			if err != nil {
				apiResponse = net.NewApiResponseWithError("Error opening buildpack file", err)
				return
			}

			if stats.IsDir() {
				err = repo.zipper.Zip(buildpackLocation, zipFileToUpload)
			} else {
				specifiedFile, err := os.Open(buildpackLocation)
				if err != nil {
					apiResponse = net.NewApiResponseWithError("Couldn't open buildpack file", err)
					return
				}
				err = normalizeBuildpackArchive(specifiedFile, zipFileToUpload)
			}
		}

		if err != nil {
			apiResponse = net.NewApiResponseWithError("Couldn't write zip file", err)
			return
		}

		apiResponse = repo.uploadBits(buildpack, zipFileToUpload, buildpackFileName)
	})

	return
}
开发者ID:normalnorman,项目名称:cli,代码行数:49,代码来源:buildpack_bits.go

示例4: UploadApp

func (repo CloudControllerApplicationBitsRepository) UploadApp(appGuid string, appDir string, cb func(path string, zipSize, fileCount uint64)) (apiResponse net.ApiResponse) {
	fileutils.TempDir("apps", func(uploadDir string, err error) {
		if err != nil {
			apiResponse = net.NewApiResponseWithMessage(err.Error())
			return
		}

		var presentResourcesJson []byte
		repo.sourceDir(appDir, func(sourceDir string, sourceErr error) {
			if sourceErr != nil {
				err = sourceErr
				return
			}
			presentResourcesJson, err = repo.copyUploadableFiles(sourceDir, uploadDir)
		})

		if err != nil {
			apiResponse = net.NewApiResponseWithMessage("%s", err)
			return
		}

		fileutils.TempFile("uploads", func(zipFile *os.File, err error) {
			if err != nil {
				apiResponse = net.NewApiResponseWithMessage("%s", err.Error())
				return
			}

			err = repo.zipper.Zip(uploadDir, zipFile)
			if err != nil {
				apiResponse = net.NewApiResponseWithError("Error zipping application", err)
				return
			}

			stat, err := zipFile.Stat()
			if err != nil {
				apiResponse = net.NewApiResponseWithError("Error zipping application", err)
				return
			}
			cb(appDir, uint64(stat.Size()), cf.CountFiles(uploadDir))

			apiResponse = repo.uploadBits(appGuid, zipFile, presentResourcesJson)
			if apiResponse.IsNotSuccessful() {
				return
			}
		})
	})
	return
}
开发者ID:rodney-vin,项目名称:cli,代码行数:48,代码来源:application_bits.go

示例5: uploadBits

func (repo CloudControllerBuildpackBitsRepository) uploadBits(buildpack cf.Buildpack, zipFile *os.File) (apiResponse net.ApiResponse) {
	url := fmt.Sprintf("%s/v2/buildpacks/%s/bits", repo.config.Target, buildpack.Guid)

	fileutils.TempFile("requests", func(requestFile *os.File, err error) {
		if err != nil {
			apiResponse = net.NewApiResponseWithMessage(err.Error())
			return
		}

		boundary, err := repo.writeUploadBody(zipFile, requestFile)
		if err != nil {
			apiResponse = net.NewApiResponseWithError("Error creating upload", err)
			return
		}

		request, apiResponse := repo.gateway.NewRequest("PUT", url, repo.config.AccessToken, requestFile)
		contentType := fmt.Sprintf("multipart/form-data; boundary=%s", boundary)
		request.HttpReq.Header.Set("Content-Type", contentType)
		if apiResponse.IsNotSuccessful() {
			return
		}

		apiResponse = repo.gateway.PerformRequest(request)
	})

	return
}
开发者ID:pmuellr,项目名称:cli,代码行数:27,代码来源:buildpack_bits.go

示例6: getFilesToUpload

func (repo CloudControllerApplicationBitsRepository) getFilesToUpload(allAppFiles []models.AppFileFields) (appFilesToUpload []models.AppFileFields, presentResourcesJson []byte, apiResponse net.ApiResponse) {
	appFilesRequest := []AppFileResource{}
	for _, file := range allAppFiles {
		appFilesRequest = append(appFilesRequest, AppFileResource{
			Path: file.Path,
			Sha1: file.Sha1,
			Size: file.Size,
		})
	}

	allAppFilesJson, err := json.Marshal(appFilesRequest)
	if err != nil {
		apiResponse = net.NewApiResponseWithError("Failed to create json for resource_match request", err)
		return
	}

	path := fmt.Sprintf("%s/v2/resource_match", repo.config.ApiEndpoint())
	req, apiResponse := repo.gateway.NewRequest("PUT", path, repo.config.AccessToken(), bytes.NewReader(allAppFilesJson))
	if apiResponse.IsNotSuccessful() {
		return
	}

	presentResourcesJson, _, apiResponse = repo.gateway.PerformRequestForResponseBytes(req)

	fileResource := []AppFileResource{}
	err = json.Unmarshal(presentResourcesJson, &fileResource)

	if err != nil {
		apiResponse = net.NewApiResponseWithError("Failed to unmarshal json response from resource_match request", err)
		return
	}

	appFilesToUpload = make([]models.AppFileFields, len(allAppFiles))
	copy(appFilesToUpload, allAppFiles)
	for _, file := range fileResource {
		appFile := models.AppFileFields{
			Path: file.Path,
			Sha1: file.Sha1,
			Size: file.Size,
		}
		appFilesToUpload = repo.deleteAppFile(appFilesToUpload, appFile)
	}

	return
}
开发者ID:rodney-vin,项目名称:cli,代码行数:45,代码来源:application_bits.go

示例7: setOrganization

func (cmd Login) setOrganization(c *cli.Context, userChanged bool) (apiResponse net.ApiResponse) {
	orgName := c.String("o")

	if orgName == "" {
		// If the user is changing, clear out the org
		if userChanged {
			err := cmd.configRepo.SetOrganization(cf.OrganizationFields{})
			if err != nil {
				apiResponse = net.NewApiResponseWithError("%s", err)
				return
			}
		}

		// Reuse org in config
		if cmd.config.HasOrganization() && !userChanged {
			return
		}

		stopChan := make(chan bool)
		defer close(stopChan)

		orgsChan, statusChan := cmd.orgRepo.ListOrgs(stopChan)

		availableOrgs := []cf.Organization{}

		for orgs := range orgsChan {
			availableOrgs = append(availableOrgs, orgs...)
			if len(availableOrgs) > maxChoices {
				stopChan <- true
				break
			}
		}

		apiResponse = <-statusChan
		if apiResponse.IsNotSuccessful() {
			cmd.ui.Failed("Error finding avilable orgs\n%s", apiResponse.Message)
			return
		}

		// Target only org if possible
		if len(availableOrgs) == 1 {
			return cmd.targetOrganization(availableOrgs[0])
		}

		orgName = cmd.promptForOrgName(availableOrgs)
	}

	// Find org
	org, apiResponse := cmd.orgRepo.FindByName(orgName)
	if apiResponse.IsNotSuccessful() {
		cmd.ui.Failed("Error finding org %s\n%s", terminal.EntityNameColor(orgName), apiResponse.Message)
		return
	}

	return cmd.targetOrganization(org)
}
开发者ID:nsnt,项目名称:cli,代码行数:56,代码来源:login.go

示例8: setSpace

func (cmd Login) setSpace(c *cli.Context, userChanged bool) (apiResponse net.ApiResponse) {
	spaceName := c.String("s")

	if spaceName == "" {
		// If user is changing, clear the space
		if userChanged {
			err := cmd.configRepo.SetSpace(cf.SpaceFields{})
			if err != nil {
				apiResponse = net.NewApiResponseWithError("%s", err)
				return
			}
		}
		// Reuse space in config
		if cmd.config.HasSpace() && !userChanged {
			return
		}

		stopChan := make(chan bool)
		defer close(stopChan)

		spacesChan, statusChan := cmd.spaceRepo.ListSpaces(stopChan)

		var availableSpaces []cf.Space

		for spaces := range spacesChan {
			availableSpaces = append(availableSpaces, spaces...)
			if len(availableSpaces) > maxChoices {
				stopChan <- true
				break
			}
		}

		apiResponse = <-statusChan
		if apiResponse.IsNotSuccessful() {
			cmd.ui.Failed("Error finding avilable spaces\n%s", apiResponse.Message)
			return
		}

		// Target only space if possible
		if len(availableSpaces) == 1 {
			return cmd.targetSpace(availableSpaces[0])
		}

		spaceName = cmd.promptForSpaceName(availableSpaces)
	}

	// Find space
	space, apiResponse := cmd.spaceRepo.FindByName(spaceName)
	if apiResponse.IsNotSuccessful() {
		cmd.ui.Failed("Error finding space %s\n%s", terminal.EntityNameColor(spaceName), apiResponse.Message)
		return
	}

	return cmd.targetSpace(space)
}
开发者ID:nsnt,项目名称:cli,代码行数:55,代码来源:login.go

示例9: TestMapDomainMappingFails

func TestMapDomainMappingFails(t *testing.T) {
	reqFactory, domainRepo := getDomainMapperDeps()
	domainRepo.MapApiResponse = net.NewApiResponseWithError("Did not work %s", errors.New("bummer"))

	ui := callDomainMapper(t, true, []string{"my-space", "foo.com"}, reqFactory, domainRepo)

	assert.Equal(t, len(ui.Outputs), 3)
	testassert.SliceContains(t, ui.Outputs, testassert.Lines{
		{"Mapping domain", "foo.com", "my-space"},
		{"FAILED"},
		{"Did not work", "bummer"},
	})
}
开发者ID:pmuellr,项目名称:cli,代码行数:13,代码来源:domain_mapper_test.go

示例10: Update

func (repo CCUserProvidedServiceInstanceRepository) Update(serviceInstance cf.ServiceInstance) (apiResponse net.ApiResponse) {
	path := fmt.Sprintf("%s/v2/user_provided_service_instances/%s", repo.config.Target, serviceInstance.Guid)

	type RequestBody struct {
		Credentials    map[string]string `json:"credentials,omitempty"`
		SysLogDrainUrl string            `json:"syslog_drain_url,omitempty"`
	}

	reqBody := RequestBody{serviceInstance.Params, serviceInstance.SysLogDrainUrl}
	jsonBytes, err := json.Marshal(reqBody)
	if err != nil {
		apiResponse = net.NewApiResponseWithError("Error parsing response", err)
		return
	}

	return repo.gateway.UpdateResource(path, repo.config.AccessToken, bytes.NewReader(jsonBytes))
}
开发者ID:jalateras,项目名称:cli,代码行数:17,代码来源:user_provided_service_instances.go

示例11: Update

func (repo CloudControllerApplicationRepository) Update(appGuid string, params models.AppParams) (updatedApp models.Application, apiResponse net.ApiResponse) {
	data, err := repo.formatAppJSON(params)
	if err != nil {
		apiResponse = net.NewApiResponseWithError("Failed to marshal JSON", err)
		return
	}

	path := fmt.Sprintf("%s/v2/apps/%s?inline-relations-depth=1", repo.config.ApiEndpoint(), appGuid)
	resource := new(ApplicationResource)
	apiResponse = repo.gateway.UpdateResourceForResponse(path, repo.config.AccessToken(), strings.NewReader(data), resource)
	if apiResponse.IsNotSuccessful() {
		return
	}

	updatedApp = resource.ToModel()
	return
}
开发者ID:normalnorman,项目名称:cli,代码行数:17,代码来源:applications.go

示例12: UploadBuildpack

func (repo CloudControllerBuildpackBitsRepository) UploadBuildpack(buildpack cf.Buildpack, dir string) (apiResponse net.ApiResponse) {
	fileutils.TempFile("buildpack", func(zipFile *os.File, err error) {
		if err != nil {
			apiResponse = net.NewApiResponseWithMessage(err.Error())
			return
		}
		err = repo.zipper.Zip(dir, zipFile)
		if err != nil {
			apiResponse = net.NewApiResponseWithError("Invalid buildpack", err)
			return
		}
		apiResponse = repo.uploadBits(buildpack, zipFile)
		if apiResponse.IsNotSuccessful() {
			return
		}
	})
	return
}
开发者ID:pmuellr,项目名称:cli,代码行数:18,代码来源:buildpack_bits.go

示例13: SetEnv

func (repo CloudControllerApplicationRepository) SetEnv(app cf.Application, envVars map[string]string) (apiResponse net.ApiResponse) {
	path := fmt.Sprintf("%s/v2/apps/%s", repo.config.Target, app.Guid)

	type setEnvReqBody struct {
		EnvJson map[string]string `json:"environment_json"`
	}

	body := setEnvReqBody{EnvJson: envVars}

	jsonBytes, err := json.Marshal(body)
	if err != nil {
		apiResponse = net.NewApiResponseWithError("Error creating json", err)
		return
	}

	apiResponse = repo.gateway.UpdateResource(path, repo.config.AccessToken, bytes.NewReader(jsonBytes))
	return
}
开发者ID:jalateras,项目名称:cli,代码行数:18,代码来源:applications.go

示例14: Create

func (repo CloudControllerBuildpackRepository) Create(name string, position *int, enabled *bool) (createdBuildpack cf.Buildpack, apiResponse net.ApiResponse) {
	path := repo.config.Target + buildpacks_path
	entity := BuildpackEntity{Name: name, Position: position, Enabled: enabled}
	body, err := json.Marshal(entity)
	if err != nil {
		apiResponse = net.NewApiResponseWithError("Could not serialize information", err)
		return
	}

	resource := new(BuildpackResource)
	apiResponse = repo.gateway.CreateResourceForResponse(path, repo.config.AccessToken, bytes.NewReader(body), resource)
	if apiResponse.IsNotSuccessful() {
		return
	}

	createdBuildpack = unmarshallBuildpack(*resource)
	return
}
开发者ID:nsnt,项目名称:cli,代码行数:18,代码来源:buildpacks.go

示例15: Update

func (repo CloudControllerBuildpackRepository) Update(buildpack cf.Buildpack) (updatedBuildpack cf.Buildpack, apiResponse net.ApiResponse) {
	path := fmt.Sprintf("%s%s/%s", repo.config.Target, buildpacks_path, buildpack.Guid)

	entity := BuildpackEntity{buildpack.Name, buildpack.Position, buildpack.Enabled, "", ""}
	body, err := json.Marshal(entity)
	if err != nil {
		apiResponse = net.NewApiResponseWithError("Could not serialize updates.", err)
		return
	}

	resource := new(BuildpackResource)
	apiResponse = repo.gateway.UpdateResourceForResponse(path, repo.config.AccessToken, bytes.NewReader(body), resource)
	if apiResponse.IsNotSuccessful() {
		return
	}

	updatedBuildpack = unmarshallBuildpack(*resource)
	return
}
开发者ID:nsnt,项目名称:cli,代码行数:19,代码来源:buildpacks.go


注:本文中的cf/net.NewApiResponseWithError函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。