Skip to content

Commit

Permalink
feat: allow both DELETE and GET as deletion request types, spec amended
Browse files Browse the repository at this point in the history
  • Loading branch information
castdrian committed Jan 12, 2024
1 parent 056c83d commit afeb2b9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ By default, ishare supports and opens `.iscu` files for configuration. They are
📝 Specification (2.0.0 and newer)
</summary>

The custom uploader specification since version 2.0.0 has the following structure:
The custom uploader specification since version 2.0.0+ has the following structure:

```json
{
Expand All @@ -121,7 +121,8 @@ By default, ishare supports and opens `.iscu` files for configuration. They are
"fileFormName": "file", // optional
"requestBodyType": "multipartFormData", // optional, can be "multipartFormData" or "binary"
"responseURL": "https://uploader.com/{{jsonproperty}}",
"deletionURL": "https://uploader.com/{{jsonproperty}}" // optional
"deletionURL": "https://uploader.com/{{jsonproperty}}", // optional
"deleteRequestType": "DELETE" // optional, can be "DELETE" or "GET"
}
```

Expand Down
4 changes: 3 additions & 1 deletion examples/chibisafe.iscu
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
"x-api-key": "API_KEY"
},
"fileFormName": "file[]",
"responseURL": "{{url}}"
"responseURL": "{{url}}",
"deletionURL": "{{deleteUrl}}",
"deleteRequestType": "DELETE"
}
27 changes: 21 additions & 6 deletions ishare/Http/Custom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,29 @@ func performDeletionRequest(deletionUrl: String, completion: @escaping (Result<S
return
}

AF.request(url, method: .get).response { response in
switch response.result {
case .success:
completion(.success("Deleted file successfully"))
case .failure(let error):
completion(.failure(error))
@Default(.activeCustomUploader) var activeCustomUploader
var uploader = CustomUploader.allCases.first(where: { $0.id == activeCustomUploader })
var headers = HTTPHeaders(uploader?.headers ?? [:])

func sendRequest(with method: HTTPMethod) {
AF.request(url, method: method, headers: headers).response { response in
switch response.result {
case .success:
completion(.success("Deleted file successfully"))
case .failure(let error):
completion(.failure(error))
}
}
}

switch uploader?.deleteRequestType {
case .GET:
sendRequest(with: .get)
case .DELETE:
sendRequest(with: .delete)
case nil:
sendRequest(with: .get)
}
}

func handleResponse(data: Data, specification: CustomUploader, callback: ((Error?, URL?) -> Void)?, completion: @escaping () -> Void) {
Expand Down
11 changes: 10 additions & 1 deletion ishare/Util/CustomUploader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ enum RequestBodyType: String, Codable {
case binary = "binary"
}

enum DeleteRequestType: String, Codable {
case GET = "get"
case DELETE = "delete"
}

struct CustomUploader: Codable, Hashable, Equatable, CaseIterable, Identifiable, Defaults.Serializable {
var id: UUID
let name: String
Expand All @@ -23,8 +28,9 @@ struct CustomUploader: Codable, Hashable, Equatable, CaseIterable, Identifiable,
let requestBodyType: RequestBodyType?
let responseURL: String
let deletionURL: String?
let deleteRequestType: DeleteRequestType?

init(id: UUID = UUID(), name: String, requestURL: String, headers: [String: String]?, formData: [String: String]?, fileFormName: String?, requestBodyType: RequestBodyType? = nil, responseURL: String, deletionURL: String? = nil) {
init(id: UUID = UUID(), name: String, requestURL: String, headers: [String: String]?, formData: [String: String]?, fileFormName: String?, requestBodyType: RequestBodyType? = nil, responseURL: String, deletionURL: String? = nil, deleteRequestType: DeleteRequestType? = nil) {
self.id = id
self.name = name
self.requestURL = requestURL
Expand All @@ -34,6 +40,7 @@ struct CustomUploader: Codable, Hashable, Equatable, CaseIterable, Identifiable,
self.requestBodyType = requestBodyType
self.responseURL = responseURL
self.deletionURL = deletionURL
self.deleteRequestType = deleteRequestType
}

enum CodingKeys: String, CodingKey {
Expand All @@ -46,6 +53,7 @@ struct CustomUploader: Codable, Hashable, Equatable, CaseIterable, Identifiable,
case requestBodyType = "requestbodytype"
case responseURL = "responseurl"
case deletionURL = "deletionurl"
case deleteRequestType = "deleterequesttype"
}

init(from decoder: Decoder) throws {
Expand All @@ -60,6 +68,7 @@ struct CustomUploader: Codable, Hashable, Equatable, CaseIterable, Identifiable,
requestBodyType = try container.decodeDynamicIfPresent(RequestBodyType.self, forKey: DynamicCodingKey(stringValue: "requestbodytype")!)
responseURL = try container.decodeDynamic(String.self, forKey: DynamicCodingKey(stringValue: "responseurl")!)
deletionURL = try container.decodeDynamicIfPresent(String.self, forKey: DynamicCodingKey(stringValue: "deletionurl")!)
deleteRequestType = try container.decodeDynamicIfPresent(DeleteRequestType.self, forKey: DynamicCodingKey(stringValue: "deleterequesttype")!)
}

static var allCases: [CustomUploader] {
Expand Down

0 comments on commit afeb2b9

Please sign in to comment.