Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for customizing target's url #6

Merged
merged 2 commits into from
Nov 24, 2016
Merged

Conversation

devxoul
Copy link
Owner

@devxoul devxoul commented Nov 24, 2016

Summary

This PR provides an way to customize the target's url.

Background

Imagine that we have an API which returns the next url path or url:

{
  "data": [
    {...},
    {...},
    {...},
  ],
  "next": "/articles?page=2"
}

In this case, we can add a case path(String):

enum MyAPI: SugarTargetType {
  case path(String)
  case user(Int)

  var route: Route {
    switch self {
    case .path(let path):
      return .get(path)
    case .user(let id):
      return .get("/user/\(id)")
    }
  }
}

Default implementation of Moya uses URL.appendingPathComponent(_:) to make URL, so the URL will become http://example.com/articles%3Fpage=2 (? becomes %3F)

Customizing URL

With this PR, you'll be able to customize target URLs.

extension MyAPI: SugarTargetType {
  var url: URL {
    switch self {
    case .path:
      return URL(string: self.baseURL + self.path)! // custom url
    default:
      return self.defaultURL // default url
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant