Skip to content

Commit

Permalink
feat: add Parse hook functions and triggers (#373)
Browse files Browse the repository at this point in the history
* 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
cbaker6 authored Jun 24, 2022
1 parent 36f9d1a commit 97885ce
Show file tree
Hide file tree
Showing 97 changed files with 4,654 additions and 675 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

### main

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

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

__New features__
- Add the ability to use Parse Hooks and Triggers ([#373](https://github.com/parse-community/Parse-Swift/pull/373)), thanks to [Corey Baker](https://github.com/cbaker6).
- Add ParseInstagram authentication ([#372](https://github.com/parse-community/Parse-Swift/pull/372)), thanks to [Ulaş Sancak](https://github.com/rocxteady).
- Add the ability to send APN and FCM push notifications. Also adds the ability to query _PushStatus ([#371](https://github.com/parse-community/Parse-Swift/pull/371)), thanks to [Corey Baker](https://github.com/cbaker6).
- Add ParseSchema, ParseCLP, and ParseFieldOptions. Should only be used when using the Swift SDK on a secured server ([#370](https://github.com/parse-community/Parse-Swift/pull/370)), thanks to [Corey Baker](https://github.com/cbaker6).
- Add ParseInstagram authentication ([#372](https://github.com/parse-community/Parse-Swift/pull/372)), thanks to [Ulaş Sancak](https://github.com/rocxteady).

### 4.5.0
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/4.4.0...4.5.0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import Foundation
import ParseSwift
PlaygroundPage.current.needsIndefiniteExecution = true

/*: start parse-server with
npm start -- --appId applicationId --clientKey clientKey --masterKey masterKey --mountPath /1
/*:
start parse-server with
npm start -- --appId applicationId --clientKey clientKey --masterKey masterKey --mountPath /1
*/

/*: In Xcode, make sure you are building the "ParseSwift (macOS)" framework.
/*:
In Xcode, make sure you are building the "ParseSwift (macOS)" framework.
*/

initializeParseCustomObjectId()
initializeParse(customObjectId: true)

//: Create your own value typed `ParseObject`.
struct GameScore: ParseObject {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
//: [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
Expand Down Expand Up @@ -80,8 +86,10 @@ struct GameScore2: ParseObject {
}
}

//: It's recommended to place custom initializers in an extension
//: to preserve the memberwise initializer.
/*:
It's recommended to place custom initializers in an extension
to preserve the memberwise initializer.
*/
extension GameScore2 {

init(points: Int) {
Expand Down Expand Up @@ -170,6 +178,16 @@ gameScoreSchema.update { result in
}
}

//: We can also fetch the schema.
gameScoreSchema.fetch { result in
switch result {
case .success(let fetchedGameScore):
print("The fetched GameScore2 schema is: \(fetchedGameScore)")
case .failure(let error):
print("Couldn't fetch schema: \(error)")
}
}

/*:
Fields can also be deleted on a schema. Lets remove
the **data** field since it's not going being used.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
//: [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
Expand Down Expand Up @@ -42,7 +48,7 @@ struct Installation: ParseInstallation {
}
}

/**
/*:
We will begin by creating the payload information we want to
send in the push notification.
*/
Expand Down
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)
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)
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,17 @@ installationToUpdate?.save { results in
}
}

//: You can fetch your installation at anytime.
Installation.current?.fetch { results in

switch results {
case .success(let fetchedInstallation):
print("Successfully fetched installation from ParseServer: \(fetchedInstallation)")
case .failure(let error):
print("Failed to fetch installation: \(error)")
}

}

PlaygroundPage.current.finishExecution()
//: [Next](@next)
12 changes: 2 additions & 10 deletions ParseSwift.playground/Sources/Common.swift
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)
}
Loading

0 comments on commit 97885ce

Please sign in to comment.