Skip to content

Commit

Permalink
🐛 Fix headers (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
MadsBogeskov authored Mar 8, 2024
1 parent 55fabe0 commit 12d57b2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)\")
}
"""
}
Expand Down Expand Up @@ -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:
Expand Down
21 changes: 15 additions & 6 deletions Sources/SwaggerSwiftCore/Generator/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 12d57b2

Please sign in to comment.