Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Gutenberg] Add support for POST requests in Gutenberg networking #15864

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 51 additions & 10 deletions WordPress/Classes/ViewRelated/Gutenberg/GutenbergNetworking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,53 @@ struct GutenbergNetworkRequest {

private let path: String
private unowned let blog: Blog

private let httpMethod: GutenbergHTTPMethod
private let parameters: [String: AnyObject]?

fluiddot marked this conversation as resolved.
Show resolved Hide resolved
init(path: String, blog: Blog) {
self.path = path
self.blog = blog
self.httpMethod = .get
self.parameters = nil
}

init(path: String, blog: Blog, parameters: [String: AnyObject]? = nil) {
self.path = path
self.blog = blog
self.httpMethod = .post
self.parameters = parameters
}

fluiddot marked this conversation as resolved.
Show resolved Hide resolved
func request(completion: @escaping CompletionHandler) {
if blog.isAccessibleThroughWPCom(), let dotComID = blog.dotComID {
dotComRequest(with: dotComID, completion: completion)
switch httpMethod {
case .post:
dotComPostRequest(with: dotComID, completion: completion)
case .get:
dotComGetRequest(with: dotComID, completion: completion)
}
} else {
selfHostedRequest(completion: completion)
}
}

// MARK: - dotCom

private func dotComRequest(with dotComID: NSNumber, completion: @escaping CompletionHandler) {
private func dotComGetRequest(with dotComID: NSNumber, completion: @escaping CompletionHandler) {
blog.wordPressComRestApi()?.GET(dotComPath(with: dotComID), parameters: nil, success: { (response, httpResponse) in
completion(.success(response))
}, failure: { (error, httpResponse) in
completion(.failure(error.nsError(with: httpResponse)))
})
}

fluiddot marked this conversation as resolved.
Show resolved Hide resolved
private func dotComPostRequest(with dotComID: NSNumber, completion: @escaping CompletionHandler) {
blog.wordPressComRestApi()?.POST(dotComPath(with: dotComID), parameters: parameters, success: { (response, httpResponse) in
completion(.success(response))
}, failure: { (error, httpResponse) in
completion(.failure(error.nsError(with: httpResponse)))
})
}

private func dotComPath(with dotComID: NSNumber) -> String {
return path.replacingOccurrences(of: "/wp/v2/", with: "/wp/v2/sites/\(dotComID)/")
Expand All @@ -46,14 +70,26 @@ struct GutenbergNetworkRequest {
return
}

api.GET(path, parameters: nil) { (result, httpResponse) in
switch result {
case .success(let response):
completion(.success(response))
case .failure(let error):
completion(.failure(error as NSError))
switch httpMethod {
case .post:
api.POST(path, parameters: parameters) { (result, httpResponse) in
switch result {
case .success(let response):
completion(.success(response))
case .failure(let error):
completion(.failure(error as NSError))
}
}
}
case .get:
api.GET(path, parameters: nil) { (result, httpResponse) in
switch result {
case .success(let response):
completion(.success(response))
case .failure(let error):
completion(.failure(error as NSError))
}
}
}
}

private var selfHostedPath: String {
Expand All @@ -71,3 +107,8 @@ private extension Error {
return URLError(code, userInfo: [NSLocalizedDescriptionKey: localizedDescription]) as NSError
}
}

enum GutenbergHTTPMethod: String {
case get = "GET"
case post = "POST"
}
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,13 @@ extension GutenbergViewController {

extension GutenbergViewController: GutenbergBridgeDelegate {

func gutenbergDidRequestFetch(path: String, completion: @escaping (Result<Any, NSError>) -> Void) {
func gutenbergDidGetRequestFetch(path: String, completion: @escaping (Result<Any, NSError>) -> Void) {
GutenbergNetworkRequest(path: path, blog: post.blog).request(completion: completion)
}

fluiddot marked this conversation as resolved.
Show resolved Hide resolved
func gutenbergDidPostRequestFetch(path: String, parameters: [String: AnyObject]?, completion: @escaping (Result<Any, NSError>) -> Void) {
GutenbergNetworkRequest(path: path, blog: post.blog, parameters: parameters).request(completion: completion)
}

func editorDidAutosave() {
autosaver.contentDidChange()
Expand Down