Skip to content

Commit

Permalink
feat: Use new ParseHookFunction and ParseHookTrigger types (#43)
Browse files Browse the repository at this point in the history
* feat: Use new ParseHookFunction and ParseHookTrigger types

* lint
  • Loading branch information
cbaker6 authored Jun 24, 2023
1 parent 7774338 commit be2c6dd
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 81 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/netreconlab/Parse-Swift.git",
"state" : {
"revision" : "835083e870b20a7a999b49e5ed3f57c0dc442e89",
"version" : "5.7.4"
"revision" : "66627b55b558698f50220eaae4cea7057cd14450",
"version" : "5.8.0"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", .upToNextMajor(from: "4.77.0")),
.package(url: "https://github.com/netreconlab/Parse-Swift.git",
.upToNextMajor(from: "5.7.4"))
.upToNextMajor(from: "5.8.0"))
],
targets: [
.target(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// HookFunction.swift
// ParseHookFunction+Vapor.swift
//
//
// Created by Corey Baker on 6/23/22.
Expand All @@ -9,29 +9,18 @@ import Foundation
import ParseSwift
import Vapor

/**
Parse Hook Functions can be created by conforming to
`ParseHookFunctionable`.
*/
public struct HookFunction: ParseHookFunctionable {
public var functionName: String?
public var url: URL?

public init() {}
}

// MARK: HookFunction - Internal
extension HookFunction {
extension ParseHookFunction {

@discardableResult
static func method(_ method: HTTPMethod,
_ path: [PathComponent],
name: String,
parseServerURLStrings: [String]) async throws -> [String: HookFunction] {
parseServerURLStrings: [String]) async throws -> [String: Self] {
let url = try buildServerPathname(path)
let hookFunction = HookFunction(name: name,
url: url)
var hookFunctions = [String: HookFunction]()
let hookFunction = Self(name: name,
url: url)
var hookFunctions = [String: Self]()

for parseServerURLString in parseServerURLStrings {
do {
Expand Down Expand Up @@ -79,7 +68,7 @@ extension HookFunction {
}

// MARK: HookFunction - Fetch
public extension HookFunction {
public extension ParseHookFunction {

/**
Fetches a Parse Cloud Code hook function.
Expand All @@ -93,7 +82,7 @@ public extension HookFunction {
*/
static func fetch(_ path: PathComponent...,
name: String,
parseServerURLStrings: [String]) async throws -> [String: HookFunction] {
parseServerURLStrings: [String]) async throws -> [String: Self] {
try await fetch(path, name: name, parseServerURLStrings: parseServerURLStrings)
}

Expand All @@ -109,7 +98,7 @@ public extension HookFunction {
*/
static func fetch(_ path: [PathComponent],
name: String,
parseServerURLStrings: [String]) async throws -> [String: HookFunction] {
parseServerURLStrings: [String]) async throws -> [String: Self] {
try await method(.PUT, path, name: name, parseServerURLStrings: parseServerURLStrings)
}

Expand All @@ -125,7 +114,7 @@ public extension HookFunction {
*/
static func fetchAll(_ path: PathComponent...,
name: String,
parseServerURLStrings: [String]) async throws -> [String: [HookFunction]] {
parseServerURLStrings: [String]) async throws -> [String: [Self]] {
try await fetchAll(path, name: name, parseServerURLStrings: parseServerURLStrings)
}

Expand All @@ -141,11 +130,11 @@ public extension HookFunction {
*/
static func fetchAll(_ path: [PathComponent],
name: String,
parseServerURLStrings: [String]) async throws -> [String: [HookFunction]] {
parseServerURLStrings: [String]) async throws -> [String: [Self]] {
let url = try buildServerPathname(path)
let hookFunction = HookFunction(name: name,
let hookFunction = Self(name: name,
url: url)
var hookFunctions = [String: [HookFunction]]()
var hookFunctions = [String: [Self]]()

for parseServerURLString in parseServerURLStrings {
do {
Expand All @@ -161,7 +150,7 @@ public extension HookFunction {
}

// MARK: HookFunction - Create
public extension HookFunction {
public extension ParseHookFunction {

/**
Creates a Parse Cloud Code hook function.
Expand All @@ -177,7 +166,7 @@ public extension HookFunction {
static func create(_ path: PathComponent...,
name: String,
// swiftlint:disable:next line_length
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: HookFunction] {
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: Self] {
try await create(path, name: name, parseServerURLStrings: parseServerURLStrings)
}

Expand All @@ -195,13 +184,13 @@ public extension HookFunction {
static func create(_ path: [PathComponent],
name: String,
// swiftlint:disable:next line_length
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: HookFunction] {
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: Self] {
try await method(.POST, path, name: name, parseServerURLStrings: parseServerURLStrings)
}
}

// MARK: HookFunction - Update
public extension HookFunction {
public extension ParseHookFunction {

/**
Updates a Parse Cloud Code hook function.
Expand All @@ -217,7 +206,7 @@ public extension HookFunction {
static func update(_ path: PathComponent...,
name: String,
// swiftlint:disable:next line_length
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: HookFunction] {
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: Self] {
try await update(path, name: name, parseServerURLStrings: parseServerURLStrings)
}

Expand All @@ -235,13 +224,13 @@ public extension HookFunction {
static func update(_ path: [PathComponent],
name: String,
// swiftlint:disable:next line_length
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: HookFunction] {
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: Self] {
try await method(.PUT, path, name: name, parseServerURLStrings: parseServerURLStrings)
}
}

// MARK: HookFunction - Delete
public extension HookFunction {
public extension ParseHookFunction {

/**
Removes a Parse Cloud Code hook function.
Expand Down Expand Up @@ -359,8 +348,8 @@ public extension RoutesBuilder {
let route = self.on(.POST, path, body: body, use: closure)
Task {
do {
await configuration.hooks.updateFunctions(try await HookFunction.create(route.path,
name: name))
await configuration.hooks.updateFunctions(try await ParseHookFunction.create(route.path,
name: name))
} catch {
// swiftlint:disable:next line_length
configuration.logger.error("Could not create HookFunction route for path: \(path); name: \(name) on servers: \(configuration.parseServerURLStrings) because of error: \(error)")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// HookTrigger.swift
// ParseHookTrigger+Vapor.swift
//
//
// Created by Corey Baker on 6/23/22.
Expand All @@ -9,20 +9,8 @@ import Foundation
import ParseSwift
import Vapor

/**
Parse Hook Triggers can be created by conforming to
`ParseHookTriggerable`.
*/
public struct HookTrigger: ParseHookTriggerable {
public var className: String?
public var triggerName: ParseHookTriggerType?
public var url: URL?

public init() {}
}

// MARK: HookTrigger - Internal
extension HookTrigger {
extension ParseHookTrigger {

@discardableResult
static func method(_ method: HTTPMethod,
Expand All @@ -31,16 +19,16 @@ extension HookTrigger {
trigger: ParseHookTriggerType,
parseServerURLStrings: [String]) async throws -> [String: Self] {
let url = try buildServerPathname(path)
let hookTrigger: HookTrigger!
let hookTrigger: Self!
var hookTriggers = [String: Self]()

if let className = className {
hookTrigger = HookTrigger(className: className,
triggerName: trigger,
url: url)
hookTrigger = Self(className: className,
trigger: trigger,
url: url)
} else {
hookTrigger = try HookTrigger(triggerName: trigger,
url: url)
hookTrigger = try Self(trigger: trigger,
url: url)
}

for parseServerURLString in parseServerURLStrings {
Expand Down Expand Up @@ -90,7 +78,7 @@ extension HookTrigger {
}

// MARK: HookTrigger - Fetch
public extension HookTrigger {
public extension ParseHookTrigger {

/**
Fetch a Parse Cloud Code hook trigger.
Expand Down Expand Up @@ -392,16 +380,16 @@ public extension HookTrigger {
// swiftlint:disable:next line_length
parseServerURLStrings: [String] = ParseServerSwift.configuration.parseServerURLStrings) async throws -> [String: [Self]] {
let url = try buildServerPathname(path)
let hookTrigger: HookTrigger!
let hookTrigger: Self!
var hookTriggers = [String: [Self]]()

if let className = className {
hookTrigger = Self(className: className,
triggerName: trigger,
trigger: trigger,
url: url)
} else {
hookTrigger = try HookTrigger(triggerName: trigger,
url: url)
hookTrigger = try Self(trigger: trigger,
url: url)
}
for parseServerURLString in parseServerURLStrings {
do {
Expand All @@ -417,7 +405,7 @@ public extension HookTrigger {
}

// MARK: HookTrigger - Create
public extension HookTrigger {
public extension ParseHookTrigger {

/**
Create a Parse Cloud Code hook trigger.
Expand Down Expand Up @@ -575,7 +563,7 @@ public extension HookTrigger {
}

// MARK: HookTrigger - Update
public extension HookTrigger {
public extension ParseHookTrigger {

/**
Update a Parse Cloud Code hook trigger.
Expand Down Expand Up @@ -733,7 +721,7 @@ public extension HookTrigger {
}

// MARK: HookTrigger - Delete
public extension HookTrigger {
public extension ParseHookTrigger {

/**
Delete a Parse Cloud Code hook trigger.
Expand Down Expand Up @@ -1196,9 +1184,9 @@ public extension RoutesBuilder {
let route = self.on(.POST, path, body: body, use: closure)
Task {
do {
await configuration.hooks.updateTriggers(try await HookTrigger.create(path,
className: className,
trigger: trigger))
await configuration.hooks.updateTriggers(try await ParseHookTrigger.create(path,
className: className,
trigger: trigger))
} catch {
if let className = className {
// swiftlint:disable:next line_length
Expand Down
18 changes: 10 additions & 8 deletions Sources/ParseServerSwift/Models/Hooks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@
//

import Foundation
import ParseSwift

/// An actor containing all of the current Hooks.
actor Hooks {
var functions = [String: HookFunction]()
var triggers = [String: HookTrigger]()
var functions = [String: ParseHookFunction]()
var triggers = [String: ParseHookTrigger]()
}

// MARK: Hook Functions
extension Hooks {
/// Get all of the current functions.
/// - returns: A dictionary where the keys are Parse Server `URL`'s and the respective `HookFunction`.
func getFunctions() -> [String: HookFunction] {
func getFunctions() -> [String: ParseHookFunction] {
functions
}

/// Update curent functions.
/// - parameter functions: A dictionary where the keys are Parse Server `URL`'s and the respective `HookFunction`.
func updateFunctions(_ functions: [String: HookFunction]) {
func updateFunctions(_ functions: [String: ParseHookFunction]) {
for (url, function) in functions {
self.functions[url] = function
}
Expand All @@ -46,14 +47,15 @@ extension Hooks {
// MARK: Hook Triggers
extension Hooks {
/// Get all of the current triggers.
/// - returns: A dictionary where the keys are Parse Server `URL`'s and the respective `HookTrigger`.
func getTriggers() -> [String: HookTrigger] {
/// - returns: A dictionary where the keys are Parse Server `URL`'s and the respective `ParseHookTrigger`.
func getTriggers() -> [String: ParseHookTrigger] {
triggers
}

/// Update curent triggers.
/// - parameter triggers: A dictionary where the keys are Parse Server `URL`'s and the respective `HookTrigger`.
func updateTriggers(_ triggers: [String: HookTrigger]) {
/// - parameter triggers: A dictionary where the keys are Parse Server `URL`'s and the
/// respective `ParseHookTrigger`.
func updateTriggers(_ triggers: [String: ParseHookTrigger]) {
for (url, trigger) in triggers {
self.triggers[url] = trigger
}
Expand Down
16 changes: 10 additions & 6 deletions Tests/ParseServerSwiftTests/AppTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ final class AppTests: XCTestCase {
}
XCTAssertGreaterThan(configuration.parseServerURLStrings.count, 0)

let function = HookFunction(name: "hello", url: url)
let trigger = try HookTrigger(triggerName: .afterSave, url: url)
let function = ParseHookFunction(name: "hello", url: url)
let trigger = try ParseHookTrigger(trigger: .afterSave, url: url)

await configuration.hooks.updateFunctions([ urlString: function ])
await configuration.hooks.updateTriggers([ urlString: trigger ])
Expand Down Expand Up @@ -214,8 +214,8 @@ final class AppTests: XCTestCase {
let functions = await configuration.hooks.getFunctions()
XCTAssertTrue(functions.isEmpty)

let dummyHooks = ["yo": HookFunction(name: "hello", url: nil),
"no": HookFunction(name: "hello", url: nil)]
let dummyHooks = ["yo": ParseHookFunction(name: "hello", url: nil),
"no": ParseHookFunction(name: "hello", url: nil)]
await configuration.hooks.updateFunctions(dummyHooks)
let functions2 = await configuration.hooks.getFunctions()
XCTAssertEqual(functions2.count, 2)
Expand All @@ -238,8 +238,12 @@ final class AppTests: XCTestCase {
XCTFail("Should have unwrapped")
return
}
let dummyHooks = ["yo": HookTrigger(className: "hello", triggerName: .afterDelete, url: url),
"no": HookTrigger(className: "hello", triggerName: .afterEvent, url: url)]
let dummyHooks = ["yo": ParseHookTrigger(className: "hello",
trigger: .afterDelete,
url: url),
"no": ParseHookTrigger(className: "hello",
trigger: .afterEvent,
url: url)]
await configuration.hooks.updateTriggers(dummyHooks)
let triggers2 = await configuration.hooks.getTriggers()
XCTAssertEqual(triggers2.count, 2)
Expand Down

0 comments on commit be2c6dd

Please sign in to comment.