Thats my model class
struct LoginResponse: Codable {
let main: LoginModel
}
struct LoginModel: Codable {
let success: Bool?
let token: String?
let message: String?
static var placeholder: LoginModel {
return LoginModel(success: nil, token: nil, message: nil)
}
}
Thats my service. I have one more issue i am using two map here but when try to remove map.data getting error in dataTaskPublisher. error mention below
Instance method 'decode(type:decoder:)' requires the types 'URLSession.DataTaskPublisher.Output' (aka '(data: Data, response: URLResponse)') and 'JSONDecoder.Input' (aka 'Data') be equivalent
class LoginService {
func doLoginTask(username: String, password: String) -> AnyPublisher<LoginModel, Error> {
let networkQueue = DispatchQueue(label: "Networking",
qos: .default,
attributes: .concurrent)
guard let url = URL(string: Constants.URLs.baseUrl(urlPath: Constants.URLs.loginPath)) else {
fatalError("Invalid URL")
}
print("uri", url)
let body: [String: String] = ["username": username, "password": password]
let finalBody = try! JSONSerialization.data(withJSONObject: body)
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = finalBody
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
return URLSession.shared.dataTaskPublisher(for: request)
.map(\.data)
.decode(type: LoginResponse.self, decoder: JSONDecoder())
.map { $0.main }
.receive(on: networkQueue)
.eraseToAnyPublisher()
}
}
Thats my contentView
Button(action: {
self.counter += 1
print("count from action", self.counter)
func loaginTask() {
_ = loginService.doLoginTask(username: "1234567890", password: "12345")
.sink(
receiveCompletion: {
print("Received Completion: \($0)") },
receiveValue: { doctor in
print("hhhhh")
// print("yes ", doctor.message as Any)
}
)
}
})
Thats my json response
{
"success": true,
"token": "ed48aa9b40c2d88079e6fd140c87ac61fc9ce78a",
"expert-token": "6ec84e92ea93b793924d48aa9b40c2d88079e6fd140c87ac61fc9ce78ae4fa93",
"message": "Logged in successfully"
}