|
| 1 | +// |
| 2 | +// Ulysses |
| 3 | +// CallbackURLKit |
| 4 | +/* |
| 5 | + The MIT License (MIT) |
| 6 | + Copyright (c) 2017 Eric Marchand (phimage) |
| 7 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + of this software and associated documentation files (the "Software"), to deal |
| 9 | + in the Software without restriction, including without limitation the rights |
| 10 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + copies of the Software, and to permit persons to whom the Software is |
| 12 | + furnished to do so, subject to the following conditions: |
| 13 | + The above copyright notice and this permission notice shall be included in all |
| 14 | + copies or substantial portions of the Software. |
| 15 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | + SOFTWARE. |
| 22 | + */ |
| 23 | +#if IMPORT |
| 24 | + import CallbackURLKit |
| 25 | +#endif |
| 26 | +import Foundation |
| 27 | + |
| 28 | +/* |
| 29 | + Client for ulysses app https://ulyssesapp.com |
| 30 | + https://ulyssesapp.com/kb/x-callback-url/ |
| 31 | + */ |
| 32 | +public class Ulysses: Client { |
| 33 | + |
| 34 | + public enum UlyssesError: Int, FailureCallbackError { |
| 35 | + |
| 36 | + case noAccessToken = 31 |
| 37 | + |
| 38 | + public var message: String { |
| 39 | + switch self { |
| 40 | + case .noAccessToken: |
| 41 | + return "No access token provided" |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public var code: Int { |
| 46 | + return self.rawValue |
| 47 | + } |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + public init() { |
| 52 | + super.init(urlScheme: "ulysses") |
| 53 | + } |
| 54 | + |
| 55 | + /* |
| 56 | + To protect the Ulysses library against access from malicious apps, actions that expose content or destructively change require the calling app to be authorized. |
| 57 | + |
| 58 | + @param appName your app name |
| 59 | + @param onSuccess the success callback where you will receive the access-token as String. |
| 60 | + |
| 61 | + Available since Ulysses 2.8 (API version 2). |
| 62 | + */ |
| 63 | + public func authorize(appName: String, onSuccess: @escaping (_ token: String) -> Void, onFailure: FailureCallback? = nil) throws { |
| 64 | + try self.perform(action: "authorize", parameters: ["appname": appName], onSuccess: { parameters in |
| 65 | + if let token = parameters?["access-token"] { |
| 66 | + onSuccess(token) |
| 67 | + } else { |
| 68 | + onFailure?(UlyssesError.noAccessToken) |
| 69 | + } |
| 70 | + }, onFailure: onFailure) |
| 71 | + } |
| 72 | + |
| 73 | + /* |
| 74 | + Creates a new sheet. |
| 75 | + Available on iOS since Ulysses 2.6 (API version 1), on Mac since Ulysses 2.8 (API version 2). |
| 76 | + |
| 77 | + @param text The contents that should be inserted to the new sheet. Must be URL-encoded. Contents are imported as Markdown by default. |
| 78 | + |
| 79 | + @param group Optional. Specifies the group the new sheet should be inserted to. This argument can be set to one of the following values: |
| 80 | + - A group name (e.g. My Group) that will match the first group having the same name, regardless of its position in the group hierarchy. |
| 81 | + - A path to a particular target group (e.g. /My Group/My Subgroup). Any path must begin with a slash. (More…) |
| 82 | + - A unique identifier of the target group. (More…) |
| 83 | + If no value is given, the sheet is created inside the Inbox. |
| 84 | + @param format Optional. Specifies the format of the imported text: markdown, text, html. Defaults to Markdown. |
| 85 | + @param index Optional. The position of the new sheet in its parent group. Use 0 to make it the first sheet. Available since Ulysses 2.8 (API version 2). |
| 86 | + */ |
| 87 | + public func newSheet(text: String, group: String? = nil, format: String? = nil, index: String? = nil, onSuccess: SuccessCallback? = nil, onFailure: FailureCallback? = nil, onCancel: CancelCallback? = nil) throws { |
| 88 | + |
| 89 | + var parameters = ["text": text] |
| 90 | + if let group = group { |
| 91 | + parameters["group"] = group |
| 92 | + } |
| 93 | + if let format = format { |
| 94 | + parameters["format"] = format |
| 95 | + } |
| 96 | + if let index = index { |
| 97 | + parameters["index"] = index |
| 98 | + } |
| 99 | + |
| 100 | + try self.perform(action: "new-sheet", parameters: parameters, onSuccess: onSuccess, onCancel: onCancel) |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments