Skip to content

Commit 7f604b6

Browse files
authored
fix: file upload response fields (#7)
* Fix upload file response decoding * Remove unused
1 parent ebf0b19 commit 7f604b6

File tree

1 file changed

+18
-32
lines changed

1 file changed

+18
-32
lines changed

Sources/Slackito/Slackito.swift

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ actor Slackito {
5353

5454
let (data, response) = try await session.data(for: request)
5555
guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
56-
logger.error("[Slack API] request \(request) failed. Response: \(response)")
56+
logger.error("request \(request) failed. Response: \(response)")
5757
throw URLError(.badServerResponse)
5858
}
5959

@@ -82,8 +82,7 @@ actor Slackito {
8282
data: Data,
8383
filename: String,
8484
fileType: String,
85-
channels: [String] = [],
86-
initialComment: String? = nil
85+
channels: [String] = []
8786
) async throws -> FileUploadResponse {
8887
guard let baseUrl else { throw ClientError.invalidSlackURL }
8988

@@ -92,38 +91,26 @@ actor Slackito {
9291
var request = URLRequest(url: url)
9392
request.setValue("Bearer \(appToken)", forHTTPHeaderField: "Authorization")
9493
request.httpMethod = "POST"
95-
96-
// Create multipart form data
94+
9795
let boundary = "Boundary-\(UUID().uuidString)"
9896
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
9997

10098
var body = Data()
101-
102-
// Add filename
99+
103100
body.append("--\(boundary)\r\n".data(using: .utf8)!)
104101
body.append("Content-Disposition: form-data; name=\"filename\"\r\n\r\n".data(using: .utf8)!)
105102
body.append("\(filename)\r\n".data(using: .utf8)!)
106-
107-
// Add file type
103+
108104
body.append("--\(boundary)\r\n".data(using: .utf8)!)
109105
body.append("Content-Disposition: form-data; name=\"filetype\"\r\n\r\n".data(using: .utf8)!)
110106
body.append("\(fileType)\r\n".data(using: .utf8)!)
111-
112-
// Add channels if provided
107+
113108
if !channels.isEmpty {
114109
body.append("--\(boundary)\r\n".data(using: .utf8)!)
115110
body.append("Content-Disposition: form-data; name=\"channels\"\r\n\r\n".data(using: .utf8)!)
116111
body.append("\(channels.joined(separator: ","))\r\n".data(using: .utf8)!)
117112
}
118-
119-
// Add initial comment if provided
120-
if let initialComment = initialComment {
121-
body.append("--\(boundary)\r\n".data(using: .utf8)!)
122-
body.append("Content-Disposition: form-data; name=\"initial_comment\"\r\n\r\n".data(using: .utf8)!)
123-
body.append("\(initialComment)\r\n".data(using: .utf8)!)
124-
}
125-
126-
// Add file data
113+
127114
body.append("--\(boundary)\r\n".data(using: .utf8)!)
128115
body.append("Content-Disposition: form-data; name=\"file\"; filename=\"\(filename)\"\r\n".data(using: .utf8)!)
129116
body.append("Content-Type: application/octet-stream\r\n\r\n".data(using: .utf8)!)
@@ -133,17 +120,20 @@ actor Slackito {
133120
request.httpBody = body
134121

135122
do {
136-
logger.debug("[Slack API] uploading file: \(filename)")
137-
123+
logger.debug("Uploading file: \(filename)")
124+
138125
let (data, response) = try await session.data(for: request)
139126
guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
140-
logger.error("[Slack API] file upload failed. Response: \(response)")
127+
logger.error("File upload failed. Response: \(response)")
141128
throw URLError(.badServerResponse)
142129
}
143-
144-
return try JSONDecoder().decode(FileUploadResponse.self, from: data)
130+
131+
let decoder = JSONDecoder()
132+
decoder.keyDecodingStrategy = .convertFromSnakeCase
133+
134+
return try decoder.decode(FileUploadResponse.self, from: data)
145135
} catch {
146-
logger.error("[Slack API] file upload failed: \(error.localizedDescription)")
136+
logger.error("File upload failed: \(error.localizedDescription)")
147137
throw error
148138
}
149139
}
@@ -172,17 +162,13 @@ extension Slackito {
172162
}
173163

174164
struct FileInfo: Decodable {
175-
/// File ID
176165
let id: String
177-
/// File name
178166
let name: String
179-
/// File URL
180167
let urlPrivate: String
181-
/// Public URL (if available)
182168
let urlPrivateDownload: String?
183-
/// File type
169+
let permalink: String
170+
let permalinkPublic: String?
184171
let filetype: String?
185-
/// File size
186172
let size: Int?
187173
}
188174

0 commit comments

Comments
 (0)