buildAPIRequest validates flags and builds 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 *APIOptions)
| 127 | // When dryRun is true and a file is provided, file reading is skipped and |
| 128 | // FileUploadMeta is returned instead so the caller can render dry-run output. |
| 129 | func buildAPIRequest(opts *APIOptions) (client.RawApiRequest, *cmdutil.FileUploadMeta, error) { |
| 130 | stdin := opts.Factory.IOStreams.In |
| 131 | fileIO := opts.Factory.ResolveFileIO(opts.Ctx) |
| 132 | |
| 133 | // Validate --file mutual exclusions first. |
| 134 | if err := cmdutil.ValidateFileFlag(opts.File, opts.Params, opts.Data, opts.Output, opts.PageAll, opts.Method); err != nil { |
| 135 | return client.RawApiRequest{}, nil, err |
| 136 | } |
| 137 | |
| 138 | // stdin conflict: --params and --data cannot both read from stdin, regardless of --file. |
| 139 | if opts.Params == "-" && opts.Data == "-" { |
| 140 | return client.RawApiRequest{}, nil, errs.NewValidationError(errs.SubtypeInvalidArgument, |
| 141 | "--params and --data cannot both read from stdin (-)"). |
| 142 | WithHint("pass at most one flag as '-'; give the other inline JSON or @file"). |
| 143 | WithParams( |
| 144 | errs.InvalidParam{Name: "--params", Reason: "reads from stdin (-)"}, |
| 145 | errs.InvalidParam{Name: "--data", Reason: "reads from stdin (-)"}, |
| 146 | ) |
| 147 | } |
| 148 | |
| 149 | params, err := cmdutil.ParseJSONMap(opts.Params, "--params", stdin, fileIO) |
| 150 | if err != nil { |
| 151 | return client.RawApiRequest{}, nil, err |
| 152 | } |
| 153 | if opts.PageSize > 0 { |
| 154 | params["page_size"] = opts.PageSize |
| 155 | } |
| 156 | |
| 157 | request := client.RawApiRequest{ |
| 158 | Method: opts.Method, |
| 159 | URL: normalisePath(opts.Path), |
| 160 | Params: params, |
| 161 | As: opts.As, |
| 162 | } |
| 163 | |
| 164 | if opts.File != "" { |
| 165 | // File upload path: build formdata. |
| 166 | fieldName, filePath, isStdin := cmdutil.ParseFileFlag(opts.File, "file") |
| 167 | |
| 168 | // Parse --data as JSON map for form fields (not as body). |
| 169 | var dataFields any |
| 170 | if opts.Data != "" { |
| 171 | dataFields, err = cmdutil.ParseOptionalBody(opts.Method, opts.Data, stdin, fileIO) |
| 172 | if err != nil { |
| 173 | return client.RawApiRequest{}, nil, err |
| 174 | } |
| 175 | if _, ok := dataFields.(map[string]any); !ok { |
| 176 | return client.RawApiRequest{}, nil, errs.NewValidationError(errs.SubtypeInvalidArgument, |
| 177 | "--data must be a JSON object when used with --file"). |
| 178 | WithHint(`with --file, --data carries multipart form fields, e.g. --data '{"image_type":"message"}'`). |
| 179 | WithParam("--data") |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if opts.DryRun { |
| 184 | return request, &cmdutil.FileUploadMeta{ |
| 185 | FieldName: fieldName, FilePath: filePath, FormFields: dataFields, |
| 186 | }, nil |
no test coverage detected