當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。