Skip to content

Future proof SDK by always sending clientVersion header #146

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

Merged
merged 2 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.7.2...main)
* _Contributing to this repo? Add info about your change here to be included in the next release_

__Improvements__
- Future proof SDK by always sending client version header. Also added http method PATCH to API for future use ([#146](https://github.com/parse-community/Parse-Swift/pull/146)), thanks to [Corey Baker](https://github.com/cbaker6).

### 1.7.2
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.7.1...1.7.2)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct GameScore: ParseObject {
let score = GameScore(score: 10)

//: Save asynchronously (preferred way) with the context option.
score.save(options: .init(.context(["hello": "world"]))) { result in
score.save(options: [.context(["hello": "world"])]) { result in
switch result {
case .success(let savedScore):
print("Successfully saved \(savedScore)")
Expand Down
2 changes: 1 addition & 1 deletion ParseSwift.playground/contents.xcplayground
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
<page name='14 - Config'/>
<page name='15 - Custom ObjectId'/>
</pages>
</playground>
</playground>
8 changes: 4 additions & 4 deletions Sources/ParseSwift/API/API+Commands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal extension API {
switch self.prepareURLRequest(options: options, childObjects: childObjects, childFiles: childFiles) {

case .success(let urlRequest):
if method == .POST || method == .PUT {
if method == .POST || method == .PUT || method == .PATCH {
let task = URLSession.parse.uploadTask(withStreamedRequest: urlRequest)
ParseSwift.sessionDelegate.uploadDelegates[task] = uploadProgress
ParseSwift.sessionDelegate.streamDelegates[task] = stream
Expand Down Expand Up @@ -128,7 +128,7 @@ internal extension API {
}
} else {
//ParseFiles are handled with a dedicated URLSession
if method == .POST || method == .PUT {
if method == .POST || method == .PUT || method == .PATCH {
switch self.prepareURLRequest(options: options,
childObjects: childObjects,
childFiles: childFiles) {
Expand Down Expand Up @@ -204,7 +204,7 @@ internal extension API {
childFiles: [UUID: ParseFile]? = nil) -> Result<URLRequest, ParseError> {
let params = self.params?.getQueryItems()
var headers = API.getHeaders(options: options)
if !(method == .POST) && !(method == .PUT) {
if !(method == .POST) && !(method == .PUT) && !(method == .PATCH) {
headers.removeValue(forKey: "X-Parse-Request-Id")
}
let url = parseURL == nil ?
Expand Down Expand Up @@ -622,7 +622,7 @@ internal extension API {
// MARK: URL Preperation
func prepareURLRequest(options: API.Options) -> Result<URLRequest, ParseError> {
var headers = API.getHeaders(options: options)
if !(method == .POST) && !(method == .PUT) {
if !(method == .POST) && !(method == .PUT) && !(method == .PATCH) {
headers.removeValue(forKey: "X-Parse-Request-Id")
}
let url = ParseSwift.configuration.serverURL.appendingPathComponent(path.urlComponent)
Expand Down
7 changes: 6 additions & 1 deletion Sources/ParseSwift/API/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Foundation
public struct API {

internal enum Method: String, Encodable {
case GET, POST, PUT, DELETE
case GET, POST, PUT, PATCH, DELETE
}

internal enum Endpoint: Encodable {
Expand Down Expand Up @@ -169,6 +169,7 @@ public struct API {
headers["X-Parse-Installation-Id"] = installationId
}

headers["X-Parse-Client-Version"] = clientVersion()
headers["X-Parse-Request-Id"] = UUID().uuidString.lowercased()

options.forEach { (option) in
Expand Down Expand Up @@ -204,6 +205,10 @@ public struct API {

return headers
}

internal static func clientVersion() -> String {
ParseConstants.sdk+ParseConstants.version
}
}

internal extension Dictionary where Key == String, Value == String? {
Expand Down
4 changes: 2 additions & 2 deletions Sources/ParseSwift/Objects/ParseInstallation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ extension ParseInstallation {
}
#endif

if parseVersion != ParseConstants.parseVersion {
parseVersion = ParseConstants.parseVersion
if parseVersion != ParseConstants.version {
parseVersion = ParseConstants.version
}
}

Expand Down
3 changes: 2 additions & 1 deletion Sources/ParseSwift/ParseConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import Foundation

enum ParseConstants {
static let parseVersion = "1.7.2"
static let sdk = "swift"
static let version = "1.7.2"
static let hashingKey = "parseSwift"
static let fileManagementDirectory = "parse/"
static let fileManagementPrivateDocumentsDirectory = "Private Documents/"
Expand Down
89 changes: 89 additions & 0 deletions Tests/ParseSwiftTests/APICommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,95 @@ class APICommandTests: XCTestCase {
}
}

func testClientVersionAPIMethod() {
let clientVersion = API.clientVersion()
XCTAssertTrue(clientVersion.contains(ParseConstants.sdk))
XCTAssertTrue(clientVersion.contains(ParseConstants.version))

let splitString = clientVersion
.components(separatedBy: ParseConstants.sdk)
XCTAssertEqual(splitString.count, 2)
//If successful, will remove `swift` resulting in ""
XCTAssertEqual(splitString[0], "")
XCTAssertEqual(splitString[1], ParseConstants.version)

//Test incorrect split
let splitString2 = clientVersion
.components(separatedBy: "hello")
XCTAssertEqual(splitString2.count, 1)
XCTAssertEqual(splitString2[0], clientVersion)
}

func testClientVersionHeader() {
let headers = API.getHeaders(options: [])
XCTAssertEqual(headers["X-Parse-Client-Version"], API.clientVersion())

let post = API.Command<Level, NoBody?>(method: .POST, path: .login) { _ in
return nil
}
switch post.prepareURLRequest(options: []) {

case .success(let request):
if request.allHTTPHeaderFields?["X-Parse-Client-Version"] != API.clientVersion() {
XCTFail("Should contain correct Client Version header")
}
case .failure(let error):
XCTFail(error.localizedDescription)
}

let put = API.Command<Level, NoBody?>(method: .PUT, path: .login) { _ in
return nil
}
switch put.prepareURLRequest(options: []) {

case .success(let request):
if request.allHTTPHeaderFields?["X-Parse-Client-Version"] != API.clientVersion() {
XCTFail("Should contain correct Client Version header")
}
case .failure(let error):
XCTFail(error.localizedDescription)
}

let patch = API.Command<Level, NoBody?>(method: .PATCH, path: .login) { _ in
return nil
}
switch patch.prepareURLRequest(options: []) {

case .success(let request):
if request.allHTTPHeaderFields?["X-Parse-Client-Version"] != API.clientVersion() {
XCTFail("Should contain correct Client Version header")
}
case .failure(let error):
XCTFail(error.localizedDescription)
}

let delete = API.Command<Level, NoBody?>(method: .DELETE, path: .login) { _ in
return nil
}
switch delete.prepareURLRequest(options: []) {

case .success(let request):
if request.allHTTPHeaderFields?["X-Parse-Client-Version"] != API.clientVersion() {
XCTFail("Should contain correct Client Version header")
}
case .failure(let error):
XCTFail(error.localizedDescription)
}

let get = API.Command<Level, NoBody?>(method: .GET, path: .login) { _ in
return nil
}
switch get.prepareURLRequest(options: []) {

case .success(let request):
if request.allHTTPHeaderFields?["X-Parse-Client-Version"] != API.clientVersion() {
XCTFail("Should contain correct Client Version header")
}
case .failure(let error):
XCTFail(error.localizedDescription)
}
}

func testIdempodency() {
let headers = API.getHeaders(options: [])
XCTAssertNotNil(headers["X-Parse-Request-Id"])
Expand Down