Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save LukaszDziwosz/3639a459966bf218673a6999349821fc to your computer and use it in GitHub Desktop.
Save LukaszDziwosz/3639a459966bf218673a6999349821fc to your computer and use it in GitHub Desktop.
// Post
func fetchToken(reqBody: TokenRequest) -> AnyPublisher<TokenResponse, AFError> {
let url = URL(string: baseURL + Endpoints.start.makeURL)!
let headers: HTTPHeaders = [
"Content-Type" : "application/json"
]
return AF.request(url, method: .post, parameters: reqBody, encoder: JSONParameterEncoder.default, headers: headers)
.validate()
.publishDecodable(type: TokenResponse.self)
.value()
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
// multipart image upload
var progressFloat = PassthroughSubject<Float,Never>()
func uploadPhoto(image: UIImage) -> AnyPublisher<[String], AFError> {
let token = TokenProvider.getToken()
let url = URL(string: baseURL + Endpoints.images.makeURL)!
let headers: HTTPHeaders = [
"Content-Type" : "application/json",
"Authorization" : "Bearer \(token)"
]
return AF.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(image.jpegData(compressionQuality: 0.5)!, withName: UUID().uuidString, fileName: "file.jpeg", mimeType: "image/jpg")
},
to: url, method: .post , headers: headers)
.uploadProgress(queue: .main, closure: { progress in
//Current upload progress of file
self.progressFloat.send(Float(progress.fractionCompleted))
})
.publishDecodable(type: [String].self)
.value()
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
// delete
func deleteJob(jobID: Int) -> AnyPublisher<SimpleJob, AFError> {
let token = TokenProvider.getToken()
let url = URL(string: baseURL + Endpoints.findJobs(id: jobID).makeURL)!
let headers: HTTPHeaders = [
"Content-Type" : "application/json",
"Authorization" : "Bearer \(token)"
]
return AF.request(url, method: .delete, headers: headers)
.validate()
.publishDecodable(type: SimpleJob.self)
.value()
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
// empty response request
func userComplete(userID: Int, actualHours: Int) -> AnyPublisher<EmptyEntity, AFError> {
let token = TokenProvider.getToken()
let url = URL(string: baseURL + Endpoints.heroComplete(userID: userID).makeURL)!
let headers: HTTPHeaders = [
"Content-Type" : "application/json",
"Authorization" : "Bearer \(token)"
]
let body = UserComplete(actualHours: actualHours, isCompleted: true)
return AF.request(url, method: .post, parameters: body, encoder: JSONParameterEncoder.default, headers: headers)
.validate()
.publishDecodable(type: EmptyEntity.self, emptyResponseCodes: [200, 204, 205])
.value()
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
struct EmptyEntity: Codable, EmptyResponse {
static func emptyValue() -> EmptyEntity {
return EmptyEntity.init()
}
// Download PDF
func downloadAgreement(heroID: Int) -> AnyPublisher<URL, AFError> {
let token = TokenProvider.getToken()
let url = URL(string: baseURL + Endpoints.agreementDownload(heroID: heroID).makeURL)!
let headers: HTTPHeaders = [
"Content-Type" : "application/json",
"Authorization" : "Bearer \(token)"
]
let destinationPath: DownloadRequest.Destination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0];
let fileURL = documentsURL.appendingPathComponent("Agreement\(heroID).pdf")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
return AF.download(url, method: .get, headers: headers, to: destinationPath)
.validate()
.publishURL()
.value()
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
//
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment