Skip to content

Commit a7a6f40

Browse files
authored
New Feature: Health Check (#134)
* New Feature: Health Check * Fix test for linux comparison * Simplify error check for linux * Remove debugDescription in ParseHealth
1 parent 2704ee9 commit a7a6f40

File tree

17 files changed

+350
-21
lines changed

17 files changed

+350
-21
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
# Parse-Swift Changelog
22

33
### main
4-
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.7.0...main)
4+
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.7.1...main)
55
* _Contributing to this repo? Add info about your change here to be included in the next release_
66

7+
### 1.7.1
8+
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.7.0...1.7.1)
9+
10+
__New features__
11+
- Can now check the health of a Parse Server using ParseHealth. ([#134](https://github.com/parse-community/Parse-Swift/pull/134)), thanks to [Corey Baker](https://github.com/cbaker6).
12+
713
### 1.7.0
814
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.6.0...1.7.0)
915

ParseSwift.playground/Pages/1 - Your first Object.xcplaygroundpage/Contents.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ npm start -- --appId applicationId --clientKey clientKey --masterKey masterKey -
1818

1919
initializeParse()
2020

21+
//: Check the health of your Parse Server.
22+
print(try ParseHealth.check())
23+
2124
//: Create your own value typed `ParseObject`.
2225
struct GameScore: ParseObject {
2326
//: Those are required for Object

ParseSwift.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "ParseSwift"
3-
s.version = "1.7.0"
3+
s.version = "1.7.1"
44
s.summary = "Parse Pure Swift SDK"
55
s.homepage = "https://github.com/parse-community/Parse-Swift"
66
s.authors = {

ParseSwift.xcodeproj/project.pbxproj

Lines changed: 44 additions & 8 deletions
Large diffs are not rendered by default.

Scripts/jazzy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ bundle exec jazzy \
55
--author_url http://parseplatform.org \
66
--github_url https://github.com/parse-community/Parse-Swift \
77
--root-url http://parseplatform.org/Parse-Swift/api/ \
8-
--module-version 1.7.0 \
8+
--module-version 1.7.1 \
99
--theme fullwidth \
1010
--skip-undocumented \
1111
--output ./docs/api \

Sources/ParseSwift/API/API.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public struct API {
3636
case jobs(name: String)
3737
case aggregate(className: String)
3838
case config
39+
case health
3940
case any(String)
4041

4142
var urlComponent: String {
@@ -80,6 +81,8 @@ public struct API {
8081
return "/aggregate/\(className)"
8182
case .config:
8283
return "/config"
84+
case .health:
85+
return "/health"
8386
case .any(let path):
8487
return path
8588
}

Sources/ParseSwift/API/Responses.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,8 @@ internal struct ConfigFetchResponse<T>: Codable where T: ParseConfig {
124124
internal struct ConfigUpdateResponse: Codable {
125125
let result: Bool
126126
}
127+
128+
// MARK: HealthResponse
129+
internal struct HealthResponse: Codable {
130+
let status: String
131+
}

Sources/ParseSwift/Objects/ParseUser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ extension ParseUser {
331331
unless your code is already running from a background thread.
332332
- parameter options: A set of header options sent to the server. Defaults to an empty set.
333333
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
334-
- parameter completion: A block that will be called when logging out, completes or fails.
334+
- parameter completion: A block that will be called when logging out completes or fails.
335335
*/
336336
public static func logout(options: API.Options = [], callbackQueue: DispatchQueue = .main,
337337
completion: @escaping (Result<Void, ParseError>) -> Void) {

Sources/ParseSwift/ParseConstants.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import Foundation
1010

1111
enum ParseConstants {
12-
static let parseVersion = "1.7.0"
12+
static let parseVersion = "1.7.1"
1313
static let hashingKey = "parseSwift"
1414
static let fileManagementDirectory = "parse/"
1515
static let fileManagementPrivateDocumentsDirectory = "Private Documents/"

Sources/ParseSwift/Types/ParseCloud+combine.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public extension ParseCloud {
3232
// MARK: Jobs - Combine
3333

3434
/**
35-
Starts a Cloud Code job *asynchronously* and returns a result with the jobStatusId of the job.
35+
Starts a Cloud Code Job *asynchronously* and returns a result with the jobStatusId of the job.
3636
Publishes when complete.
3737
- parameter options: A set of header options sent to the server. Defaults to an empty set.
3838
- returns: A publisher that eventually produces a single value and then finishes or fails.

Sources/ParseSwift/Types/ParseCloud.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ extension ParseCloud {
4242
Calls a Cloud Code function *asynchronously* and returns a result of it's execution.
4343
- parameter options: A set of header options sent to the server. Defaults to an empty set.
4444
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
45-
- parameter completion: A block that will be called when logging out, completes or fails.
45+
- parameter completion: A block that will be called when the Cloud Code completes or fails.
4646
It should have the following argument signature: `(Result<ReturnType, ParseError>)`.
4747
*/
4848
public func runFunction(options: API.Options = [],
@@ -70,7 +70,7 @@ extension ParseCloud {
7070
// MARK: Jobs
7171
extension ParseCloud {
7272
/**
73-
Starts a Cloud Code job *synchronously* and returns a result with the jobStatusId of the job.
73+
Starts a Cloud Code Job *synchronously* and returns a result with the jobStatusId of the job.
7474
- parameter options: A set of header options sent to the server. Defaults to an empty set.
7575
- returns: Returns a `Decodable` type.
7676
*/
@@ -79,10 +79,10 @@ extension ParseCloud {
7979
}
8080

8181
/**
82-
Starts a Cloud Code job *asynchronously* and returns a result with the jobStatusId of the job.
82+
Starts a Cloud Code Job *asynchronously* and returns a result with the jobStatusId of the job.
8383
- parameter options: A set of header options sent to the server. Defaults to an empty set.
8484
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
85-
- parameter completion: A block that will be called when logging out, completes or fails.
85+
- parameter completion: A block that will be called when the Cloud Code Job completes or fails.
8686
It should have the following argument signature: `(Result<ReturnType, ParseError>)`.
8787
*/
8888
public func startJob(options: API.Options = [],

Sources/ParseSwift/Types/ParseConfig+combine.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public extension ParseConfig {
1717
// MARK: Fetchable - Combine
1818

1919
/**
20-
Fetch the Config *asynchronously*.
20+
Fetch the Config *asynchronously*. Publishes when complete.
2121
- parameter options: A set of header options sent to the server. Defaults to an empty set.
2222
- returns: A publisher that eventually produces a single value and then finishes or fails.
2323
*/

Sources/ParseSwift/Types/ParseConfig.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extension ParseConfig {
3232
Fetch the Config *asynchronously*.
3333
- parameter options: A set of header options sent to the server. Defaults to an empty set.
3434
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
35-
- parameter completion: A block that will be called when logging out, completes or fails.
35+
- parameter completion: A block that will be called when retrieving the config completes or fails.
3636
It should have the following argument signature: `(Result<Self, ParseError>)`.
3737
*/
3838
public func fetch(options: API.Options = [],
@@ -73,7 +73,7 @@ extension ParseConfig {
7373
Update the Config *asynchronously*.
7474
- parameter options: A set of header options sent to the server. Defaults to an empty set.
7575
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
76-
- parameter completion: A block that will be called when logging out, completes or fails.
76+
- parameter completion: A block that will be called when retrieving the config completes or fails.
7777
It should have the following argument signature: `(Result<Bool, ParseError>)`.
7878
*/
7979
public func save(options: API.Options = [],
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// ParseHealth+Combine.swift
3+
// ParseSwift
4+
//
5+
// Created by Corey Baker on 4/28/21.
6+
// Copyright © 2021 Parse Community. All rights reserved.
7+
//
8+
9+
#if canImport(Combine)
10+
import Foundation
11+
import Combine
12+
13+
// MARK: Combine
14+
@available(macOS 10.15, iOS 13.0, macCatalyst 13.0, watchOS 6.0, tvOS 13.0, *)
15+
public extension ParseHealth {
16+
17+
// MARK: Check - Combine
18+
19+
/**
20+
Calls the health check function *asynchronously*. Publishes when complete.
21+
- parameter options: A set of header options sent to the server. Defaults to an empty set.
22+
- returns: A publisher that eventually produces a single value and then finishes or fails.
23+
*/
24+
static func checkPublisher(options: API.Options = []) -> Future<String, ParseError> {
25+
Future { promise in
26+
Self.check(options: options,
27+
completion: promise)
28+
}
29+
}
30+
}
31+
#endif
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// ParseHealth.swift
3+
// ParseSwift
4+
//
5+
// Created by Corey Baker on 4/28/21.
6+
// Copyright © 2021 Parse Community. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
/**
12+
`ParseHealth` allows you to check the health of a Parse Server.
13+
*/
14+
public struct ParseHealth: ParseType, Decodable {
15+
16+
/**
17+
Calls the health check function *synchronously* and returns a result of it's execution.
18+
- parameter options: A set of header options sent to the server. Defaults to an empty set.
19+
- returns: Returns the status of the server.
20+
- throws: An error of type `ParseError`.
21+
*/
22+
static public func check(options: API.Options = []) throws -> String {
23+
try healthCommand().execute(options: options)
24+
}
25+
26+
/**
27+
Calls the health check function *asynchronously* and returns a result of it's execution.
28+
- parameter options: A set of header options sent to the server. Defaults to an empty set.
29+
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
30+
- parameter completion: A block that will be called when the health check completes or fails.
31+
It should have the following argument signature: `(Result<String, ParseError>)`.
32+
*/
33+
static public func check(options: API.Options = [],
34+
callbackQueue: DispatchQueue = .main,
35+
completion: @escaping (Result<String, ParseError>) -> Void) {
36+
healthCommand()
37+
.executeAsync(options: options) { result in
38+
callbackQueue.async {
39+
completion(result)
40+
}
41+
}
42+
}
43+
44+
internal static func healthCommand() -> API.NonParseBodyCommand<NoBody, String> {
45+
return API.NonParseBodyCommand(method: .POST,
46+
path: .health) { (data) -> String in
47+
return try ParseCoding.jsonDecoder().decode(HealthResponse.self, from: data).status
48+
}
49+
}
50+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//
2+
// ParseHealthCombineTests.swift
3+
// ParseSwift
4+
//
5+
// Created by Corey Baker on 4/28/21.
6+
// Copyright © 2021 Parse Community. All rights reserved.
7+
//
8+
9+
#if canImport(Combine)
10+
11+
import Foundation
12+
import XCTest
13+
import Combine
14+
@testable import ParseSwift
15+
16+
@available(macOS 10.15, iOS 13.0, macCatalyst 13.0, watchOS 6.0, tvOS 13.0, *)
17+
class ParseHealthCombineTests: XCTestCase {
18+
override func setUpWithError() throws {
19+
try super.setUpWithError()
20+
guard let url = URL(string: "http://localhost:1337/1") else {
21+
XCTFail("Should create valid URL")
22+
return
23+
}
24+
ParseSwift.initialize(applicationId: "applicationId",
25+
clientKey: "clientKey",
26+
masterKey: "masterKey",
27+
serverURL: url,
28+
testing: true)
29+
}
30+
31+
override func tearDownWithError() throws {
32+
try super.tearDownWithError()
33+
MockURLProtocol.removeAll()
34+
#if !os(Linux) && !os(Android)
35+
try KeychainStore.shared.deleteAll()
36+
#endif
37+
try ParseStorage.shared.deleteAll()
38+
}
39+
40+
func testCheck() {
41+
var subscriptions = Set<AnyCancellable>()
42+
let expectation1 = XCTestExpectation(description: "Save")
43+
44+
let healthOfServer = "ok"
45+
let serverResponse = HealthResponse(status: healthOfServer)
46+
let encoded: Data!
47+
do {
48+
encoded = try ParseCoding.jsonEncoder().encode(serverResponse)
49+
} catch {
50+
XCTFail("Should encode/decode. Error \(error)")
51+
return
52+
}
53+
54+
MockURLProtocol.mockRequests { _ in
55+
return MockURLResponse(data: encoded, statusCode: 200, delay: 0.0)
56+
}
57+
58+
let publisher = ParseHealth.checkPublisher()
59+
.sink(receiveCompletion: { result in
60+
61+
if case let .failure(error) = result {
62+
XCTFail(error.localizedDescription)
63+
}
64+
expectation1.fulfill()
65+
66+
}, receiveValue: { health in
67+
XCTAssertEqual(health, healthOfServer)
68+
})
69+
publisher.store(in: &subscriptions)
70+
71+
wait(for: [expectation1], timeout: 20.0)
72+
}
73+
}
74+
#endif

0 commit comments

Comments
 (0)