Moya: [Question] Get absolute url from TargetType instance value?

Created on 27 Oct 2017  路  7Comments  路  Source: Moya/Moya

I want to cache some response data for urls, and get cached data by url. But I use a TargetType value to make a request, and I don't find an easy way to get absolute url from this value.

question

Most helpful comment

I use this code currently:

if let request = try? MoyaProvider.defaultEndpointMapping(for: target).urlRequest(),
  let url = request.url {
  let key = url.absoluteString
  return key
}

All 7 comments

@ufosky Can you use TargetType's two propertiesbaseURL & path to form the absolute URL? You could add an extension to TargetType to make this easier for you across all of your targets.

I just remembered that Moya also has a URL extension to initlaize a URL with a TargetType. You can check out the source file for this. It could help 馃槂

But this way only get url from baseURL & path, if some parameters in the url, like http://www.example.com/path?param1=test, only get result http://www.example.com/path

Sorry @ufosky you're right. I'm not sure of any easy way of doing this at the moment. The final URLRequest is constructed through the Endpoint class 馃

I use this code currently:

if let request = try? MoyaProvider.defaultEndpointMapping(for: target).urlRequest(),
  let url = request.url {
  let key = url.absoluteString
  return key
}

And you could extend your TargetType maybe to wrap this in a variable 馃憤

Sent with GitHawk

happy we could help @ufosky!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AndrewSB picture AndrewSB  路  23Comments

BasThomas picture BasThomas  路  27Comments

gperdomor picture gperdomor  路  23Comments

pete183 picture pete183  路  25Comments

justinmakaila picture justinmakaila  路  26Comments