Moya: How to handle same url with different http method?

Created on 21 Oct 2020  路  3Comments  路  Source: Moya/Moya

My api is like:
GET: /user
POST: /user
DELETE: /user

but moya target enum can not use same name, how can I handle this api style without custom diffrent enum name?

question

Most helpful comment

Thx for help!

All 3 comments

Seems there is no other way. But I think it is even better to use different cases for enum in your case. It makes your API TargetType more easy to read.

enum UserAPI {
    case getUser
    case updateUser // (or createUser)
    case deleteUser
}

extension BrandsAPI: TargetType {

   var path: String {
        return "/user"
   }

   var method: Moya.Method {
        switch self {
        case .getUser:
            return .get
        case .updateUser:
            return .post
        case .deleteUser:
            return .delete
        }
    }
}

Yeah, what @e5faf2 proposed is definitely something I'd recommend as well.

Thx for help!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

PlutusCat picture PlutusCat  路  3Comments

JoeFerrucci picture JoeFerrucci  路  3Comments

gunterhager picture gunterhager  路  3Comments

dimpiax picture dimpiax  路  3Comments