Moya: Unable to use relative paths for target

Created on 19 Jan 2019  ·  12Comments  ·  Source: Moya/Moya

Moya 12.0.1

We have baseURL = "www.somehost.com/api/apiversion/"
For each request we have some paths like
target.path = "some/method_a"
or
target.path = "some/method_b"
e.t.c

URL for each request constructs in URL+Moya.swift extension using the next line of code.

self = target.baseURL.appendingPathComponent(target.path)

As a result we have URL like
"www.somehost.com/api/apiversion/some/method_a"
or
"www.somehost.com/api/apiversion/some/method_b"
e.t.c

But what if we need to alternate the path from the root for some rare case?
Like "www.somehost.com/alternative/path/method_z"

It will be logically to use relative path for this case:
target.path = "../../alternative/path/method_z"

But the current implementation will result with this:
"www.somehost.com/api/apiversion/../../alternative/path/method_z"

Looks like an issue.

It could be solved by creating URL using path relative to the base URL:

self = URL(string: target.path, relativeTo: target.baseURL)

Is there any alternative solutions how to solve such issue?

question stale

All 12 comments

Hey @artbasil, I think keeping only www.somehost.com as baseURL would solve your problem. You can then append api/apiversion/some/method_a or alternative/path/method_z to it.

Hi Pedro,

This is the one of the solutions. But this will require much more than only one line of code in the existing application.

Best regards,
Basil

21 янв. 2019 г., в 14:50, Pedro Vereza notifications@github.com написал(а):

Hey @artbasil, I think keeping only www.somehost.com as baseURL would solve your problem. You can then append api/apiversion/some/method_a or alternative/path/method_z to it.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

@artbasil I'd like to better understand your use case.

Why isn't the baseURL set to www.somehost.com? Have you considered wrapping the custom path creation in functions? Similar to:

    var baseURL: URL { return URL(string:"http://somehost.com")! }

    var path: String {
        switch self {
        case .method_a:
            return standardApiPath(forMethod: "method_a")
        case .method_b:
            return standardApiPath(forMethod: "method_b")
        case .method_z:
            return alternativeApiPath(forMethod: "method_z")
        }
    }

    private func standardApiPath(forMethod method: String) -> String {
        return "api/apiversion/\(method)"
    }

    private func alternativeApiPath(forMethod method: String) -> String {
        return "alternative/path/\(method)"
    }

@pedrovereza sorry for not being clear in my post.
I have some existing project with some existing API implementation based on some third party framework. And we would like to migrate to the Moya.
We have some code already used regular API and the alternative API. And it should be just easy as switch one framework to another. But the existing implementation rely on the possibility to use relative paths. Your framework does not support that feature. I've provided the possible solution. Which is not making conflict with the existing functionality.

@artbasil I think the change would be ok, as long as it is backwards compatible. Could you send over a PR with the change?

@Moya/core-team any thoughts or comments?

I don't see any reason this wouldn't work – I'd want to make sure we have a unit test but yeah, sounds like a great idea 👍

There only thing which is needed to think about is error behavior. As this function returns optional. And needs to be unwrapped.
self = URL(string: target.path, relativeTo: target.baseURL) ?? target.baseURL
as a possible solution.

@artbasil I'd rather have it throw an error. If the user is trying to build a malformed URL, we shouldn't hide the problem

Does that method throw? I agreed that would be ideal.

That was my first idea. But this method doesn't trow.

This issue has been marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

This issue has been auto-closed because there hasn't been any activity for at least 21 days. However, we really appreciate your contribution, so thank you for that! 🙏 Also, feel free to open a new issue if you still experience this problem 👍.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mwawrusch picture mwawrusch  ·  3Comments

dimpiax picture dimpiax  ·  3Comments

JianweiWangs picture JianweiWangs  ·  3Comments

kamwysoc picture kamwysoc  ·  3Comments

Tynox picture Tynox  ·  3Comments