From 12d57b297f88dd1e4846ede7be65221a196be432 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mads=20B=C3=B8geskov?= Date: Fri, 8 Mar 2024 10:55:33 +0100 Subject: [PATCH] :bug: Fix headers (#143) --- .../Models/APIRequest+Swiftable.swift | 6 +++--- .../Generator/Generator.swift | 21 +++++++++++++------ .../Generator/Models/GlobalHeadersModel.swift | 2 +- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Sources/SwaggerSwiftCore/API Request Factory/Models/APIRequest+Swiftable.swift b/Sources/SwaggerSwiftCore/API Request Factory/Models/APIRequest+Swiftable.swift index fd4babe..5f5d102 100644 --- a/Sources/SwaggerSwiftCore/API Request Factory/Models/APIRequest+Swiftable.swift +++ b/Sources/SwaggerSwiftCore/API Request Factory/Models/APIRequest+Swiftable.swift @@ -55,11 +55,11 @@ extension APIRequest { .sorted(by: { $0.swiftyName < $1.swiftyName }) .map { if $0.isRequired { - return "request.addValue(\(headersName).\($0.swiftyName), forHTTPHeaderField: \"\($0.fullHeaderName)\")" + return "request.setValue(\(headersName).\($0.swiftyName), forHTTPHeaderField: \"\($0.fullHeaderName)\")" } else { return """ if let \(($0.swiftyName)) = \(headersName).\($0.swiftyName) { - request.addValue(\($0.swiftyName), forHTTPHeaderField: \"\($0.fullHeaderName)\") + request.setValue(\($0.swiftyName), forHTTPHeaderField: \"\($0.fullHeaderName)\") } """ } @@ -140,7 +140,7 @@ if let \(($0.swiftyName)) = \(headersName).\($0.swiftyName) { switch consumes { case .json: urlSessionMethodName = "data(for: request)" - headerStatements.append("request.addValue(\"application/json\", forHTTPHeaderField: \"Content-Type\")") + headerStatements.append("request.setValue(\"application/json\", forHTTPHeaderField: \"Content-Type\")") case .formUrlEncoded: fallthrough case .multiPartFormData: diff --git a/Sources/SwaggerSwiftCore/Generator/Generator.swift b/Sources/SwaggerSwiftCore/Generator/Generator.swift index f2822c0..1bbda34 100644 --- a/Sources/SwaggerSwiftCore/Generator/Generator.swift +++ b/Sources/SwaggerSwiftCore/Generator/Generator.swift @@ -188,19 +188,28 @@ public struct Generator { private func download(githubToken: String, organisation: String, serviceName: String, branch: String, swaggerPath: String, urlSession: URLSession) async throws -> Data { let url = URL(string: "https://raw.githubusercontent.com/\(organisation)/\(serviceName)/\(branch)/\(swaggerPath)")! var request = URLRequest(url: url) - request.addValue("token \(githubToken)", forHTTPHeaderField: "Authorization") - request.addValue("application/vnd.github.v3.raw", forHTTPHeaderField: "Accept") + request.setValue("token \(githubToken)", forHTTPHeaderField: "Authorization") + request.setValue("application/vnd.github.v3.raw", forHTTPHeaderField: "Accept") log("Downloading Swagger at: \(url.absoluteString)") let (data, response) = try await fetchSwagger(request) if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode != 200 { if branch != "master" || branch != "main" { log(" ⚠️ \(serviceName): Failed to download with custom branch ´\(branch)´ - Trying master instead.", error: true) - return try await download(githubToken: githubToken, organisation: organisation, serviceName: serviceName, branch: "master", swaggerPath: swaggerPath, urlSession: urlSession) + return try await download( + githubToken: githubToken, + organisation: organisation, + serviceName: serviceName, + branch: "master", + swaggerPath: swaggerPath, + urlSession: urlSession + ) } else { - throw FetchSwaggerError.requestFailed(serviceName: serviceName, - branch: branch, - statusCode: httpResponse.statusCode) + throw FetchSwaggerError.requestFailed( + serviceName: serviceName, + branch: branch, + statusCode: httpResponse.statusCode + ) } } diff --git a/Sources/SwaggerSwiftCore/Generator/Models/GlobalHeadersModel.swift b/Sources/SwaggerSwiftCore/Generator/Models/GlobalHeadersModel.swift index 2059471..d47fbf4 100644 --- a/Sources/SwaggerSwiftCore/Generator/Models/GlobalHeadersModel.swift +++ b/Sources/SwaggerSwiftCore/Generator/Models/GlobalHeadersModel.swift @@ -70,7 +70,7 @@ struct GlobalHeadersModel { .sorted(by: { $0.swiftyName < $1.swiftyName }) .map { """ if let \($0.swiftyName) = \($0.swiftyName) { - request.addValue(\($0.swiftyName), forHTTPHeaderField: \"\($0.fullHeaderName)\") + request.setValue(\($0.swiftyName), forHTTPHeaderField: \"\($0.fullHeaderName)\") } """ } .joined(separator: "\n\n")