Skip to content

Commit

Permalink
Merge pull request #14 from writeas/add-wf-prefix-to-custom-types
Browse files Browse the repository at this point in the history
Add "WF" prefix to custom types
  • Loading branch information
AngeloStavrow authored Aug 17, 2020
2 parents fdb7530 + 7badb9d commit 5ab1e3b
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 170 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ Once you've done that, just import the library into whichever files should consu
// The rest of the Swift file goes here
```

Use public methods on the `WriteFreelyClient` to send and receive data from the server. The methods leverage completion blocks and the `Result` type, so you'd call them like so:
Use public methods on the `WFClient` to send and receive data from the server. The methods leverage completion blocks and the `Result` type, so you'd call them like so:

```swift
func loginHandler(result: (Result<User, Error>)) {
func loginHandler(result: (Result<WFUser, Error>)) {
do {
let user = try result.get()
print("Hello, \(user.username)!")
Expand All @@ -53,7 +53,7 @@ func loginHandler(result: (Result<User, Error>)) {


guard let instanceURL = URL(string: "https://your.writefreely.host/") else { fatalError() }
let client = WriteFreelyClient(for: instanceURL)
let client = WFClient(for: instanceURL)
client.login(username: "username", password: "password", completion: loginHandler)
```

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public struct Collection {
public struct WFCollection {
public var alias: String?
public var title: String
public var description: String?
Expand All @@ -11,7 +11,7 @@ public struct Collection {
public var url: String?
}

extension Collection: Decodable {
extension WFCollection: Decodable {
enum CodingKeys: String, CodingKey {
case alias
case title
Expand All @@ -23,9 +23,9 @@ extension Collection: Decodable {
case url
}

/// Creates a basic `Collection` object.
/// Creates a basic `WFCollection` object.
///
/// This initializer creates a bare-minimum `Collection` object for sending to the server; use the decoder-based
/// This initializer creates a bare-minimum `WFCollection` object for sending to the server; use the decoder-based
/// initializer to populate its other properties from the server response.
///
/// If no `alias` parameter is provided, one will be generated by the server.
Expand All @@ -47,9 +47,9 @@ extension Collection: Decodable {
self.url = nil
}

/// Creates a `Collection` object from the server response.
/// Creates a `WFCollection` object from the server response.
///
/// Primarily used by the `WriteFreelyClient` to create a `Collection` object from the JSON returned by the server.
/// Primarily used by the `WFClient` to create a `WFCollection` object from the JSON returned by the server.
///
/// - Parameter decoder: The decoder to use for translating the server response to a Swift object.
/// - Throws: Error thrown by the `try` attempt when decoding any given property.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

enum WriteFreelyError: Int, Error {
enum WFError: Int, Error {
case badRequest = 400
case unauthorized = 401
case forbidden = 403
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public struct Post {
public struct WFPost {
public var postId: String?
public var slug: String?
public var appearance: String?
Expand All @@ -15,7 +15,7 @@ public struct Post {
public var collectionAlias: String?
}

extension Post: Decodable {
extension WFPost: Decodable {
enum CodingKeys: String, CodingKey {
case postId = "id"
case slug
Expand All @@ -35,9 +35,9 @@ extension Post: Decodable {
}
}

/// Creates a basic `Post` object.
/// Creates a basic `WFPost` object.
///
/// This initializer creates a bare-minimum `Post` object for sending to the server; use the decoder-based
/// This initializer creates a bare-minimum `WFPost` object for sending to the server; use the decoder-based
/// initializer to populate its other properties from the server response.
///
/// Only the `body` parameter is required. If other properties are not provided, they will be generated by
Expand Down Expand Up @@ -76,9 +76,9 @@ extension Post: Decodable {
self.collectionAlias = nil
}

/// Creates a `Post` object from the server response.
/// Creates a `WFPost` object from the server response.
///
/// Primarily used by the `WriteFreelyClient` to create a `Post` object from the JSON returned by the server.
/// Primarily used by the `WFClient` to create a `WFPost` object from the JSON returned by the server.
///
/// - Parameter decoder: The decoder to use for translating the server response to a Swift object.
/// - Throws: Error thrown by the `try` attempt when decoding any given property.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import Foundation

public struct User {
public struct WFUser {
public var token: String
public var username: String?
public var email: String?
public var createdDate: Date?
}

extension User: Decodable {
extension WFUser: Decodable {
enum RootKeys: String, CodingKey {
case data
}
Expand All @@ -23,9 +23,9 @@ extension User: Decodable {
case createdDate = "created"
}

/// Creates a `User` object from the server response.
/// Creates a `WFUser` object from the server response.
///
/// Primarily used by the `WriteFreelyClient` to create a `User` object from the JSON returned by the server.
/// Primarily used by the `WFClient` to create a `WFUser` object from the JSON returned by the server.
///
/// - Parameter decoder: The decoder to use for translating the server response to a Swift object.
/// - Throws: Error thrown by the `try` attempt when decoding any given property.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
8 changes: 4 additions & 4 deletions docs/Home.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Types

- [Collection](/Collection)
- [Post](/Post)
- [User](/User)
- [WriteFreelyClient](/WriteFreelyClient)
- [WFClient](/WFClient)
- [WFCollection](/WFCollection)
- [WFPost](/WFPost)
- [WFUser](/WFUser)
60 changes: 30 additions & 30 deletions docs/WriteFreelyClient.md → docs/WFClient.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# WriteFreelyClient
# WFClient

``` swift
public class WriteFreelyClient
public class WFClient
```

## Initializers

### `init(for:)`

Initializes the WriteFreelyClient.
Initializes the WriteFreely client.

``` swift
public init(for instanceURL: URL)
Expand Down Expand Up @@ -37,7 +37,7 @@ var requestURL: URL
### `user`

``` swift
var user: User?
var user: WFUser?
```

## Methods
Expand All @@ -47,7 +47,7 @@ var user: User?
Creates a new collection.

``` swift
public func createCollection(token: String? = nil, withTitle title: String, alias: String? = nil, completion: @escaping (Result<Collection, Error>) -> Void)
public func createCollection(token: String? = nil, withTitle title: String, alias: String? = nil, completion: @escaping (Result<WFCollection, Error>) -> Void)
```

If only a `title` is given, the server will generate and return an alias; in this case, clients should store
Expand All @@ -58,14 +58,14 @@ the returned `alias` for future operations.
- token: - token: The access token for the user creating the collection.
- title: - title: The title of the new collection.
- alias: - alias: The alias of the collection.
- completion: - completion: A handler for the returned `Collection` on success, or `Error` on failure.
- completion: - completion: A handler for the returned `WFCollection` on success, or `Error` on failure.

### `getCollection(token:withAlias:completion:)`

Retrieves a collection's metadata.

``` swift
public func getCollection(token: String? = nil, withAlias alias: String, completion: @escaping (Result<Collection, Error>) -> Void)
public func getCollection(token: String? = nil, withAlias alias: String, completion: @escaping (Result<WFCollection, Error>) -> Void)
```

Collections can be retrieved without authentication. However, authentication is required for retrieving a
Expand All @@ -75,7 +75,7 @@ private collection or one with scheduled posts.

- token: - token: The access token for the user retrieving the collection.
- alias: - alias: The alias for the collection to be retrieved.
- completion: - completion: A handler for the returned `Collection` on success, or `Error` on failure.
- completion: - completion: A handler for the returned `WFCollection` on success, or `Error` on failure.

### `deleteCollection(token:withAlias:completion:)`

Expand All @@ -98,7 +98,7 @@ Any posts in the collection are not deleted; rather, they are made anonymous.
Retrieves an array of posts.

``` swift
public func getPosts(token: String? = nil, in collectionAlias: String? = nil, completion: @escaping (Result<[Post], Error>) -> Void)
public func getPosts(token: String? = nil, in collectionAlias: String? = nil, completion: @escaping (Result<[WFPost], Error>) -> Void)
```

If the `collectionAlias` argument is provided, an array of all posts in that collection is retrieved; if
Expand All @@ -111,7 +111,7 @@ private collection or one with scheduled posts.

- token: - token: The access token for the user retrieving the posts.
- collectionAlias: - collectionAlias: The alias for the collection whose posts are to be retrieved.
- completion: - completion: A handler for the returned `[Post]` on success, or `Error` on failure.
- completion: - completion: A handler for the returned `[WFPost]` on success, or `Error` on failure.

### `movePost(token:postId:with:to:completion:)`

Expand All @@ -121,7 +121,7 @@ Moves a post to a collection.
public func movePost(token: String? = nil, postId: String, with modifyToken: String? = nil, to collectionAlias: String, completion: @escaping (Result<Bool, Error>) -> Void)
```

> Attention: - The closure should return a result type of \`\<\[Post\], Error\>\`.
> Attention: - The closure should return a result type of \`\<\[WFPost\], Error\>\`.
> - The modifyToken for the post is currently ignored.
>

Expand Down Expand Up @@ -176,7 +176,7 @@ array of posts, this function only accepts a single post.
Creates a new post.

``` swift
public func createPost(token: String? = nil, post: Post, in collectionAlias: String? = nil, completion: @escaping (Result<Post, Error>) -> Void)
public func createPost(token: String? = nil, post: WFPost, in collectionAlias: String? = nil, completion: @escaping (Result<WFPost, Error>) -> Void)
```

Creates a new post. If a `collectionAlias` is provided, the post is published to that collection; otherwise, it
Expand All @@ -185,52 +185,52 @@ is posted to the user's Drafts.
#### Parameters

- token: - token: The access token of the user creating the post.
- post: - post: The `Post` object to be published.
- post: - post: The `WFPost` object to be published.
- collectionAlias: - collectionAlias: The collection to which the post should be published.
- completion: - completion: A handler for the `Post` object returned on success, or `Error` on failure.
- completion: - completion: A handler for the `WFPost` object returned on success, or `Error` on failure.

### `getPost(token:byId:completion:)`

Retrieves a post.

``` swift
public func getPost(token: String? = nil, byId postId: String, completion: @escaping (Result<Post, Error>) -> Void)
public func getPost(token: String? = nil, byId postId: String, completion: @escaping (Result<WFPost, Error>) -> Void)
```

The `Post` object returned may include additional data, including page views and extracted tags.
The `WFPost` object returned may include additional data, including page views and extracted tags.

#### Parameters

- token: - token: The access token of the user retrieving the post.
- postId: - postId: The ID of the post to be retrieved.
- completion: - completion: A handler for the `Post` object returned on success, or `Error` on failure.
- completion: - completion: A handler for the `WFPost` object returned on success, or `Error` on failure.

### `getPost(token:bySlug:from:completion:)`

Retrieves a post from a collection.

``` swift
public func getPost(token: String? = nil, bySlug slug: String, from collectionAlias: String, completion: @escaping (Result<Post, Error>) -> Void)
public func getPost(token: String? = nil, bySlug slug: String, from collectionAlias: String, completion: @escaping (Result<WFPost, Error>) -> Void)
```

Collection posts can be retrieved without authentication. However, authentication is required for retrieving a
post from a private collection.

The `Post` object returned may include additional data, including page views and extracted tags.
The `WFPost` object returned may include additional data, including page views and extracted tags.

#### Parameters

- token: - token: The access token of the user retrieving the post.
- slug: - slug: The slug of the post to be retrieved.
- collectionAlias: - collectionAlias: The alias of the collection from which the post should be retrieved.
- completion: - completion: A handler for the `Post` object returned on success, or `Error` on failure.
- completion: - completion: A handler for the `WFPost` object returned on success, or `Error` on failure.

### `updatePost(token:postId:updatedPost:with:completion:)`

Updates an existing post.

``` swift
public func updatePost(token: String? = nil, postId: String, updatedPost: Post, with modifyToken: String? = nil, completion: @escaping (Result<Post, Error>) -> Void)
public func updatePost(token: String? = nil, postId: String, updatedPost: WFPost, with modifyToken: String? = nil, completion: @escaping (Result<WFPost, Error>) -> Void)
```

Note that if the `updatedPost` object is provided without a title, the original post's title will be removed.
Expand All @@ -242,9 +242,9 @@ Note that if the `updatedPost` object is provided without a title, the original

- token: - token: The access token for the user updating the post.
- postId: - postId: The ID of the post to be updated.
- updatedPost: - updatedPost: The `Post` object with which to update the existing post.
- updatedPost: - updatedPost: The `WFPost` object with which to update the existing post.
- modifyToken: - modifyToken: The post's modify token; required if the post doesn't belong to the requesting user.
- completion: - completion: A handler for the `Post` object returned on success, or `Error` on failure.
- completion: - completion: A handler for the `WFPost` object returned on success, or `Error` on failure.

### `deletePost(token:postId:with:completion:)`

Expand All @@ -269,10 +269,10 @@ public func deletePost(token: String? = nil, postId: String, with modifyToken: S
Logs the user in to their account on the WriteFreely instance.

``` swift
public func login(username: String, password: String, completion: @escaping (Result<User, Error>) -> Void)
public func login(username: String, password: String, completion: @escaping (Result<WFUser, Error>) -> Void)
```

On successful login, the `WriteFreelyClient`'s `user` property is set to the returned `User` object; this allows
On successful login, the `WFClient`'s `user` property is set to the returned `WFUser` object; this allows
authenticated requests to be made without having to provide an access token.

It is otherwise not necessary to login the user if their access token is provided to the calling function.
Expand All @@ -281,7 +281,7 @@ It is otherwise not necessary to login the user if their access token is provide

- username: - username: The user's username.
- password: - password: The user's password.
- completion: - completion: A handler for the `User` object returned on success, or `Error` on failure.
- completion: - completion: A handler for the `WFUser` object returned on success, or `Error` on failure.

### `logout(token:completion:)`

Expand Down Expand Up @@ -314,16 +314,16 @@ public func getUserData(token: String? = nil, completion: @escaping (Result<Data
Retrieves a user's collections.

``` swift
public func getUserCollections(token: String? = nil, completion: @escaping (Result<[Collection], Error>) -> Void)
public func getUserCollections(token: String? = nil, completion: @escaping (Result<[WFCollection], Error>) -> Void)
```

#### Parameters

- token: - token: The access token for the user whose collections are to be retrieved.
- completion: - completion: A handler for the `[Collection]` object returned on success, or `Error` on failure.
- completion: - completion: A handler for the `[WFCollection]` object returned on success, or `Error` on failure.

### `translateWriteFreelyError(fromServerResponse:)`
### `translateWFError(fromServerResponse:)`

``` swift
func translateWriteFreelyError(fromServerResponse response: Data) -> WriteFreelyError?
func translateWFError(fromServerResponse response: Data) -> WFError?
```
Loading

0 comments on commit 5ab1e3b

Please sign in to comment.