Skip to content

Commit

Permalink
Rename class name that follow the API design guidelines
Browse files Browse the repository at this point in the history
  • Loading branch information
jasl committed Apr 9, 2016
1 parent fcbacbc commit e772f53
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Example/MoyaXDemo-iOS/Sources/GitHubAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public enum GitHub {
case UserRepositories(String)
}

extension GitHub: TargetType {
extension GitHub: Target {
public var baseURL: NSURL { return NSURL(string: "https://api.github.com")! }
public var path: String {
switch self {
Expand Down
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ github "jasl/MoyaX"

### Declare a remote API (target)

First, you can declare a `struct`, `class` or an `enum` and let it conform `TargetType` protocol to store remote API's informations.
First, you can declare a `struct`, `class` or an `enum` and let it conform `Target` protocol to store remote API's informations.

```swift
// This struct defined Github show user API
struct GithubShowUser: TargetType {
struct GithubShowUser: Target {
// The username which should requesting
let name: String

Expand Down
6 changes: 3 additions & 3 deletions Readme_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ MoyaX 的基本使用仅需两步。

### 声明服务器端 API

只需要声明一个类、结构体、枚举,使其实现 `TargetType` 协议,如:
只需要声明一个类、结构体、枚举,使其实现 `Target` 协议,如:

```swift
// 这个结构体封装了 Github 的返回指定用户信息的 API
struct GithubShowUser: TargetType {
struct GithubShowUser: Target {
// 要请求的用户的用户名
let name: String

Expand Down Expand Up @@ -124,4 +124,4 @@ provider.request(GithubShowUser(name: "jasl")) { response in

## License

MoyaX 被许可在 MIT 协议下使用。查阅 LICENSE 文件来获得更多信息。
MoyaX 被许可在 MIT 协议下使用。查阅 LICENSE 文件来获得更多信息。
6 changes: 3 additions & 3 deletions Sources/Backends/AlamofireBackend.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import Alamofire

internal final class AlamofireCancellableToken: Cancellable {
internal final class AlamofireCancellableToken: CancellableToken {
internal(set) var request: Alamofire.Request?
private(set) var isCancelled: Bool = false

Expand Down Expand Up @@ -32,7 +32,7 @@ internal final class AlamofireCancellableToken: Cancellable {
}

/// The backend for Alamofire
public class AlamofireBackend: BackendType {
public class AlamofireBackend: Backend {
/// Default Alamofire manager for backend.
public static let defaultManager: Manager = {
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
Expand Down Expand Up @@ -73,7 +73,7 @@ public class AlamofireBackend: BackendType {
/**
Encodes the endpoint to Alamofire's Request and perform it.
*/
public func request(endpoint: Endpoint, completion: Completion) -> Cancellable {
public func request(endpoint: Endpoint, completion: Completion) -> CancellableToken {
let cancellableToken = AlamofireCancellableToken()

let request = NSMutableURLRequest(URL: endpoint.URL)
Expand Down
26 changes: 13 additions & 13 deletions Sources/Backends/StubBackend.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Foundation

public protocol TargetWithSampleType: TargetType {
public protocol TargetWithSampleType: Target {
var sampleResponse: StubResponse { get }
}

internal final class StubCancellableToken: Cancellable {
internal final class StubCancellableToken: CancellableToken {
private(set) var isCancelled = false

private var lock: OSSpinLock = OS_SPINLOCK_INIT
Expand Down Expand Up @@ -39,7 +39,7 @@ public enum StubResponse {
}

public struct StubRule {
public typealias ConditionalResponseClosure = (endpoint: Endpoint, target: TargetType?) -> StubResponse
public typealias ConditionalResponseClosure = (endpoint: Endpoint, target: Target?) -> StubResponse
let URL: NSURL
let behavior: StubBehavior?
let conditionalResponse: ConditionalResponseClosure
Expand Down Expand Up @@ -77,7 +77,7 @@ public func == (lhs: StubAction, rhs: StubAction) -> Bool {
return lhs.hashValue == rhs.hashValue
}

public class StubBackend: BackendType {
public class StubBackend: Backend {
internal var stubs: [StubAction: StubRule]

public let defaultBehavior: StubBehavior
Expand All @@ -90,20 +90,20 @@ public class StubBackend: BackendType {
self.defaultResponse = defaultResponse
}

public func stubTarget(target: TargetType, rule: StubRule) {
public func stubTarget(target: Target, rule: StubRule) {
let action = StubAction(URL: target.fullURL, method: target.method)
let rule = rule

self.stubs[action] = rule
}

public func stubTarget(target: TargetType, behavior: StubBehavior? = nil, conditionalResponse: StubRule.ConditionalResponseClosure) {
public func stubTarget(target: Target, behavior: StubBehavior? = nil, conditionalResponse: StubRule.ConditionalResponseClosure) {
let rule = StubRule(URL: target.fullURL, behavior: behavior, conditionalResponse: conditionalResponse)

self.stubTarget(target, rule: rule)
}

public func stubTarget(target: TargetType, behavior: StubBehavior? = nil, response: StubResponse) {
public func stubTarget(target: Target, behavior: StubBehavior? = nil, response: StubResponse) {
let rule = StubRule(URL: target.fullURL, behavior: behavior, response: response)

self.stubTarget(target, rule: rule)
Expand All @@ -113,13 +113,13 @@ public class StubBackend: BackendType {
self.stubs = [:]
}

public func removeStubTarget(target: TargetType) {
public func removeStubTarget(target: Target) {
let action = StubAction(URL: target.fullURL, method: target.method)

self.stubs.removeValueForKey(action)
}

public func request(endpoint: Endpoint, completion: Completion) -> Cancellable {
public func request(endpoint: Endpoint, completion: Completion) -> CancellableToken {
let target = endpoint.target
let action = StubAction(URL: endpoint.URL, method: endpoint.method)

Expand Down Expand Up @@ -167,20 +167,20 @@ public class StubBackend: BackendType {
}
}

public class GenericStubBackend<Target: TargetType>: StubBackend {
public class GenericStubBackend<TargetType: Target>: StubBackend {
public override init(defaultBehavior: StubBehavior = .Immediate, defaultResponse: StubResponse = .NoStubError) {
super.init(defaultBehavior: defaultBehavior, defaultResponse: defaultResponse)
}

public func stub(target: Target, behavior: StubBehavior? = nil, response: StubResponse) {
public func stub(target: TargetType, behavior: StubBehavior? = nil, response: StubResponse) {
self.stubTarget(target, behavior: behavior, response: response)
}

public func stub(target: Target, behavior: StubBehavior? = nil, conditionalResponse: StubRule.ConditionalResponseClosure) {
public func stub(target: TargetType, behavior: StubBehavior? = nil, conditionalResponse: StubRule.ConditionalResponseClosure) {
self.stubTarget(target, behavior: behavior, conditionalResponse: conditionalResponse)
}

public func removeStub(target: Target) {
public func removeStub(target: TargetType) {
self.removeStubTarget(target)
}
}
4 changes: 2 additions & 2 deletions Sources/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
/// Endpoint is the intermediate representation for a target on requesting
public final class Endpoint {
/// The raw target instance
public let target: TargetType?
public let target: Target?
/// Set `false` will abort the request
public var willPerform = true

Expand All @@ -15,7 +15,7 @@ public final class Endpoint {
public var parameters: [String: AnyObject]
public let parameterEncoding: ParameterEncoding

public init(target: TargetType) {
public init(target: Target) {
self.target = target

self.URL = target.fullURL
Expand Down
6 changes: 3 additions & 3 deletions Sources/Middlewares/NetworkActivityMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public enum NetworkActivityChangeType {
}

/// Notify a request's network activity changes (request begins or ends).
public final class NetworkActivityMiddleware: MiddlewareType {
public final class NetworkActivityMiddleware: Middleware {

public typealias NetworkActivityClosure = (NetworkActivityChangeType) -> ()
let networkActivityClosure: NetworkActivityClosure
Expand All @@ -18,12 +18,12 @@ public final class NetworkActivityMiddleware: MiddlewareType {
// MARK: Middleware

/// Called by the provider as soon as the request is about to start
public func willSendRequest(target: TargetType, endpoint: Endpoint) {
public func willSendRequest(target: Target, endpoint: Endpoint) {
networkActivityClosure(.Began)
}

/// Called by the provider as soon as a response arrives, even the request is cancelled.
public func didReceiveResponse(target: TargetType, response: Result<Response, Error>) {
public func didReceiveResponse(target: Target, response: Result<Response, Error>) {
networkActivityClosure(.Ended)
}
}
18 changes: 9 additions & 9 deletions Sources/MoyaX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public enum MultipartFormData {
}

/// Protocol to define the base URL, path, method, parameters and etc. for a target.
public protocol TargetType {
public protocol Target {
/// Required
var baseURL: NSURL { get }
/// Required
Expand All @@ -73,7 +73,7 @@ public protocol TargetType {
var endpoint: Endpoint { get }
}

public extension TargetType {
public extension Target {
var method: HTTPMethod {
return .GET
}
Expand All @@ -99,15 +99,15 @@ public extension TargetType {
}

/// Protocol to define a middleware that can be regiestered to provider
public protocol MiddlewareType {
public protocol Middleware {
/**
Will be called before the endpoint be passed to backend.

- Parameters:
- target: The target instance which being requested
- endpoint: The intermediate representation of target, modify it will cause side-effect
*/
func willSendRequest(target: TargetType, endpoint: Endpoint)
func willSendRequest(target: Target, endpoint: Endpoint)

/**
Will be called before calling completion closure.
Expand All @@ -116,22 +116,22 @@ public protocol MiddlewareType {
- target: The target instance which being requested
- response: The result of the request
*/
func didReceiveResponse(target: TargetType, response: Result<Response, Error>)
func didReceiveResponse(target: Target, response: Result<Response, Error>)
}

/// Protocol to define a backend which handle transform endpoint to request and perform it.
public protocol BackendType: class {
func request(endpoint: Endpoint, completion: Completion) -> Cancellable
public protocol Backend: class {
func request(endpoint: Endpoint, completion: Completion) -> CancellableToken
}

/// Protocol to define the opaque type returned from a request
public protocol Cancellable: CustomDebugStringConvertible {
public protocol CancellableToken: CustomDebugStringConvertible {
func cancel()
var debugDescription: String { get }
}

/// A fake Cancellable implementation for request which aborted by setting `endpoint.shouldPerform = false`
internal final class AbortingCancellableToken: Cancellable {
internal final class AbortingCancellableToken: CancellableToken {
func cancel() {}

var debugDescription: String {
Expand Down
18 changes: 9 additions & 9 deletions Sources/Provider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import Foundation
/// Request provider class. Requests should be made through this class only.
public class MoyaXProvider {

public let backend: BackendType
public let middlewares: [MiddlewareType]
public let backend: Backend
public let middlewares: [Middleware]
private let prepareForEndpoint: (Endpoint -> ())?

/**
Expand All @@ -15,8 +15,8 @@ public class MoyaXProvider {
- Parameter prepareForEndpoint: a closure will be called on `request` method, mostly used for modifying endpoint,
e.g: add an authentication header
*/
public init(backend: BackendType = AlamofireBackend(),
middlewares: [MiddlewareType] = [],
public init(backend: Backend = AlamofireBackend(),
middlewares: [Middleware] = [],
prepareForEndpoint: (Endpoint -> ())? = nil) {
self.backend = backend
self.middlewares = middlewares
Expand Down Expand Up @@ -45,7 +45,7 @@ public class MoyaXProvider {

- Returns: The cancellable token for the request.
*/
public final func request(target: TargetType, withCustomBackend backend: BackendType? = nil, completion: Completion) -> Cancellable {
public final func request(target: Target, withCustomBackend backend: Backend? = nil, completion: Completion) -> CancellableToken {
let endpoint = target.endpoint

self.prepareForEndpoint?(endpoint)
Expand All @@ -71,7 +71,7 @@ public class MoyaXProvider {

/// Request provider class. Requests should be made through this class only.
/// This is the generic provider that convenient for `enum` targets
public class MoyaXGenericProvider<Target: TargetType>: MoyaXProvider {
public class MoyaXGenericProvider<TargetType: Target>: MoyaXProvider {

/**
Initializes a provider.
Expand All @@ -81,8 +81,8 @@ public class MoyaXGenericProvider<Target: TargetType>: MoyaXProvider {
- Parameter prepareForEndpoint: a closure will be called on `request` method, mostly used for modifying endpoint,
e.g: add an authentication header
*/
public override init(backend: BackendType = AlamofireBackend(),
middlewares: [MiddlewareType] = [],
public override init(backend: Backend = AlamofireBackend(),
middlewares: [Middleware] = [],
prepareForEndpoint: (Endpoint -> ())? = nil) {
super.init(backend: backend, middlewares: middlewares, prepareForEndpoint: prepareForEndpoint)
}
Expand All @@ -109,7 +109,7 @@ public class MoyaXGenericProvider<Target: TargetType>: MoyaXProvider {

- Returns: The cancellable token for the request.
*/
public func request(target: Target, withCustomBackend backend: BackendType? = nil, completion: Completion) -> Cancellable {
public func request(target: TargetType, withCustomBackend backend: Backend? = nil, completion: Completion) -> CancellableToken {
return super.request(target, withCustomBackend: backend, completion: completion)
}
}
2 changes: 1 addition & 1 deletion Tests/Sources/TestTarget.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import MoyaX

struct TestTarget: TargetType {
struct TestTarget: Target {
var baseURL: NSURL { return NSURL(string: "http://test.local")! }
var path: String { return "foo/bar" }

Expand Down

0 comments on commit e772f53

Please sign in to comment.