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

Add POST requests helper in WordPressOrgRestApi #589

Merged
merged 5 commits into from
Apr 2, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ _None._

### Internal Changes

_None._
Add POST requests helper in `WordPressOrgRestApi`.
mokagio marked this conversation as resolved.
Show resolved Hide resolved


## 7.0.0
Expand Down
4 changes: 4 additions & 0 deletions WordPressKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
17CE77F420C701C8001DEA5A /* ReaderSiteSearchServiceRemoteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17CE77F320C701C8001DEA5A /* ReaderSiteSearchServiceRemoteTests.swift */; };
17D936252475D8AB008B2205 /* RemoteHomepageType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17D936242475D8AB008B2205 /* RemoteHomepageType.swift */; };
1A4F98672279A87D00D86E8E /* WPKit-Swift.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A4F98662279A87D00D86E8E /* WPKit-Swift.h */; settings = {ATTRIBUTES = (Private, ); }; };
1D969EB329D45745001FE37C /* wp-reusable-blocks.json in Resources */ = {isa = PBXBuildFile; fileRef = 1D969EB229D45745001FE37C /* wp-reusable-blocks.json */; };
1DAC3D2629AF4F250068FE13 /* RemoteVideoPressVideo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1DAC3D2529AF4F250068FE13 /* RemoteVideoPressVideo.swift */; };
1DC837C229B9F04F009DCD4B /* RemoteVideoPressVideoTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1DC837C129B9F04F009DCD4B /* RemoteVideoPressVideoTests.swift */; };
1DF972BA29B0DF8C007A72BC /* videopress-token.json in Resources */ = {isa = PBXBuildFile; fileRef = 1DF972B729B0DF8C007A72BC /* videopress-token.json */; };
Expand Down Expand Up @@ -683,6 +684,7 @@
17CE77F320C701C8001DEA5A /* ReaderSiteSearchServiceRemoteTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReaderSiteSearchServiceRemoteTests.swift; sourceTree = "<group>"; };
17D936242475D8AB008B2205 /* RemoteHomepageType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteHomepageType.swift; sourceTree = "<group>"; };
1A4F98662279A87D00D86E8E /* WPKit-Swift.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "WPKit-Swift.h"; sourceTree = "<group>"; };
1D969EB229D45745001FE37C /* wp-reusable-blocks.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "wp-reusable-blocks.json"; sourceTree = "<group>"; };
1DAC3D2529AF4F250068FE13 /* RemoteVideoPressVideo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteVideoPressVideo.swift; sourceTree = "<group>"; };
1DC837C129B9F04F009DCD4B /* RemoteVideoPressVideoTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteVideoPressVideoTests.swift; sourceTree = "<group>"; };
1DF972B729B0DF8C007A72BC /* videopress-token.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "videopress-token.json"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2284,6 +2286,7 @@
FFA4D4AC2423B1FE00BF5180 /* wp-admin-post-new.html */,
FFA4D4AE2423B33800BF5180 /* wp-forbidden.json */,
FFA4D4AF2423B33800BF5180 /* wp-pages.json */,
1D969EB229D45745001FE37C /* wp-reusable-blocks.json */,
740B23EB1F17FB7E00067A2A /* xmlrpc-bad-username-password-error.xml */,
740B23EC1F17FB7E00067A2A /* xmlrpc-malformed-request-xml-error.xml */,
740B23D71F17FB4200067A2A /* xmlrpc-metaweblog-editpost-bad-xml-failure.xml */,
Expand Down Expand Up @@ -2745,6 +2748,7 @@
E14694031F344F71004052C8 /* site-plugins-error.json in Resources */,
C738CAF728622B94001BE107 /* qrlogin-authenticate-200.json in Resources */,
829BA4311FACF187003ADEEA /* activity-rewind-status-success.json in Resources */,
1D969EB329D45745001FE37C /* wp-reusable-blocks.json in Resources */,
93BD27571EE73442002BB00B /* auth-send-login-email-no-user-failure.json in Resources */,
9A88174C223C01E400A3AB20 /* jetpack-service-success.json in Resources */,
9A881755223C01E400A3AB20 /* jetpack-service-error-activation-response.json in Resources */,
Expand Down
7 changes: 7 additions & 0 deletions WordPressKit/WordPressOrgRestApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ open class WordPressOrgRestApi: NSObject, WordPressRestApi {
return request(method: .get, path: path, parameters: parameters, completion: completion)
}

@discardableResult
open func POST(_ path: String,
parameters: [String: AnyObject]?,
completion: @escaping Completion) -> Progress? {
return request(method: .post, path: path, parameters: parameters, completion: completion)
}

@discardableResult
open func request(method: HTTPMethod,
path: String,
Expand Down
50 changes: 50 additions & 0 deletions WordPressKitTests/Mock Data/wp-reusable-blocks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"id": 6,
"date": "2021-02-10T11:51:53",
"date_gmt": "2021-02-10T11:51:53",
"guid": {
"rendered": "https:\/\/test-site.org\/2021\/02\/10\/untitled-reusable-block\/",
"raw": "https:\/\/test-site.org\/2021\/02\/10\/untitled-reusable-block\/"
},
"modified": "2021-02-10T12:31:39",
"modified_gmt": "2021-02-10T12:31:39",
"password": "",
"slug": "untitled-reusable-block",
"status": "publish",
"type": "wp_block",
"link": "https:\/\/test-site.org\/2021\/02\/10\/untitled-reusable-block\/",
"title": {
"raw": "A resuable block"
},
"content": {
"raw": "<!-- wp:paragraph -->\n<p>Some text<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:list -->\n<ul><li>Item 1<\/li><li>Item 2<\/li><li>Item 3<\/li><\/ul>\n<!-- \/wp:list -->",
"protected": false,
"block_version": 1
},
"template": "",
"_links": {
"self": [{
"href": "https:\/\/test-site.org\/wp-json\/wp\/v2\/blocks\/6"
}],
"collection": [{
"href": "https:\/\/test-site.org\/wp-json\/wp\/v2\/blocks"
}],
"about": [{
"href": "https:\/\/test-site.org\/wp-json\/wp\/v2\/types\/wp_block"
}],
"wp:attachment": [{
"href": "https:\/\/test-site.org\/wp-json\/wp\/v2\/media?parent=6"
}],
"wp:action-publish": [{
"href": "https:\/\/test-site.org\/wp-json\/wp\/v2\/blocks\/6"
}],
"wp:action-unfiltered-html": [{
"href": "https:\/\/test-site.org\/wp-json\/wp\/v2\/blocks\/6"
}],
"curies": [{
"name": "wp",
"href": "https:\/\/api.w.org\/{rel}",
"templated": true
}]
}
}
32 changes: 31 additions & 1 deletion WordPressKitTests/WordPressOrgRestApiTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class WordPressOrgRestApiTests: XCTestCase {
waitForExpectations(timeout: 2, handler: nil)
}

func testSuccessfulCall() {
func testSuccessfulGetCall() {
stub(condition: isAPIRequest()) { _ in
let stubPath = OHPathForFile("wp-pages.json", type(of: self))
return fixture(filePath: stubPath!, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
Expand All @@ -63,4 +63,34 @@ class WordPressOrgRestApiTests: XCTestCase {
}
waitForExpectations(timeout: 2, handler: nil)
}

func testSuccessfulPostCall() {
stub(condition: isAPIRequest()) { _ in
let stubPath = OHPathForFile("wp-reusable-blocks.json", type(of: self))
return fixture(filePath: stubPath!, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
}
let expect = self.expectation(description: "One callback should be invoked")
let api = WordPressOrgRestApi(apiBase: apiBase)
let blockContent = "<!-- wp:paragraph -->\n<p>Some text</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:list -->\n<ul><li>Item 1</li><li>Item 2</li><li>Item 3</li></ul>\n<!-- /wp:list -->"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a long value to use in the test given that the response is stubbed. Is there a particular reason for use it? Having an example with real-world-like input?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could make it shorter, the only condition is to match the content from the stub:

"raw": "<!-- wp:paragraph -->\n<p>Some text<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:list -->\n<ul><li>Item 1<\/li><li>Item 2<\/li><li>Item 3<\/li><\/ul>\n<!-- \/wp:list -->",

let parameters: [String: String] = ["id": "6", "content": blockContent]
api.POST("wp/v2/blocks/6", parameters: parameters as [String: AnyObject]) { (result, _) in
expect.fulfill()
switch result {
case .success(let object):
guard
let block = object as? [String: AnyObject],
let content = block["content"] as? [String: AnyObject],
let rawContent = content["raw"] as? String
else {
XCTFail("Unexpected API result")
return
}
XCTAssertEqual(block.count, 15, "The API should return the block")
XCTAssertEqual(rawContent, blockContent, "The API should return the block")
case .failure:
XCTFail("This call should not fail")
}
}
waitForExpectations(timeout: 2, handler: nil)
}
}