encodable request doesn't return a Model as expected. (Combine)

#54 · closed · 4 comments

View on GitHub ↗

irangareddy

```swift struct OTPRequest: Codable { let mobile, countryCode: String enum CodingKeys: String, CodingKey { case mobile case countryCode = "country_code" } } ``` ```swift struct OTPResponse: Codable { let data: String? let message: String } ``` Expected Request ```swift public func authenticateUser(authRequest: OTPRequest) -> AnyPublisher<OTPResponse, Error> { client.post("/otp/",encodable: authRequest) } ``` Error: No 'post' candidates produce the expected contextual result type 'AnyPublisher<OTPResponse, Error>' Work around solution I am using right know is: ```swift public func authenticateUser(authRequest: OTPRequest) -> AnyPublisher<OTPResponse, Error> { client.post("/otp/",params: ["mobile" : authRequest.mobile, "country_code": authRequest.countryCode ]) } ``` Kindly let me know if there is best approach other than this. Responses Appreciated!

Comments

s4cha

@irangareddy I assume you are using the encodable api form the `encodable_post` branch (commit `273fdd2a3135e186a58d282421223ff80bc90f0d`. This has not been merge yet. I think the encodable api is not (yet) available for `AnyPublisher<T, Error>` only for other types. It should be available via the async await api though

s4cha

I am going to add the missing AnyPublisher<T, Error> version. In the meantime, the `async` version should work ```swift public func authenticateUser(authRequest: OTPRequest) async throws -> OTPResponse { try await network.post("/otp/",encodable: authRequest) } ```

s4cha

The missing apis have been added in the latest commit `7eeccab55bc57be245c83c6766015d67b5fb2d19`, @irangareddy let me know if that works :)

irangareddy

Thanks for the changes @s4cha! It's working as expected.