Skip to content

Commit

Permalink
Merge pull request #24 from writeas/extend-move-api-to-drafts
Browse files Browse the repository at this point in the history
Extend move API to drafts
  • Loading branch information
AngeloStavrow authored Oct 9, 2020
2 parents accd9c4 + 4327131 commit e8fc77d
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions Sources/WriteFreely/WFClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -327,40 +327,39 @@ public class WFClient {
/// - Parameters:
/// - token: The access token for the user moving the post to a collection.
/// - postId: The ID of the post to add to the collection.
/// - modifyToken: The post's modify token; required if the post doesn't belong to the requesting user.
/// - collectionAlias: The alias of the collection to which the post should be added.
/// - modifyToken: The post's modify token; required if the post doesn't belong to the requesting user. If `collectionAlias` is `nil`, do not include a `modifyToken`.
/// - collectionAlias: The alias of the collection to which the post should be added; if `nil`, this removes the post from any collection.
/// - completion: A handler for the returned `Bool` on success, or `Error` on failure.
public func movePost(
token: String? = nil,
postId: String,
with modifyToken: String? = nil,
to collectionAlias: String,
to collectionAlias: String?,
completion: @escaping (Result<Bool, Error>) -> Void
) {
if token == nil && user == nil { return }
guard let tokenToVerify = token ?? user?.token else { return }

guard let url = URL(string: "collections/\(collectionAlias)/collect", relativeTo: requestURL) else { return }
if collectionAlias == nil && modifyToken != nil { completion(.failure(WFError.badRequest)) }

var urlString = ""
if let collectionAlias = collectionAlias {
urlString = "collections/\(collectionAlias)/collect"
} else {
urlString = "posts/disperse"
}
guard let url = URL(string: urlString, relativeTo: requestURL) else { return }
var request = URLRequest(url: url)

request.httpMethod = "POST"
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue(tokenToVerify, forHTTPHeaderField: "Authorization")

var bodyObject: [[String: Any]]
var bodyObject: [Any]
if let modifyToken = modifyToken {
bodyObject = [
[
"id": postId,
"token": modifyToken
]
]
bodyObject = [ [ "id": postId, "token": modifyToken ] ]
} else {
bodyObject = [
[
"id": postId
]
]
bodyObject = collectionAlias == nil ? [ postId ] : [ [ "id": postId ] ]
}

do {
Expand Down

0 comments on commit e8fc77d

Please sign in to comment.