buildServiceRequest parses flags, builds the URL with path/query params, and returns a RawApiRequest. When dryRun is true and a file is provided, file reading is skipped and FileUploadMeta is returned instead so the caller can render dry-run output.
(opts *ServiceMethodOptions)
| 541 | // When dryRun is true and a file is provided, file reading is skipped and |
| 542 | // FileUploadMeta is returned instead so the caller can render dry-run output. |
| 543 | func buildServiceRequest(opts *ServiceMethodOptions) (client.RawApiRequest, *cmdutil.FileUploadMeta, error) { |
| 544 | method := opts.Method |
| 545 | httpMethod := method.HTTPMethod |
| 546 | |
| 547 | // stdin is an io.Reader consumed at most once. Only one of --params/--data |
| 548 | // may use "-" (stdin); the conflict check below prevents silent data loss. |
| 549 | stdin := opts.Factory.IOStreams.In |
| 550 | fileIO := opts.Factory.ResolveFileIO(opts.Ctx) |
| 551 | |
| 552 | // Validate --file mutual exclusions. |
| 553 | if err := cmdutil.ValidateFileFlag(opts.File, opts.Params, opts.Data, opts.Output, opts.PageAll, httpMethod); err != nil { |
| 554 | return client.RawApiRequest{}, nil, err |
| 555 | } |
| 556 | if opts.Params == "-" && opts.Data == "-" { |
| 557 | return client.RawApiRequest{}, nil, errs.NewValidationError(errs.SubtypeInvalidArgument, "--params and --data cannot both read from stdin (-)").WithParam("--params") |
| 558 | } |
| 559 | params, err := cmdutil.ParseJSONMap(opts.Params, "--params", stdin, fileIO) |
| 560 | if err != nil { |
| 561 | return client.RawApiRequest{}, nil, err |
| 562 | } |
| 563 | opts.binder.overlay(opts.Cmd, params) |
| 564 | |
| 565 | url := opts.ServicePath + "/" + method.Path |
| 566 | |
| 567 | specs := method.Params() |
| 568 | for _, s := range specs { |
| 569 | if s.Location != "path" { |
| 570 | continue |
| 571 | } |
| 572 | val, ok := params[s.Name] |
| 573 | if !ok || unusableParamValue(val) { |
| 574 | return client.RawApiRequest{}, nil, errs.NewValidationError(errs.SubtypeInvalidArgument, |
| 575 | "missing required path parameter: %s", s.Name). |
| 576 | WithHint("%s", missingParamHint(opts, s)). |
| 577 | WithParam(s.Name) |
| 578 | } |
| 579 | valStr := fmt.Sprintf("%v", val) |
| 580 | if err := validate.ResourceName(valStr, s.Name); err != nil { |
| 581 | return client.RawApiRequest{}, nil, errs.NewValidationError(errs.SubtypeInvalidArgument, "%s", err).WithParam(s.Name).WithCause(err) |
| 582 | } |
| 583 | url = strings.Replace(url, "{"+s.Name+"}", validate.EncodePathSegment(valStr), 1) |
| 584 | delete(params, s.Name) |
| 585 | } |
| 586 | |
| 587 | queryParams := map[string]interface{}{} |
| 588 | for _, s := range specs { |
| 589 | if s.Location != "query" { |
| 590 | continue |
| 591 | } |
| 592 | value, exists := params[s.Name] |
| 593 | isPaginationParam := opts.PageAll && (s.Name == "page_token" || s.Name == "page_size") |
| 594 | if s.Required && !isPaginationParam && (!exists || unusableParamValue(value)) { |
| 595 | return client.RawApiRequest{}, nil, errs.NewValidationError(errs.SubtypeInvalidArgument, |
| 596 | "missing required query parameter: %s", s.Name). |
| 597 | WithHint("%s", missingParamHint(opts, s)). |
| 598 | WithParam(s.Name) |
| 599 | } |
| 600 | if exists && !unusableParamValue(value) { |
no test coverage detected