-
Notifications
You must be signed in to change notification settings - Fork 749
Expand file tree
/
Copy pathNetworkTransport.swift
More file actions
76 lines (61 loc) · 2.7 KB
/
NetworkTransport.swift
File metadata and controls
76 lines (61 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/// A network transport is responsible for sending GraphQL operations to a server.
public protocol NetworkTransport: class {
/// Send a GraphQL operation to a server and return a response.
///
/// - Parameters:
/// - operation: The operation to send.
/// - completionHandler: A closure to call when a request completes. On `success` will contain the response received from the server. On `failure` will contain the error which occurred.
/// - Returns: An object that can be used to cancel an in progress request.
func send<Operation>(operation: Operation, completionHandler: @escaping (_ result: Result<GraphQLResponse<Operation>, Error>) -> Void) -> Cancellable
/// The name of the client to send as the `"apollographql-client-name"` header.
var clientName: String { get set }
/// The version of the client to send as the `"apollographql-client-version"` header
var clientVersion: String { get set }
}
public extension NetworkTransport {
/// The header field name for the Client Name
static var headerFieldNameClientName: String {
return "apollographql-client-name"
}
/// The header field name for the client version
static var headerFieldNameClientVersion: String {
return "apollographql-client-version"
}
/// The default client name to use when setting up the `clientName` property
static var defaultClientName: String {
guard let identifier = Bundle.main.bundleIdentifier else {
return "apollo-ios-client"
}
return "\(identifier)-apollo-ios"
}
/// The default client version to use when setting up the `clientVersion` property.
static var defaultClientVersion: String {
var version = String()
if let shortVersion = Bundle.main.shortVersion {
version.append(shortVersion)
}
if let buildNumber = Bundle.main.buildNumber {
if version.isEmpty {
version.append(buildNumber)
} else {
version.append("-\(buildNumber)")
}
}
if version.isEmpty {
version = "(unknown)"
}
return version
}
}
// MARK: -
/// A network transport which can also handle uploads of files.
public protocol UploadingNetworkTransport: NetworkTransport {
/// Uploads the given files with the given operation.
///
/// - Parameters:
/// - operation: The operation to send
/// - files: An array of `GraphQLFile` objects to send.
/// - completionHandler: The completion handler to execute when the request completes or errors
/// - Returns: An object that can be used to cancel an in progress request.
func upload<Operation>(operation: Operation, files: [GraphQLFile], completionHandler: @escaping (_ result: Result<GraphQLResponse<Operation>, Error>) -> Void) -> Cancellable
}