-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Parse hook functions and triggers (#373)
* feat: add Parse hook capabilities * add ParseTypeable * switch CloudType to ParseCloudTypeable * refactor * Add working playgrounds for functions * update change log * add hook key * add hook trigger * remove webhookKey and fix docs * add tests and more docs * more codecov * codecov * make ParseHookResponse accept ParseError's * improve ParseHookResponse * fix playground * triggers work with ParseFile * testing file * Parse Server Hooks doesn't support triggers for file and beforeConnect * Update changelog * Make methods public * clean up playgrounds * nit
- Loading branch information
Showing
97 changed files
with
4,654 additions
and
675 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
ParseSwift.playground/Pages/22 - Cloud Hook Functions.xcplaygroundpage/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
//: [Previous](@previous) | ||
|
||
/*: | ||
The code in this Playground is intended to run at the | ||
server level only. It is not intended to be run in client | ||
applications as it requires the use of the master key. | ||
*/ | ||
|
||
import PlaygroundSupport | ||
import Foundation | ||
import ParseSwift | ||
|
||
PlaygroundPage.current.needsIndefiniteExecution = true | ||
initializeParse() | ||
|
||
/*: | ||
Parse Hook Functions can be created by conforming to | ||
`ParseHookFunctionable`. | ||
*/ | ||
struct HookFunction: ParseHookFunctionable { | ||
var functionName: String? | ||
var url: URL? | ||
} | ||
|
||
/*: | ||
Lets create our first Hook function by first creating an instance | ||
with the name of the function and url for the hook. | ||
*/ | ||
var myFunction = HookFunction(name: "foo", | ||
url: URL(string: "https://api.example.com/foo")) | ||
|
||
//: Then, create the function on the server. | ||
myFunction.create { result in | ||
switch result { | ||
case .success(let newFunction): | ||
print("Created: \"\(newFunction)\"") | ||
case .failure(let error): | ||
print("Could not create: \(error)") | ||
} | ||
} | ||
|
||
/*: | ||
The function can be fetched at any time. | ||
*/ | ||
myFunction.fetch { result in | ||
switch result { | ||
case .success(let fetchedFunction): | ||
print("Fetched: \"\(fetchedFunction)\"") | ||
case .failure(let error): | ||
print("Could not fetch: \(error)") | ||
} | ||
} | ||
|
||
/*: | ||
There will be times you need to update a Hook function. | ||
You can update your hook at anytime. | ||
*/ | ||
myFunction.url = URL(string: "https://api.example.com/bar") | ||
myFunction.update { result in | ||
switch result { | ||
case .success(let updated): | ||
print("Updated: \"\(updated)\"") | ||
case .failure(let error): | ||
print("Could not update: \(error)") | ||
} | ||
} | ||
|
||
/*: | ||
Lets fetchAll using the instance method to see all of the | ||
available hook functions. | ||
*/ | ||
myFunction.fetchAll { result in | ||
switch result { | ||
case .success(let functions): | ||
print("Current: \"\(functions)\"") | ||
case .failure(let error): | ||
print("Could not fetch: \(error)") | ||
} | ||
} | ||
|
||
/*: | ||
Hook functions can also be deleted. | ||
*/ | ||
myFunction.delete { result in | ||
switch result { | ||
case .success: | ||
print("The Parse Cloud function was deleted successfully") | ||
case .failure(let error): | ||
print("Could not delete: \(error)") | ||
} | ||
} | ||
|
||
/*: | ||
You can also use the fetchAll type method to fetch all of | ||
the current Hook functions. | ||
*/ | ||
HookFunction.fetchAll { result in | ||
switch result { | ||
case .success(let functions): | ||
print("Current: \"\(functions)\"") | ||
case .failure(let error): | ||
print("Could not fetch: \(error)") | ||
} | ||
} | ||
|
||
PlaygroundPage.current.finishExecution() | ||
//: [Next](@next) |
147 changes: 147 additions & 0 deletions
147
ParseSwift.playground/Pages/23 - Cloud Hook Triggers.xcplaygroundpage/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
//: [Previous](@previous) | ||
|
||
/*: | ||
The code in this Playground is intended to run at the | ||
server level only. It is not intended to be run in client | ||
applications as it requires the use of the master key. | ||
*/ | ||
|
||
import PlaygroundSupport | ||
import Foundation | ||
import ParseSwift | ||
|
||
PlaygroundPage.current.needsIndefiniteExecution = true | ||
initializeParse() | ||
|
||
//: Create your own value typed `ParseObject`. | ||
struct GameScore: ParseObject { | ||
//: These are required by ParseObject | ||
var objectId: String? | ||
var createdAt: Date? | ||
var updatedAt: Date? | ||
var ACL: ParseACL? | ||
var originalData: Data? | ||
|
||
//: Your own properties. | ||
var points: Int? | ||
|
||
//: Implement your own version of merge | ||
func merge(with object: Self) throws -> Self { | ||
var updated = try mergeParse(with: object) | ||
if updated.shouldRestoreKey(\.points, | ||
original: object) { | ||
updated.points = object.points | ||
} | ||
return updated | ||
} | ||
} | ||
|
||
//: It's recommended to place custom initializers in an extension | ||
//: to preserve the memberwise initializer. | ||
extension GameScore { | ||
|
||
init(points: Int) { | ||
self.points = points | ||
} | ||
|
||
init(objectId: String?) { | ||
self.objectId = objectId | ||
} | ||
} | ||
|
||
/*: | ||
Parse Hook Triggers can be created by conforming to | ||
`ParseHookFunctionable`. | ||
*/ | ||
struct HookTrigger: ParseHookTriggerable { | ||
var className: String? | ||
var triggerName: ParseHookTriggerType? | ||
var url: URL? | ||
} | ||
|
||
/*: | ||
Lets create our first Hook trigger by first creating an instance | ||
with the name of the trigger and url for the hook. | ||
*/ | ||
let gameScore = GameScore() | ||
var myTrigger = HookTrigger(object: gameScore, | ||
triggerName: .afterSave, | ||
url: URL(string: "https://api.example.com/bar")!) | ||
|
||
//: Then, create the trigger on the server. | ||
myTrigger.create { result in | ||
switch result { | ||
case .success(let newFunction): | ||
print("Created: \"\(newFunction)\"") | ||
case .failure(let error): | ||
print("Could not create: \(error)") | ||
} | ||
} | ||
|
||
/*: | ||
The trigger can be fetched at any time. | ||
*/ | ||
myTrigger.fetch { result in | ||
switch result { | ||
case .success(let fetchedFunction): | ||
print("Fetched: \"\(fetchedFunction)\"") | ||
case .failure(let error): | ||
print("Could not fetch: \(error)") | ||
} | ||
} | ||
|
||
/*: | ||
There will be times you need to update a Hook trigger. | ||
You can update your hook at anytime. | ||
*/ | ||
myTrigger.url = URL(string: "https://api.example.com/car") | ||
myTrigger.update { result in | ||
switch result { | ||
case .success(let updated): | ||
print("Updated: \"\(updated)\"") | ||
case .failure(let error): | ||
print("Could not update: \(error)") | ||
} | ||
} | ||
|
||
/*: | ||
Lets fetchAll using the instance method to see all of the | ||
available hook triggers. | ||
*/ | ||
myTrigger.fetchAll { result in | ||
switch result { | ||
case .success(let triggers): | ||
print("Current: \"\(triggers)\"") | ||
case .failure(let error): | ||
print("Could not fetch: \(error)") | ||
} | ||
} | ||
|
||
/*: | ||
Hook triggers can also be deleted. | ||
*/ | ||
myTrigger.delete { result in | ||
switch result { | ||
case .success: | ||
print("The Parse Cloud trigger was deleted successfully") | ||
case .failure(let error): | ||
print("Could not delete: \(error)") | ||
} | ||
} | ||
|
||
/*: | ||
You can also use the fetchAll type method to fetch all of | ||
the current Hook triggers. | ||
*/ | ||
HookTrigger.fetchAll { result in | ||
switch result { | ||
case .success(let triggers): | ||
print("Current: \"\(triggers)\"") | ||
case .failure(let error): | ||
print("Could not fetch: \(error)") | ||
} | ||
} | ||
|
||
PlaygroundPage.current.finishExecution() | ||
|
||
//: [Next](@next) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,11 @@ | ||
import Foundation | ||
import ParseSwift | ||
|
||
public func initializeParse() { | ||
public func initializeParse(customObjectId: Bool = false) { | ||
ParseSwift.initialize(applicationId: "applicationId", | ||
clientKey: "clientKey", | ||
masterKey: "masterKey", | ||
serverURL: URL(string: "http://localhost:1337/1")!, | ||
usingTransactions: false, | ||
usingEqualQueryConstraint: false) | ||
} | ||
|
||
public func initializeParseCustomObjectId() { | ||
ParseSwift.initialize(applicationId: "applicationId", | ||
clientKey: "clientKey", | ||
serverURL: URL(string: "http://localhost:1337/1")!, | ||
allowingCustomObjectIds: true, | ||
allowingCustomObjectIds: customObjectId, | ||
usingEqualQueryConstraint: false) | ||
} |
Oops, something went wrong.