|
35 | 35 | | tvOS | 11+ | |
36 | 36 |
|
37 | 37 | ## Highlights |
38 | | --[x] Simple interface |
| 38 | +🕹 Simple interface |
39 | 39 |
|
40 | | --[x] Combine support |
| 40 | +🚥 Combine support |
41 | 41 |
|
42 | | --[x] Live updates using SSE |
| 42 | +📡 Live updates using SSE |
43 | 43 |
|
44 | | --[x] Cancel ongoing request |
| 44 | +🚧 Cancel ongoing request |
45 | 45 | ## Getting started |
46 | 46 |
|
47 | 47 | ### Introduction |
48 | 48 | All the methods in this package support Combine. Retaining AnyCancellables will take care of canceling the ongoing API request when the retaining class is deinitialized. However, to cancel ongoing API requests when using completion handler supported methods, use [MTAPIServiceTask](https://mailtmswift.waseem.works/documentation/mailtmswift/mtapiservicetask). |
49 | 49 |
|
50 | | -The Helper classes [`MTAccountService`](https://mailtmswift.waseem.works/documentation/mailtmswift/mtaccountservice), [`MTMessageService`](https://mailtmswift.waseem.works/documentation/mailtmswift/mtmessageservice), [`MTDomainService`](https://mailtmswift.waseem.works/documentation/mailtmswift/mtdomainservice) are _Stateless_ classes, so you are free to create multiple instances of it, without creating any side effects. If you use a dependency container, store the instance of the class and store it as `Application` or `Singleton` scope. |
| 50 | +The Helper classes [`MTAccountService`](https://mailtmswift.waseem.works/documentation/mailtmswift/mtaccountservice), [`MTMessageService`](https://mailtmswift.waseem.works/documentation/mailtmswift/mtmessageservice) and [`MTDomainService`](https://mailtmswift.waseem.works/documentation/mailtmswift/mtdomainservice) are _Stateless_ classes, so you are free to create multiple instances of it, without creating any side effects. If you use a dependency container, store the instance of the class and store it as `Application` or `Singleton` scope. |
51 | 51 |
|
52 | 52 | ### Creating an account |
53 | 53 |
|
@@ -97,20 +97,155 @@ let accountService = MTAccountService() |
97 | 97 | accountService.deleteAccount(id: id, token: token) { (result: Result<MTEmptyResult, MTError>) in |
98 | 98 | if case let .failure(error) = result { |
99 | 99 | print("Error Occurred: \(error)") |
| 100 | + return |
100 | 101 | } |
101 | 102 |
|
102 | 103 | // Account deleted |
103 | | - doSomething() |
| 104 | + doSomethingAfterDelete() |
104 | 105 | } |
105 | 106 | ``` |
106 | 107 |
|
107 | 108 | ### Fetching available domains |
| 109 | + |
| 110 | +```swift |
| 111 | +import MailTMSwift |
| 112 | + |
| 113 | +let domainService = MTDomainService() |
| 114 | +domainService.getAllDomains { (result: Result<[MTDomain], MTError>) in |
| 115 | + switch result { |
| 116 | + case .success(let domains): |
| 117 | + print("Available domains: \(domains)") |
| 118 | + case .failure(let error): |
| 119 | + print("Error occurred \(error)") |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +To get details of a specific domain: |
| 125 | + |
| 126 | +```swift |
| 127 | +import MailTMSwift |
| 128 | + |
| 129 | +let id = "" |
| 130 | +domainService.getDomain(id: id) { (result: Result<MTDomain, MTError>) in |
| 131 | + switch result { |
| 132 | + case .success(let domains): |
| 133 | + print("Available domains: \(domains)") |
| 134 | + case .failure(let error): |
| 135 | + print("Error occurred \(error)") |
| 136 | + } |
| 137 | +} |
| 138 | +``` |
| 139 | + |
108 | 140 | ### Get all messages |
| 141 | + |
| 142 | +```swift |
| 143 | +import MailTMSwift |
| 144 | + |
| 145 | +let messageService = MTMessageService() |
| 146 | +let token = // Account JWT token |
| 147 | +messageService.getAllMessages(token: token) { (result: Result<[MTMessage], MTError>) in |
| 148 | + switch result { |
| 149 | + case .success(let messages): |
| 150 | + for message in messages { |
| 151 | + print("Message: \(message)") |
| 152 | + } |
| 153 | + case .failure(let error): |
| 154 | + print("Error occurred \(error)") |
| 155 | + } |
| 156 | +} |
| 157 | +``` |
| 158 | + |
| 159 | +> The messages returned by `getAllMessages(token:)` does not contain complete information, because it is intended to list the messages as list. To fetch the complete message with the complete information, use [`getMessage(id:token:)`](#get-complete-message). |
| 160 | +
|
| 161 | +### Get complete message |
| 162 | + |
| 163 | +```swift |
| 164 | +import MailTMSwift |
| 165 | + |
| 166 | +let messageService = MTMessageService() |
| 167 | +let id = // Message ID |
| 168 | +let token = // Account JWT token |
| 169 | +messageService.getMessage(id: id, token: token) { (result: Result<MTMessage, MTError>) in |
| 170 | + switch result { |
| 171 | + case .success(let message): |
| 172 | + print("Complete message: \(message)") |
| 173 | + case .failure(let error): |
| 174 | + print("Error occurred \(error)") |
| 175 | + } |
| 176 | +} |
| 177 | +``` |
| 178 | + |
| 179 | +> Please see [Get all messages](#get-all-messages) before proceeding with this method. |
| 180 | +
|
109 | 181 | ### Mark message as seen |
110 | | -### Get source of a message |
111 | | -### Deleteing a message |
112 | | -### Listening for live events |
113 | | - |
| 182 | + |
| 183 | +```swift |
| 184 | +import MailTMSwift |
| 185 | + |
| 186 | +let messageService = MTMessageService() |
| 187 | +let id = // Message ID |
| 188 | +let token = // Account JWT token |
| 189 | +messageService.markMessageAs(id: id, seen: true, token: token) { (result: Result<MTMessage, MTError>) in |
| 190 | + switch result { |
| 191 | + case .success(let message): |
| 192 | + print("Updated message: \(message)") |
| 193 | + case .failure(let error): |
| 194 | + print("Error occurred \(error)") |
| 195 | + } |
| 196 | +} |
| 197 | +``` |
| 198 | + |
| 199 | +### Get the source of a message |
| 200 | + |
| 201 | +```swift |
| 202 | +import MailTMSwift |
| 203 | + |
| 204 | +let messageService = MTMessageService() |
| 205 | +let id = // Message ID |
| 206 | +let token = // Account JWT token |
| 207 | +messageService.getSource(id: id, token: token) { (result: Result<MTMessageSource, MTError>) in |
| 208 | + switch result { |
| 209 | + case .success(let messageSource): |
| 210 | + print("Message source: \(messageSource)") |
| 211 | + case .failure(let error): |
| 212 | + print("Error occurred \(error)") |
| 213 | + } |
| 214 | +} |
| 215 | +``` |
| 216 | + |
| 217 | +If the size of message is big and you wish to use a downloadTask from `URLSession`, you can do so manually by using the `URLRequest` object returned by: |
| 218 | + |
| 219 | +```swift |
| 220 | +import MailTMSwift |
| 221 | + |
| 222 | +let messageService = MTMessageService() |
| 223 | +let id = // Message ID |
| 224 | +let token = // Account JWT token |
| 225 | +let urlRequest = messageService.getSourceRequest(id: id, token: token) |
| 226 | +let task = URLSession.shared.downloadTask(with: request) |
| 227 | +// handle download task |
| 228 | +``` |
| 229 | + |
| 230 | +### Deleting a message |
| 231 | + |
| 232 | +```swift |
| 233 | +import MailTMSwift |
| 234 | + |
| 235 | +let messageService = MTMessageService() |
| 236 | +let id = // Message ID |
| 237 | +let token = // Account JWT token |
| 238 | +messageService.deleteMessage(id: id, token: token) { (result: Result<MTEmptyResult, MTError>) in |
| 239 | + if case let .failure(error) = result { |
| 240 | + print("Error Occurred: \(error)") |
| 241 | + return |
| 242 | + } |
| 243 | + |
| 244 | + // Message deleted |
| 245 | + doSomethingAfterDelete() |
| 246 | +} |
| 247 | +``` |
| 248 | + |
114 | 249 | ## License |
115 | 250 |
|
116 | 251 | MailTMSwift is released under the MIT license. See [LICENSE](https://raw.githubusercontent.com/devwaseem/MailTMSwift/main/LICENSE) for details. |
|
0 commit comments