Skip to content

Commit

Permalink
New Feature: Health Check (#134)
Browse files Browse the repository at this point in the history
* New Feature: Health Check

* Fix test for linux comparison

* Simplify error check for linux

* Remove debugDescription in ParseHealth
  • Loading branch information
cbaker6 committed Apr 28, 2021
1 parent 2704ee9 commit a7a6f40
Show file tree
Hide file tree
Showing 17 changed files with 350 additions and 21 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Parse-Swift Changelog

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

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

__New features__
- 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).

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ npm start -- --appId applicationId --clientKey clientKey --masterKey masterKey -

initializeParse()

//: Check the health of your Parse Server.
print(try ParseHealth.check())

//: Create your own value typed `ParseObject`.
struct GameScore: ParseObject {
//: Those are required for Object
Expand Down
2 changes: 1 addition & 1 deletion ParseSwift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "ParseSwift"
s.version = "1.7.0"
s.version = "1.7.1"
s.summary = "Parse Pure Swift SDK"
s.homepage = "https://github.com/parse-community/Parse-Swift"
s.authors = {
Expand Down
52 changes: 44 additions & 8 deletions ParseSwift.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Scripts/jazzy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ bundle exec jazzy \
--author_url http://parseplatform.org \
--github_url https://github.com/parse-community/Parse-Swift \
--root-url http://parseplatform.org/Parse-Swift/api/ \
--module-version 1.7.0 \
--module-version 1.7.1 \
--theme fullwidth \
--skip-undocumented \
--output ./docs/api \
Expand Down
3 changes: 3 additions & 0 deletions Sources/ParseSwift/API/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public struct API {
case jobs(name: String)
case aggregate(className: String)
case config
case health
case any(String)

var urlComponent: String {
Expand Down Expand Up @@ -80,6 +81,8 @@ public struct API {
return "/aggregate/\(className)"
case .config:
return "/config"
case .health:
return "/health"
case .any(let path):
return path
}
Expand Down
5 changes: 5 additions & 0 deletions Sources/ParseSwift/API/Responses.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,8 @@ internal struct ConfigFetchResponse<T>: Codable where T: ParseConfig {
internal struct ConfigUpdateResponse: Codable {
let result: Bool
}

// MARK: HealthResponse
internal struct HealthResponse: Codable {
let status: String
}
2 changes: 1 addition & 1 deletion Sources/ParseSwift/Objects/ParseUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ extension ParseUser {
unless your code is already running from a background thread.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
- parameter completion: A block that will be called when logging out, completes or fails.
- parameter completion: A block that will be called when logging out completes or fails.
*/
public static func logout(options: API.Options = [], callbackQueue: DispatchQueue = .main,
completion: @escaping (Result<Void, ParseError>) -> Void) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/ParseSwift/ParseConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

enum ParseConstants {
static let parseVersion = "1.7.0"
static let parseVersion = "1.7.1"
static let hashingKey = "parseSwift"
static let fileManagementDirectory = "parse/"
static let fileManagementPrivateDocumentsDirectory = "Private Documents/"
Expand Down
2 changes: 1 addition & 1 deletion Sources/ParseSwift/Types/ParseCloud+combine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public extension ParseCloud {
// MARK: Jobs - Combine

/**
Starts a Cloud Code job *asynchronously* and returns a result with the jobStatusId of the job.
Starts a Cloud Code Job *asynchronously* and returns a result with the jobStatusId of the job.
Publishes when complete.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- returns: A publisher that eventually produces a single value and then finishes or fails.
Expand Down
8 changes: 4 additions & 4 deletions Sources/ParseSwift/Types/ParseCloud.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extension ParseCloud {
Calls a Cloud Code function *asynchronously* and returns a result of it's execution.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
- parameter completion: A block that will be called when logging out, completes or fails.
- parameter completion: A block that will be called when the Cloud Code completes or fails.
It should have the following argument signature: `(Result<ReturnType, ParseError>)`.
*/
public func runFunction(options: API.Options = [],
Expand Down Expand Up @@ -70,7 +70,7 @@ extension ParseCloud {
// MARK: Jobs
extension ParseCloud {
/**
Starts a Cloud Code job *synchronously* and returns a result with the jobStatusId of the job.
Starts a Cloud Code Job *synchronously* and returns a result with the jobStatusId of the job.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- returns: Returns a `Decodable` type.
*/
Expand All @@ -79,10 +79,10 @@ extension ParseCloud {
}

/**
Starts a Cloud Code job *asynchronously* and returns a result with the jobStatusId of the job.
Starts a Cloud Code Job *asynchronously* and returns a result with the jobStatusId of the job.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
- parameter completion: A block that will be called when logging out, completes or fails.
- parameter completion: A block that will be called when the Cloud Code Job completes or fails.
It should have the following argument signature: `(Result<ReturnType, ParseError>)`.
*/
public func startJob(options: API.Options = [],
Expand Down
2 changes: 1 addition & 1 deletion Sources/ParseSwift/Types/ParseConfig+combine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public extension ParseConfig {
// MARK: Fetchable - Combine

/**
Fetch the Config *asynchronously*.
Fetch the Config *asynchronously*. Publishes when complete.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- returns: A publisher that eventually produces a single value and then finishes or fails.
*/
Expand Down
4 changes: 2 additions & 2 deletions Sources/ParseSwift/Types/ParseConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extension ParseConfig {
Fetch the Config *asynchronously*.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
- parameter completion: A block that will be called when logging out, completes or fails.
- parameter completion: A block that will be called when retrieving the config completes or fails.
It should have the following argument signature: `(Result<Self, ParseError>)`.
*/
public func fetch(options: API.Options = [],
Expand Down Expand Up @@ -73,7 +73,7 @@ extension ParseConfig {
Update the Config *asynchronously*.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
- parameter completion: A block that will be called when logging out, completes or fails.
- parameter completion: A block that will be called when retrieving the config completes or fails.
It should have the following argument signature: `(Result<Bool, ParseError>)`.
*/
public func save(options: API.Options = [],
Expand Down
31 changes: 31 additions & 0 deletions Sources/ParseSwift/Types/ParseHealth+Combine.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// ParseHealth+Combine.swift
// ParseSwift
//
// Created by Corey Baker on 4/28/21.
// Copyright © 2021 Parse Community. All rights reserved.
//

#if canImport(Combine)
import Foundation
import Combine

// MARK: Combine
@available(macOS 10.15, iOS 13.0, macCatalyst 13.0, watchOS 6.0, tvOS 13.0, *)
public extension ParseHealth {

// MARK: Check - Combine

/**
Calls the health check function *asynchronously*. Publishes when complete.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- returns: A publisher that eventually produces a single value and then finishes or fails.
*/
static func checkPublisher(options: API.Options = []) -> Future<String, ParseError> {
Future { promise in
Self.check(options: options,
completion: promise)
}
}
}
#endif
50 changes: 50 additions & 0 deletions Sources/ParseSwift/Types/ParseHealth.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// ParseHealth.swift
// ParseSwift
//
// Created by Corey Baker on 4/28/21.
// Copyright © 2021 Parse Community. All rights reserved.
//

import Foundation

/**
`ParseHealth` allows you to check the health of a Parse Server.
*/
public struct ParseHealth: ParseType, Decodable {

/**
Calls the health check function *synchronously* and returns a result of it's execution.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- returns: Returns the status of the server.
- throws: An error of type `ParseError`.
*/
static public func check(options: API.Options = []) throws -> String {
try healthCommand().execute(options: options)
}

/**
Calls the health check function *asynchronously* and returns a result of it's execution.
- parameter options: A set of header options sent to the server. Defaults to an empty set.
- parameter callbackQueue: The queue to return to after completion. Default value of .main.
- parameter completion: A block that will be called when the health check completes or fails.
It should have the following argument signature: `(Result<String, ParseError>)`.
*/
static public func check(options: API.Options = [],
callbackQueue: DispatchQueue = .main,
completion: @escaping (Result<String, ParseError>) -> Void) {
healthCommand()
.executeAsync(options: options) { result in
callbackQueue.async {
completion(result)
}
}
}

internal static func healthCommand() -> API.NonParseBodyCommand<NoBody, String> {
return API.NonParseBodyCommand(method: .POST,
path: .health) { (data) -> String in
return try ParseCoding.jsonDecoder().decode(HealthResponse.self, from: data).status
}
}
}
74 changes: 74 additions & 0 deletions Tests/ParseSwiftTests/ParseHealthCombineTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// ParseHealthCombineTests.swift
// ParseSwift
//
// Created by Corey Baker on 4/28/21.
// Copyright © 2021 Parse Community. All rights reserved.
//

#if canImport(Combine)

import Foundation
import XCTest
import Combine
@testable import ParseSwift

@available(macOS 10.15, iOS 13.0, macCatalyst 13.0, watchOS 6.0, tvOS 13.0, *)
class ParseHealthCombineTests: XCTestCase {
override func setUpWithError() throws {
try super.setUpWithError()
guard let url = URL(string: "http://localhost:1337/1") else {
XCTFail("Should create valid URL")
return
}
ParseSwift.initialize(applicationId: "applicationId",
clientKey: "clientKey",
masterKey: "masterKey",
serverURL: url,
testing: true)
}

override func tearDownWithError() throws {
try super.tearDownWithError()
MockURLProtocol.removeAll()
#if !os(Linux) && !os(Android)
try KeychainStore.shared.deleteAll()
#endif
try ParseStorage.shared.deleteAll()
}

func testCheck() {
var subscriptions = Set<AnyCancellable>()
let expectation1 = XCTestExpectation(description: "Save")

let healthOfServer = "ok"
let serverResponse = HealthResponse(status: healthOfServer)
let encoded: Data!
do {
encoded = try ParseCoding.jsonEncoder().encode(serverResponse)
} catch {
XCTFail("Should encode/decode. Error \(error)")
return
}

MockURLProtocol.mockRequests { _ in
return MockURLResponse(data: encoded, statusCode: 200, delay: 0.0)
}

let publisher = ParseHealth.checkPublisher()
.sink(receiveCompletion: { result in

if case let .failure(error) = result {
XCTFail(error.localizedDescription)
}
expectation1.fulfill()

}, receiveValue: { health in
XCTAssertEqual(health, healthOfServer)
})
publisher.store(in: &subscriptions)

wait(for: [expectation1], timeout: 20.0)
}
}
#endif
Loading

0 comments on commit a7a6f40

Please sign in to comment.