By Moya in Swift,how can I send a request with absolute url when already having baseURL?

378 views Asked by At

Moya version: master integrated by GitHub

part of code sample:

enum Api {
    
    case initialize(deviceName: String, upushDeviceToken: String)
    case loadImage(_ url: String)
}

extension Api: TargetType {
    
    var baseURL: URL {
        URL(string: ApiKit.baseUrl)!
    }
    
    var path: String {
        switch self {
        case .initialize:
            return "init"
        case .loadImage(let url):
            return "\(url)"
    }
}

the loadImage request url is an absolute url like https://some-site/storage/image.png

When I tried loadImage, it combined basic url and the url, therefore server side return error 404

I guess it might have a solution of creating another TargetType to handle these absolute url requests, but I still want to know if there's a way to solve this in one TargetType.

1

There are 1 answers

0
Dayanithi Natarajan On

Hope this helps.

enum Api {
    
    case initialize(deviceName: String, upushDeviceToken: String)
    case loadImage(_ url: String)
}

extension Api: TargetType {
    
    var baseURL: URL {
        URL(string: "https://some-website.com")!
    }
    
    var path: String {
        switch self {
        case .initialize:
            return "init"
        case .loadImage(let url):
            // url should have /storage/image.png
            return "\(url)"
        }
    }
}

BaseURL + path is the final URL = https://some-website.com/storage/image.png