Swift OpenAPPI Post 请求上传文件

问题描述 投票:0回答:1

我正在尝试弄清楚如何快速使用新的 OpenAPI 生成器。

我有这个有效的架构或 yaml 文件。

openapi: "3.0.3"
info:
  title: BcNotioUpload
  version: 1.0.0
servers:
  - url: https://prod.server.co
    description: "prod server"
  - url: https://stagin.allocations.gbct.io
    description: "Staging server"
    
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT

security:
  - bearerAuth: [APIKey]
  
paths:
  /telemetry/upload:
    post:
      operationId: uploadRide
      summary: Send CSV file to the servers.
      description: Bla bla bla bla bla
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                fileName:
                  type: string
                  format: binary
      responses:
        '200':
            description: callback successfully processed

在我的 swift 代码的实现中,我基于 Get 示例使用了它。我不确定将文件名放在哪里。

主体正在接受数据,我需要发送一个 CSV 文件,但没有名称。

{编辑} 另外,我不确定是否需要文件二进制文件的附加属性,或者

fileName:
   type: string
   format: binary

保管好两把钥匙,或者我需要将其更改为

fileName:
  type: string
  format: binary
file:
  type: object
  format: binary

在邮差里我的身体会是这样

更新:

if let fileData = data {
        let transport: ClientTransport = URLSessionTransport()
        let client = Client(serverURL: try! Servers.server1(), transport: transport)
        Task{
            let resp = try? await client.uploadRide(.init(body: .binary(fileData)))
            switch(resp){
            case .ok(_):
                // Switch over the response content type.
                print("Upload Ok")
                self.delegate?.onExportEnd(error:nil)
            case .undocumented(statusCode: let statusCode, _):
                // Handle HTTP response status codes not documented in the OpenAPI document.
                print("Upload Error")
                showAlert(title: "Danger Danger Will Robinson!!", msg: "Something did not work")
                errorHandler("Upload Failled")
                print("🥺 undocumented response: \(statusCode)")
            case .none:
                print("Upload none ?")
                //errorHandler("Upload ???")
            }
        }

对于数据部分,我需要将

.binary(fileData)
更改为
.binary(Data(from: ["file.name":"theNameOf TheFile", "file":fileData)
之类的吗?或者我是否必须将文件名放在 uploadRide(.init(...)) 中的其他参数中?


我没有任何指南的例子或任何关于如何使用 uploadFile() 的线索 有谁知道我如何使用客户端发送我的文件,或者是否有任何一个作为指向更深入的 swift 文档的链接,然后是苹果给我们的小 get Cat 示例??

ios swift xcode post openapi-generator
1个回答
0
投票

您现在可以这样做:

let response = try? await client.uploadRide(body: .multipartForm([
      .file(.init(payload: .init(body: .init(fileData)))),
      .name(.init(payload: .init(body: .init(fileName))))
]))

我假设:

  • fileName: String
    带钥匙
    name
  • fileData: Foundation.Data
    带钥匙
    file
© www.soinside.com 2019 - 2024. All rights reserved.