-
Notifications
You must be signed in to change notification settings - Fork 749
Expand file tree
/
Copy pathNetworkTransport.swift
More file actions
95 lines (76 loc) · 3.71 KB
/
NetworkTransport.swift
File metadata and controls
95 lines (76 loc) · 3.71 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import Foundation
/// 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.
///
/// Note if you're implementing this yourself rather than using one of the batteries-included versions of `NetworkTransport` (which handle this for you): The `clientName` and `clientVersion` should be sent with any URL request which needs headers so your client can be identified by tools meant to see what client is using which request. The `addApolloClientHeaders` method is provided below to do this for you if you're using Apollo Graph Manager.
///
/// - 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: GraphQLOperation>(operation: Operation, completionHandler: @escaping (_ result: Result<GraphQLResponse<Operation.Data>, Error>) -> Void) -> Cancellable
/// The name of the client to send as a header value.
var clientName: String { get }
/// The version of the client to send as a header value.
var clientVersion: String { get }
}
public extension NetworkTransport {
/// The field name for the Apollo Client Name header
static var headerFieldNameApolloClientName: String {
return "apollographql-client-name"
}
/// The field name for the Apollo Client Version header
static var headerFieldNameApolloClientVersion: 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"
}
var clientName: String {
return Self.defaultClientName
}
/// The default client version to use when setting up the `clientVersion` property.
static var defaultClientVersion: String {
var version = String()
if let shortVersion = Bundle.main.apollo.shortVersion {
version.append(shortVersion)
}
if let buildNumber = Bundle.main.apollo.buildNumber {
if version.isEmpty {
version.append(buildNumber)
} else {
version.append("-\(buildNumber)")
}
}
if version.isEmpty {
version = "(unknown)"
}
return version
}
var clientVersion: String {
return Self.defaultClientVersion
}
/// Adds the Apollo client headers for this instance of `NetworkTransport` to the given request
/// - Parameter request: A mutable URLRequest to add the headers to.
func addApolloClientHeaders(to request: inout URLRequest) {
request.setValue(self.clientName, forHTTPHeaderField: Self.headerFieldNameApolloClientName)
request.setValue(self.clientVersion, forHTTPHeaderField: Self.headerFieldNameApolloClientVersion)
}
}
// 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: GraphQLOperation>(operation: Operation, files: [GraphQLFile], completionHandler: @escaping (_ result: Result<GraphQLResponse<Operation.Data>, Error>) -> Void) -> Cancellable
}