Class
WFClient
public class WFClient
Initializers
init(for:with:)
public init(for instanceURL: URL, with session: URLSessionProtocol = URLSession.shared)
Initializes the WriteFreely client.
Required for connecting to the API endpoints of a WriteFreely instance.
Parameters
Name | Type | Description |
---|---|---|
instanceURL | URL |
The URL for the WriteFreely instance to which we're connecting, including the protocol. |
session | URLSessionProtocol |
The URL session to use for connections; defaults to |
Properties
requestURL
public var requestURL: URL
user
public var user: WFUser?
Methods
createCollection(token:withTitle:alias:completion:)
public func createCollection(
token: String? = nil,
withTitle title: String,
alias: String? = nil,
completion: @escaping (Result<WFCollection, Error>) -> Void
)
Creates a new collection.
If only a title
is given, the server will generate and return an alias; in this case, clients should store
the returned alias
for future operations.
Parameters
Name | Type | Description |
---|---|---|
token | String? |
The access token for the user creating the collection. |
title | String |
The title of the new collection. |
alias | String? |
The alias of the collection. |
completion | @escaping (Result<WFCollection, Error>) -> Void |
A handler for the returned |
getCollection(token:withAlias:completion:)
public func getCollection(
token: String? = nil,
withAlias alias: String,
completion: @escaping (Result<WFCollection, Error>) -> Void
)
Retrieves a collection's metadata.
Collections can be retrieved without authentication. However, authentication is required for retrieving a private collection or one with scheduled posts.
Parameters
Name | Type | Description |
---|---|---|
token | String? |
The access token for the user retrieving the collection. |
alias | String |
The alias for the collection to be retrieved. |
completion | @escaping (Result<WFCollection, Error>) -> Void |
A handler for the returned |
deleteCollection(token:withAlias:completion:)
public func deleteCollection(
token: String? = nil,
withAlias alias: String,
completion: @escaping (Result<Bool, Error>) -> Void
)
Permanently deletes a collection.
Any posts in the collection are not deleted; rather, they are made anonymous.
Parameters
Name | Type | Description |
---|---|---|
token | String? |
The access token for the user deleting the collection. |
alias | String |
The alias for the collection to be deleted. |
completion | @escaping (Result<Bool, Error>) -> Void |
A hander for the returned |
getPosts(token:in:completion:)
public func getPosts(
token: String? = nil,
in collectionAlias: String? = nil,
completion: @escaping (Result<[WFPost], Error>) -> Void
)
Retrieves an array of posts.
If the collectionAlias
argument is provided, an array of all posts in that collection is retrieved; if
omitted, an array of all posts created by the user whose access token is provided is retrieved.
Collection posts can be retrieved without authentication; however, authentication is required for retrieving a private collection or one with scheduled posts.
Parameters
Name | Type | Description |
---|---|---|
token | String? |
The access token for the user retrieving the posts. |
collectionAlias | String? |
The alias for the collection whose posts are to be retrieved. |
completion | @escaping (Result<[WFPost], Error>) -> Void |
A handler for the returned |
movePost(token:postId:with:to:completion:)
public func movePost(
token: String? = nil,
postId: String,
with modifyToken: String? = nil,
to collectionAlias: String?,
completion: @escaping (Result<Bool, Error>) -> Void
)
Moves a post to a collection.
Parameters
Name | Type | Description |
---|---|---|
token | String? |
The access token for the user moving the post to a collection. |
postId | String |
The ID of the post to add to the collection. |
modifyToken | String? |
The post's modify token; required if the post doesn't belong to the requesting user. If |
collectionAlias | String? |
The alias of the collection to which the post should be added; if |
completion | @escaping (Result<Bool, Error>) -> Void |
A handler for the returned |
pinPost(token:postId:at:in:completion:)
public func pinPost(
token: String? = nil,
postId: String,
at position: Int? = nil,
in collectionAlias: String,
completion: @escaping (Result<Bool, Error>) -> Void
)
Pins a post to a collection.
Pinning a post to a collection adds it as a navigation item in the collection/blog home page header, rather than on the blog itself. While the API endpoint can take an array of posts, this function only accepts a single post.
Parameters
Name | Type | Description |
---|---|---|
token | String? |
The access token of the user pinning the post to the collection. |
postId | String |
The ID of the post to be pinned. |
position | Int? |
The numeric position in which to pin the post; if |
collectionAlias | String |
The alias of the collection to which the post should be pinned. |
completion | @escaping (Result<Bool, Error>) -> Void |
A handler for the |
unpinPost(token:postId:from:completion:)
public func unpinPost(
token: String? = nil,
postId: String,
from collectionAlias: String,
completion: @escaping (Result<Bool, Error>) -> Void
)
Unpins a post from a collection.
Removes the post from a navigation item and puts it back on the blog itself. While the API endpoint can take an array of posts, this function only accepts a single post.
Parameters
Name | Type | Description |
---|---|---|
token | String? |
The access token of the user un-pinning the post from the collection. |
postId | String |
The ID of the post to be un-pinned. |
collectionAlias | String |
The alias of the collection to which the post should be un-pinned. |
completion | @escaping (Result<Bool, Error>) -> Void |
A handler for the |
createPost(token:post:in:completion:)
public func createPost(
token: String? = nil,
post: WFPost,
in collectionAlias: String? = nil,
completion: @escaping (Result<WFPost, Error>) -> Void
)
Creates a new post.
Creates a new post. If a collectionAlias
is provided, the post is published to that collection; otherwise, it
is posted to the user's Drafts.
Parameters
Name | Type | Description |
---|---|---|
token | String? |
The access token of the user creating the post. |
post | WFPost |
The |
collectionAlias | String? |
The collection to which the post should be published. |
completion | @escaping (Result<WFPost, Error>) -> Void |
A handler for the |
getPost(token:byId:completion:)
public func getPost(
token: String? = nil,
byId postId: String,
completion: @escaping (Result<WFPost, Error>) -> Void
)
Retrieves a post.
The WFPost
object returned may include additional data, including page views and extracted tags.
Parameters
Name | Type | Description |
---|---|---|
token | String? |
The access token of the user retrieving the post. |
postId | String |
The ID of the post to be retrieved. |
completion | @escaping (Result<WFPost, Error>) -> Void |
A handler for the |
getPost(token:bySlug:from:completion:)
public func getPost(
token: String? = nil,
bySlug slug: String,
from collectionAlias: String,
completion: @escaping (Result<WFPost, Error>) -> Void
)
Retrieves a post from a collection.
Collection posts can be retrieved without authentication. However, authentication is required for retrieving a post from a private collection.
The WFPost
object returned may include additional data, including page views and extracted tags.
Parameters
Name | Type | Description |
---|---|---|
token | String? |
The access token of the user retrieving the post. |
slug | String |
The slug of the post to be retrieved. |
collectionAlias | String |
The alias of the collection from which the post should be retrieved. |
completion | @escaping (Result<WFPost, Error>) -> Void |
A handler for the |
updatePost(token:postId:updatedPost:with:completion:)
public func updatePost(
token: String? = nil,
postId: String,
updatedPost: WFPost,
with modifyToken: String? = nil,
completion: @escaping (Result<WFPost, Error>) -> Void
)
Updates an existing post.
Note that if the updatedPost
object is provided without a title, the original post's title will be removed.
Parameters
Name | Type | Description |
---|---|---|
token | String? |
The access token for the user updating the post. |
postId | String |
The ID of the post to be updated. |
updatedPost | WFPost |
The |
modifyToken | String? |
The post's modify token; required if the post doesn't belong to the requesting user. |
completion | @escaping (Result<WFPost, Error>) -> Void |
A handler for the |
deletePost(token:postId:with:completion:)
public func deletePost(
token: String? = nil,
postId: String,
with modifyToken: String? = nil,
completion: @escaping (Result<Bool, Error>) -> Void
)
Deletes an existing post.
Parameters
Name | Type | Description |
---|---|---|
token | String? |
The access token for the user deleting the post. |
postId | String |
The ID of the post to be deleted. |
modifyToken | String? |
The post's modify token; required if the post doesn't belong to the requesting user. |
completion | @escaping (Result<Bool, Error>) -> Void |
A handler for the |
login(username:password:completion:)
public func login(username: String, password: String, completion: @escaping (Result<WFUser, Error>) -> Void)
Logs the user in to their account on the WriteFreely instance.
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.
Parameters
Name | Type | Description |
---|---|---|
username | String |
The user's username. |
password | String |
The user's password. |
completion | @escaping (Result<WFUser, Error>) -> Void |
A handler for the |
logout(token:completion:)
public func logout(token: String? = nil, completion: @escaping (Result<Bool, Error>) -> Void)
Invalidates the user's access token.
Parameters
Name | Type | Description |
---|---|---|
token | String? |
The token to invalidate. |
completion | @escaping (Result<Bool, Error>) -> Void |
A handler for the |
getUserData(token:completion:)
public func getUserData(token: String? = nil, completion: @escaping (Result<Data, Error>) -> Void)
Retrieves a user's basic data.
Parameters
Name | Type | Description |
---|---|---|
token | String? |
The access token for the user to fetch. |
completion | @escaping (Result<Data, Error>) -> Void |
A handler for the |
getUserCollections(token:completion:)
public func getUserCollections(token: String? = nil, completion: @escaping (Result<[WFCollection], Error>) -> Void)
Retrieves a user's collections.
Parameters
Name | Type | Description |
---|---|---|
token | String? |
The access token for the user whose collections are to be retrieved. |
completion | @escaping (Result<[WFCollection], Error>) -> Void |
A handler for the |