Skip to content

Commit dca290a

Browse files
authored
Merge pull request #214 from kitasuke/support_protocol_buffers
Support protocol buffers
2 parents c76d3e2 + 37b9351 commit dca290a

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

APIKit.xcodeproj/project.pbxproj

+16
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
7F7048F11D9D8A12003C99F6 /* SessionTaskError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F7048EE1D9D8A12003C99F6 /* SessionTaskError.swift */; };
5050
7F7048F31D9D8A1F003C99F6 /* URLEncodedSerialization.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F7048F21D9D8A1F003C99F6 /* URLEncodedSerialization.swift */; };
5151
7FA1690D1D9D8C80006C982B /* HTTPStub.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FA1690C1D9D8C80006C982B /* HTTPStub.swift */; };
52+
ECA831481DE4DDBF004EB1B5 /* ProtobufDataParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECA831471DE4DDBF004EB1B5 /* ProtobufDataParser.swift */; };
53+
ECA8314A1DE4DEBE004EB1B5 /* ProtobufDataParserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECA831491DE4DEBE004EB1B5 /* ProtobufDataParserTests.swift */; };
54+
ECA8314C1DE4E677004EB1B5 /* ProtobufBodyParameters.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECA8314B1DE4E677004EB1B5 /* ProtobufBodyParameters.swift */; };
55+
ECA8314E1DE4E739004EB1B5 /* ProtobufBodyParametersTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECA8314D1DE4E739004EB1B5 /* ProtobufBodyParametersTests.swift */; };
5256
/* End PBXBuildFile section */
5357

5458
/* Begin PBXContainerItemProxy section */
@@ -126,6 +130,10 @@
126130
7F8ECDFD1B6A799E00234E04 /* Demo.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = Demo.playground; sourceTree = "<group>"; };
127131
7FA1690C1D9D8C80006C982B /* HTTPStub.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HTTPStub.swift; sourceTree = "<group>"; };
128132
CD5115241B1FFBA900514240 /* Result.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Result.framework; sourceTree = BUILT_PRODUCTS_DIR; };
133+
ECA831471DE4DDBF004EB1B5 /* ProtobufDataParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ProtobufDataParser.swift; path = Sources/APIKit/DataParser/ProtobufDataParser.swift; sourceTree = SOURCE_ROOT; };
134+
ECA831491DE4DEBE004EB1B5 /* ProtobufDataParserTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProtobufDataParserTests.swift; sourceTree = "<group>"; };
135+
ECA8314B1DE4E677004EB1B5 /* ProtobufBodyParameters.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ProtobufBodyParameters.swift; path = Sources/APIKit/BodyParameters/ProtobufBodyParameters.swift; sourceTree = SOURCE_ROOT; };
136+
ECA8314D1DE4E739004EB1B5 /* ProtobufBodyParametersTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProtobufBodyParametersTests.swift; sourceTree = "<group>"; };
129137
/* End PBXFileReference section */
130138

131139
/* Begin PBXFrameworksBuildPhase section */
@@ -176,6 +184,7 @@
176184
7F7048DB1D9D89FB003C99F6 /* FormURLEncodedBodyParameters.swift */,
177185
7F7048DC1D9D89FB003C99F6 /* JSONBodyParameters.swift */,
178186
7F7048DD1D9D89FB003C99F6 /* MultipartFormDataBodyParameters.swift */,
187+
ECA8314B1DE4E677004EB1B5 /* ProtobufBodyParameters.swift */,
179188
7F7048D81D9D89FB003C99F6 /* AbstractInputStream.m */,
180189
);
181190
path = BodyParameters;
@@ -230,6 +239,7 @@
230239
7F698E3C1D9D680C00F1561D /* FormURLEncodedBodyParametersTests.swift */,
231240
7F698E3D1D9D680C00F1561D /* JSONBodyParametersTests.swift */,
232241
7F698E3E1D9D680C00F1561D /* MultipartFormDataParametersTests.swift */,
242+
ECA8314D1DE4E739004EB1B5 /* ProtobufBodyParametersTests.swift */,
233243
7F698E3F1D9D680C00F1561D /* URLEncodedSerializationTests.swift */,
234244
);
235245
path = BodyParametersType;
@@ -240,6 +250,7 @@
240250
children = (
241251
7F698E411D9D680C00F1561D /* FormURLEncodedDataParserTests.swift */,
242252
7F698E421D9D680C00F1561D /* JSONDataParserTests.swift */,
253+
ECA831491DE4DEBE004EB1B5 /* ProtobufDataParserTests.swift */,
243254
7F698E431D9D680C00F1561D /* StringDataParserTests.swift */,
244255
);
245256
path = DataParserType;
@@ -327,6 +338,7 @@
327338
7F7048E41D9D8A08003C99F6 /* DataParser.swift */,
328339
7F7048E51D9D8A08003C99F6 /* FormURLEncodedDataParser.swift */,
329340
7F7048E61D9D8A08003C99F6 /* JSONDataParser.swift */,
341+
ECA831471DE4DDBF004EB1B5 /* ProtobufDataParser.swift */,
330342
7F7048E71D9D8A08003C99F6 /* StringDataParser.swift */,
331343
);
332344
path = DataParser;
@@ -458,8 +470,10 @@
458470
7F7048D61D9D89F2003C99F6 /* SessionAdapter.swift in Sources */,
459471
7F7048EF1D9D8A12003C99F6 /* RequestError.swift in Sources */,
460472
7F7048E91D9D8A08003C99F6 /* FormURLEncodedDataParser.swift in Sources */,
473+
ECA8314C1DE4E677004EB1B5 /* ProtobufBodyParameters.swift in Sources */,
461474
7F7048E11D9D89FB003C99F6 /* FormURLEncodedBodyParameters.swift in Sources */,
462475
7F7048F11D9D8A12003C99F6 /* SessionTaskError.swift in Sources */,
476+
ECA831481DE4DDBF004EB1B5 /* ProtobufDataParser.swift in Sources */,
463477
7F7048F31D9D8A1F003C99F6 /* URLEncodedSerialization.swift in Sources */,
464478
7F7048D71D9D89F2003C99F6 /* URLSessionAdapter.swift in Sources */,
465479
7F7048EB1D9D8A08003C99F6 /* StringDataParser.swift in Sources */,
@@ -476,6 +490,7 @@
476490
7F698E5B1D9D680C00F1561D /* SessionCallbackQueueTests.swift in Sources */,
477491
7F698E501D9D680C00F1561D /* FormURLEncodedBodyParametersTests.swift in Sources */,
478492
7F698E581D9D680C00F1561D /* RequestTests.swift in Sources */,
493+
ECA8314A1DE4DEBE004EB1B5 /* ProtobufDataParserTests.swift in Sources */,
479494
7F698E5E1D9D680C00F1561D /* TestRequest.swift in Sources */,
480495
7F698E601D9D680C00F1561D /* TestSessionTask.swift in Sources */,
481496
7FA1690D1D9D8C80006C982B /* HTTPStub.swift in Sources */,
@@ -484,6 +499,7 @@
484499
7F698E541D9D680C00F1561D /* FormURLEncodedDataParserTests.swift in Sources */,
485500
7F698E591D9D680C00F1561D /* URLSessionAdapterSubclassTests.swift in Sources */,
486501
7F698E551D9D680C00F1561D /* JSONDataParserTests.swift in Sources */,
502+
ECA8314E1DE4E739004EB1B5 /* ProtobufBodyParametersTests.swift in Sources */,
487503
7F698E511D9D680C00F1561D /* JSONBodyParametersTests.swift in Sources */,
488504
7F698E521D9D680C00F1561D /* MultipartFormDataParametersTests.swift in Sources */,
489505
7F698E531D9D680C00F1561D /* URLEncodedSerializationTests.swift in Sources */,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Foundation
2+
3+
/// `ProtobufBodyParameters` serializes Protobuf object for HTTP body and states its content type is Protobuf.
4+
public struct ProtobufBodyParameters: BodyParameters {
5+
/// The Protobuf object to be serialized.
6+
public let protobufObject: Data
7+
8+
/// Returns `ProtobufBodyParameters`.
9+
public init(protobufObject: Data) {
10+
self.protobufObject = protobufObject
11+
}
12+
13+
// MARK: - BodyParameters
14+
15+
/// `Content-Type` to send. The value for this property will be set to `Accept` HTTP header field.
16+
public var contentType: String {
17+
return "application/protobuf"
18+
}
19+
20+
/// Builds `RequestBodyEntity.data` that represents `ProtobufObject`.
21+
public func buildEntity() throws -> RequestBodyEntity {
22+
return .data(protobufObject)
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Foundation
2+
3+
/// `ProtobufDataParser` response Data data.
4+
public class ProtobufDataParser: DataParser {
5+
/// Returns `ProtobufDataParser`.
6+
public init() {}
7+
8+
// MARK: - DataParser
9+
10+
/// Value for `Accept` header field of HTTP request.
11+
public var contentType: String? {
12+
return "application/protobuf"
13+
}
14+
15+
/// Return `Any` that expresses structure of Data response.
16+
public func parse(data: Data) throws -> Any {
17+
return data
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Foundation
2+
import XCTest
3+
import APIKit
4+
5+
class ProtobufBodyParametersTests: XCTestCase {
6+
func testProtobufSuccess() {
7+
let object = NSKeyedArchiver.archivedData(withRootObject: ["foo": 1, "bar": 2, "baz": 3])
8+
let parameters = ProtobufBodyParameters(protobufObject: object)
9+
XCTAssertEqual(parameters.contentType, "application/protobuf")
10+
11+
do {
12+
guard case .data(let data) = try parameters.buildEntity() else {
13+
XCTFail()
14+
return
15+
}
16+
17+
let dictionary = NSKeyedUnarchiver.unarchiveObject(with: data) as? [String: Int]
18+
XCTAssertEqual(dictionary?["foo"], 1)
19+
XCTAssertEqual(dictionary?["bar"], 2)
20+
XCTAssertEqual(dictionary?["baz"], 3)
21+
} catch {
22+
XCTFail()
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import XCTest
2+
import APIKit
3+
import XCTest
4+
5+
class ProtobufDataParserTests: XCTestCase {
6+
func testContentType() {
7+
let parser = ProtobufDataParser()
8+
XCTAssertEqual(parser.contentType, "application/protobuf")
9+
}
10+
11+
func testProtobufSuccess() {
12+
let data = NSKeyedArchiver.archivedData(withRootObject: ["foo": 1, "bar": 2, "baz": 3])
13+
let parser = ProtobufDataParser()
14+
15+
do {
16+
let object = try parser.parse(data: data) as! Data
17+
let dictionary = NSKeyedUnarchiver.unarchiveObject(with: object) as? [String: Int]
18+
XCTAssertEqual(dictionary?["foo"], 1)
19+
XCTAssertEqual(dictionary?["bar"], 2)
20+
XCTAssertEqual(dictionary?["baz"], 3)
21+
} catch {
22+
XCTFail()
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)