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

Return context and structured metadata in rename API #381

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import Foundation
- parameter to: The new identifier to assign to the uploaded asset.
- parameter overwrite: A boolean parameter indicating whether or not to overwrite an existing image with the target Public ID. Default: false.
- parameter invalidate: A boolean parameter indicating whether to invalidate CDN cached copies of the image (and all its transformed versions). Default: false.
- parameter context: A boolean parameter indicating whether to include contextual metadata for the asset in the response. Default: false.
- parameter metadata: A boolean parameter indicating whether to include structured metadata for the asset in the response. Default: false.
- parameter params: An object holding the available parameters for the request.
- parameter completionHandler: The closure to be called once the request has finished, holding either the response object or the error.

Expand All @@ -57,8 +59,8 @@ import Foundation
as well as performing actions on the request, such as cancelling, suspending or resuming it.
*/
@discardableResult
open func rename(_ publicId: String, to: String, overwrite: Bool? = nil, invalidate: Bool? = nil, params: CLDRenameRequestParams? = nil, completionHandler: ((_ result: CLDRenameResult?, _ error: Error?) -> ())? = nil) -> CLDRenameRequest {
let renameParams = CLDRenameRequestParams(fromPublicId: publicId, toPublicId: to, overwrite: overwrite, invalidate: invalidate)
open func rename(_ publicId: String, to: String, overwrite: Bool? = nil, invalidate: Bool? = nil, params: CLDRenameRequestParams? = nil, context: Bool? = nil, metadata: Bool? = nil, completionHandler: ((_ result: CLDRenameResult?, _ error: Error?) -> ())? = nil) -> CLDRenameRequest {
Copy link
Contributor

@adimiz1 adimiz1 Apr 3, 2022

Choose a reason for hiding this comment

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

@const-cloudinary I think we should put the new optional parameters only in the CLRenameRequestParams and not part of the rename function

Copy link
Contributor

Choose a reason for hiding this comment

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

@AlexPinhasovLili please put the context and metadata params only in the CLDRenameRequestParams and not part of the rename function

let renameParams = CLDRenameRequestParams(fromPublicId: publicId, toPublicId: to, overwrite: overwrite, invalidate: invalidate, context: context, metadata: metadata)
renameParams.merge(params)
let request = networkCoordinator.callAction(.Rename, params:renameParams)
let renameRequest = CLDRenameRequest(networkRequest: request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,19 @@ import Foundation
- parameter toPublicId: The new identifier to assign to the uploaded asset.
- parameter overwrite: A boolean parameter indicating whether or not to overwrite an existing image with the target Public ID. Default: false.
- parameter invalidate: A boolean parameter whether to invalidate CDN cached copies of the image (and all its transformed versions). Default: false.
- parameter context: A boolean parameter indicating whether to include contextual metadata for the asset in the response. Default: false.
- parameter metadata: A boolean parameter indicating whether to include structured metadata for the asset in the response. Default: false.

- returns: A new instance of CLDRenameRequestParams.
*/
internal init(fromPublicId: String, toPublicId: String, overwrite: Bool? = nil, invalidate: Bool? = nil) {
internal init(fromPublicId: String, toPublicId: String, overwrite: Bool? = nil, invalidate: Bool? = nil, context: Bool? = nil, metadata: Bool? = nil) {
super.init()
setParam(RenameParams.FromPublicId.rawValue, value: fromPublicId)
setParam(RenameParams.ToPublicId.rawValue, value: toPublicId)
setParam(RenameParams.Overwrite.rawValue, value: overwrite)
setParam(RenameParams.Invalidate.rawValue, value: invalidate)
setParam(RenameParams.context.rawValue, value: context)
setParam(RenameParams.metadata.rawValue, value: metadata)
}

/**
Expand All @@ -73,5 +77,7 @@ import Foundation
case ToPublicId = "to_public_id"
case Overwrite = "overwrite"
case Invalidate = "invalidate"
case context = "context"
case metadata = "metadata"
}
}
107 changes: 106 additions & 1 deletion Example/Tests/NetworkTests/ManagementApiTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import XCTest


class ManagementApiTests: NetworkBaseTest {


private var allowFolderDecouplingCalls: Bool = false

// MARK: - rename
func testRename() {

Expand Down Expand Up @@ -85,6 +87,109 @@ class ManagementApiTests: NetworkBaseTest {
XCTAssertNil(error, "error should be nil")
XCTAssertNotNil(result, "response should not be nil")
}

func testRenameShouldReturnContext() throws {

try XCTSkipUnless(allowFolderDecouplingCalls, "prevents redundant call to Cloudinary PAID Folder Decoupling service. to allow Folder Decoupling service testing - set to true")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@adimiz1 @const-cloudinary Let me know what the service I am trying to use thats blocking me because of my free account type, il change that afterward.


let expectation = self.expectation(description: "Rename should succeed")

var result: CLDRenameResult?
var error: Error?

uploadFile().response({ (uploadResult, uploadError) in
if let publicId = uploadResult?.publicId {
let toRename = publicId + "__APPENDED STRING"
self.cloudinary!.createManagementApi().rename(publicId, to: toRename, overwrite: true, invalidate: true, context: true).response({ (resultRes, errorRes) in
result = resultRes
error = errorRes

expectation.fulfill()
})
}
else {
error = uploadError
expectation.fulfill()
}
})

waitForExpectations(timeout: timeout, handler: nil)

XCTAssertNil(error, "error should be nil")
XCTAssertNotNil(result?.context, "response should not be nil")
}

func testRenameShouldReturnContextSimplifiedToRequest() {
do {
var urlRequest = URLRequest(url: URL(string: "https://example.com/image/rename")!)
urlRequest.httpMethod = CLDNHTTPMethod.post.rawValue
let renameParams = CLDRenameRequestParams(fromPublicId: "from", toPublicId: "to", overwrite: true, invalidate: true, context: true, metadata: false)

let encodedURLRequest = try CLDNURLEncoding.default.CLDN_Encode(urlRequest, with: renameParams.params)

XCTAssertNotNil(encodedURLRequest.httpBody, "HTTPBody should not be nil")

if let httpBody = encodedURLRequest.httpBody, let decodedHTTPBody = String(data: httpBody, encoding: .utf8) {
XCTAssertTrue(decodedHTTPBody.contains("context=1") && decodedHTTPBody.contains("metadata=0"))
} else {
XCTFail("decoded http body should not be nil")
}
} catch {
XCTFail("Test encountered unexpected error: \(error)")
}
}

func testRenameShouldReturnMetadata() throws {

try XCTSkipUnless(allowFolderDecouplingCalls, "prevents redundant call to Cloudinary PAID Folder Decoupling service. to allow Folder Decoupling service testing - set to true")

let expectation = self.expectation(description: "Rename should succeed")

var result: CLDRenameResult?
var error: Error?

uploadFile().response({ (uploadResult, uploadError) in
if let publicId = uploadResult?.publicId {
let toRename = publicId + "__APPENDED STRING"
self.cloudinary!.createManagementApi().rename(publicId, to: toRename, overwrite: true, invalidate: true, metadata: true).response({ (resultRes, errorRes) in
result = resultRes
error = errorRes

expectation.fulfill()
})
}
else {
error = uploadError
expectation.fulfill()
}
})

waitForExpectations(timeout: timeout, handler: nil)

XCTAssertNil(error, "error should be nil")
XCTAssertNotNil(result?.metadata, "response should not be nil")
}

func testRenameShouldReturnMetadataSimplifiedToRequest() {
do {
var urlRequest = URLRequest(url: URL(string: "https://example.com/image/rename")!)
urlRequest.httpMethod = CLDNHTTPMethod.post.rawValue
let renameParams = CLDRenameRequestParams(fromPublicId: "from", toPublicId: "to", overwrite: true, invalidate: true, context: false, metadata: true)

// When
let encodedURLRequest = try CLDNURLEncoding.default.CLDN_Encode(urlRequest, with: renameParams.params)

XCTAssertNotNil(encodedURLRequest.httpBody, "HTTPBody should not be nil")

if let httpBody = encodedURLRequest.httpBody, let decodedHTTPBody = String(data: httpBody, encoding: .utf8) {
XCTAssertTrue(decodedHTTPBody.contains("metadata=1") && decodedHTTPBody.contains("context=0"))
} else {
XCTFail("decoded http body should not be nil")
}
} catch {
XCTFail("Test encountered unexpected error: \(error)")
}
}

// MARK: - explicit
func testExplicit() {
Expand Down