forked from mattpolzin/OpenAPIKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add callback object (not tested yet).
- Loading branch information
1 parent
f02b6ad
commit ef8bcdc
Showing
6 changed files
with
114 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// | ||
// Callback.swift | ||
// | ||
// | ||
// Created by Mathew Polzin on 11/1/20. | ||
// | ||
|
||
import Foundation | ||
|
||
extension OpenAPI { | ||
|
||
/// A URL template where the placeholders are OpenAPI **Runtime Expressions** instead | ||
/// of named variables. | ||
/// | ||
/// See [OpenAPI Callback Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#callback-object) and [OpenAPI Runtime Expression](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#runtime-expressions) for more. | ||
/// | ||
public struct CallbackURL: Hashable, RawRepresentable { | ||
public let template: URLTemplate | ||
|
||
/// The string value of the URL without variable replacement. | ||
/// | ||
/// Variables cannot be replaced based on other information in the | ||
/// OpenAPI document; they are only available at "runtime" which is | ||
/// where the name of the OpenAPI structure `CallbackURL` | ||
/// represents comes from. | ||
public var rawValue: String { | ||
template.rawValue | ||
} | ||
|
||
/// Get a URL from the runtime expression if it is a valid URL without | ||
/// variable replacement. | ||
/// | ||
/// Callback URLs with variables in them will not be valid URLs | ||
/// and are therefore guaranteed to return `nil`. | ||
public var url: URL? { | ||
template.url | ||
} | ||
|
||
/// Create a CallbackURL from the string if possible. | ||
public init?(rawValue: String) { | ||
guard let template = URLTemplate(rawValue: rawValue) else { | ||
return nil | ||
} | ||
self.template = template | ||
} | ||
|
||
public init(url: URL) { | ||
template = .init(url: url) | ||
} | ||
} | ||
|
||
/// A map from runtime expressions to path items to be used as | ||
/// callbacks for the API. | ||
/// | ||
/// See [OpenAPI Callback Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#callback-object). | ||
/// | ||
public typealias Callback = OrderedDictionary<CallbackURL, PathItem> | ||
|
||
public typealias CallbackMap = OrderedDictionary<String, Either<JSONReference<Callback>, Callback>> | ||
} | ||
|
||
extension OpenAPI.CallbackURL: Encodable { | ||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.singleValueContainer() | ||
|
||
try container.encode(rawValue) | ||
} | ||
} | ||
|
||
extension OpenAPI.CallbackURL: Decodable { | ||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.singleValueContainer() | ||
|
||
template = try container.decode(URLTemplate.self) | ||
} | ||
} |
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
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