Skip to content

Commit

Permalink
feat(specs): add v2 endpoints for ingestion
Browse files Browse the repository at this point in the history
algolia/api-clients-automation#3416

Co-authored-by: algolia-bot <accounts+algolia-api-client-bot@algolia.com>
Co-authored-by: Clément Vannicatte <vannicattec@gmail.com>
  • Loading branch information
algolia-bot and shortcuts committed Jul 25, 2024
1 parent 9cf23ef commit 3532b6c
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 20 deletions.
70 changes: 70 additions & 0 deletions Sources/Ingestion/IngestionClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2376,6 +2376,76 @@ open class IngestionClient {
)
}

/// - parameter taskID: (path) Unique identifier of a task.
/// - parameter batchWriteParams: (body) Request body of a Search API `batch` request that will be pushed in the
/// Connectors pipeline.
/// - returns: RunResponse
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
open func pushTask(
taskID: String,
batchWriteParams: IngestionBatchWriteParams,
requestOptions: RequestOptions? = nil
) async throws -> RunResponse {
let response: Response<RunResponse> = try await pushTaskWithHTTPInfo(
taskID: taskID,
batchWriteParams: batchWriteParams,
requestOptions: requestOptions
)

guard let body = response.body else {
throw AlgoliaError.missingData
}

return body
}

// Push a `batch` request payload through the Pipeline. You can check the status of task pushes with the
// observability endpoints.
// Required API Key ACLs:
// - addObject
// - deleteIndex
// - editSettings
//
// - parameter taskID: (path) Unique identifier of a task.
//
// - parameter batchWriteParams: (body) Request body of a Search API `batch` request that will be pushed in the
// Connectors pipeline.
// - returns: RequestBuilder<RunResponse>

open func pushTaskWithHTTPInfo(
taskID: String,
batchWriteParams: IngestionBatchWriteParams,
requestOptions userRequestOptions: RequestOptions? = nil
) async throws -> Response<RunResponse> {
guard !taskID.isEmpty else {
throw AlgoliaError.invalidArgument("taskID", "pushTask")
}

var resourcePath = "/2/tasks/{taskID}/push"
let taskIDPreEscape = "\(APIHelper.mapValueToPathItem(taskID))"
let taskIDPostEscape = taskIDPreEscape
.addingPercentEncoding(withAllowedCharacters: .urlPathAlgoliaAllowed) ?? ""
resourcePath = resourcePath.replacingOccurrences(
of: "{taskID}",
with: taskIDPostEscape,
options: .literal,
range: nil
)
let body = batchWriteParams
let queryParameters: [String: Any?]? = nil

let nillableHeaders: [String: Any?]? = nil

let headers = APIHelper.rejectNilHeaders(nillableHeaders)

return try await self.transporter.send(
method: "POST",
path: resourcePath,
data: body,
requestOptions: RequestOptions(headers: headers, queryParameters: queryParameters) + userRequestOptions
)
}

/// - parameter taskID: (path) Unique identifier of a task.
/// - returns: RunResponse
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
Expand Down
20 changes: 20 additions & 0 deletions Sources/Ingestion/Models/IngestionAction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost - read more on
// https://github.com/algolia/api-clients-automation. DO NOT EDIT.

import Foundation
#if canImport(Core)
import Core
#endif

/// Type of indexing operation.
public enum IngestionAction: String, Codable, CaseIterable {
case addObject
case updateObject
case partialUpdateObject
case partialUpdateObjectNoCreate
case deleteObject
case delete
case clear
}

extension IngestionAction: Hashable {}
45 changes: 45 additions & 0 deletions Sources/Ingestion/Models/IngestionBatchRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost - read more on
// https://github.com/algolia/api-clients-automation. DO NOT EDIT.

import Foundation
#if canImport(Core)
import Core
#endif

public struct IngestionBatchRequest: Codable, JSONEncodable {
public var action: IngestionAction
/// Operation arguments (varies with specified `action`).
public var body: AnyCodable

public init(action: IngestionAction, body: AnyCodable) {
self.action = action
self.body = body
}

public enum CodingKeys: String, CodingKey, CaseIterable {
case action
case body
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.action, forKey: .action)
try container.encode(self.body, forKey: .body)
}
}

extension IngestionBatchRequest: Equatable {
public static func ==(lhs: IngestionBatchRequest, rhs: IngestionBatchRequest) -> Bool {
lhs.action == rhs.action &&
lhs.body == rhs.body
}
}

extension IngestionBatchRequest: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(self.action.hashValue)
hasher.combine(self.body.hashValue)
}
}
39 changes: 39 additions & 0 deletions Sources/Ingestion/Models/IngestionBatchWriteParams.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost - read more on
// https://github.com/algolia/api-clients-automation. DO NOT EDIT.

import Foundation
#if canImport(Core)
import Core
#endif

/// Batch parameters.
public struct IngestionBatchWriteParams: Codable, JSONEncodable {
public var requests: [IngestionBatchRequest]

public init(requests: [IngestionBatchRequest]) {
self.requests = requests
}

public enum CodingKeys: String, CodingKey, CaseIterable {
case requests
}

// Encodable protocol methods

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.requests, forKey: .requests)
}
}

extension IngestionBatchWriteParams: Equatable {
public static func ==(lhs: IngestionBatchWriteParams, rhs: IngestionBatchWriteParams) -> Bool {
lhs.requests == rhs.requests
}
}

extension IngestionBatchWriteParams: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(self.requests.hashValue)
}
}
4 changes: 2 additions & 2 deletions Sources/Search/Extra/SearchClientExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ public extension SearchClient {
func chunkedBatch(
indexName: String,
objects: [some Encodable],
action: Action = .addObject,
action: SearchAction = .addObject,
waitForTasks: Bool = false,
batchSize: Int = 1000,
requestOptions: RequestOptions? = nil
Expand All @@ -439,7 +439,7 @@ public extension SearchClient {
for batch in batches {
let batchResponse = try await self.batch(
indexName: indexName,
batchWriteParams: BatchWriteParams(
batchWriteParams: SearchBatchWriteParams(
requests: batch.map {
.init(action: action, body: AnyCodable($0))
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Search/Models/MultipleBatchRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import Foundation
#endif

public struct MultipleBatchRequest: Codable, JSONEncodable {
public var action: Action
public var action: SearchAction
/// Operation arguments (varies with specified `action`).
public var body: AnyCodable
/// Index name (case-sensitive).
public var indexName: String

public init(action: Action, body: AnyCodable, indexName: String) {
public init(action: SearchAction, body: AnyCodable, indexName: String) {
self.action = action
self.body = body
self.indexName = indexName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Foundation
#endif

/// Type of indexing operation.
public enum Action: String, Codable, CaseIterable {
public enum SearchAction: String, Codable, CaseIterable {
case addObject
case updateObject
case partialUpdateObject
Expand All @@ -17,4 +17,4 @@ public enum Action: String, Codable, CaseIterable {
case clear
}

extension Action: Hashable {}
extension SearchAction: Hashable {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import Foundation
import Core
#endif

public struct BatchRequest: Codable, JSONEncodable {
public var action: Action
public struct SearchBatchRequest: Codable, JSONEncodable {
public var action: SearchAction
/// Operation arguments (varies with specified `action`).
public var body: AnyCodable

public init(action: Action, body: AnyCodable) {
public init(action: SearchAction, body: AnyCodable) {
self.action = action
self.body = body
}
Expand All @@ -30,14 +30,14 @@ public struct BatchRequest: Codable, JSONEncodable {
}
}

extension BatchRequest: Equatable {
public static func ==(lhs: BatchRequest, rhs: BatchRequest) -> Bool {
extension SearchBatchRequest: Equatable {
public static func ==(lhs: SearchBatchRequest, rhs: SearchBatchRequest) -> Bool {
lhs.action == rhs.action &&
lhs.body == rhs.body
}
}

extension BatchRequest: Hashable {
extension SearchBatchRequest: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(self.action.hashValue)
hasher.combine(self.body.hashValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import Foundation
#endif

/// Batch parameters.
public struct BatchWriteParams: Codable, JSONEncodable {
public var requests: [BatchRequest]
public struct SearchBatchWriteParams: Codable, JSONEncodable {
public var requests: [SearchBatchRequest]

public init(requests: [BatchRequest]) {
public init(requests: [SearchBatchRequest]) {
self.requests = requests
}

Expand All @@ -26,13 +26,13 @@ public struct BatchWriteParams: Codable, JSONEncodable {
}
}

extension BatchWriteParams: Equatable {
public static func ==(lhs: BatchWriteParams, rhs: BatchWriteParams) -> Bool {
extension SearchBatchWriteParams: Equatable {
public static func ==(lhs: SearchBatchWriteParams, rhs: SearchBatchWriteParams) -> Bool {
lhs.requests == rhs.requests
}
}

extension BatchWriteParams: Hashable {
extension SearchBatchWriteParams: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(self.requests.hashValue)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Search/SearchClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ open class SearchClient {
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
open func batch(
indexName: String,
batchWriteParams: BatchWriteParams,
batchWriteParams: SearchBatchWriteParams,
requestOptions: RequestOptions? = nil
) async throws -> BatchResponse {
let response: Response<BatchResponse> = try await batchWithHTTPInfo(
Expand Down Expand Up @@ -295,7 +295,7 @@ open class SearchClient {

open func batchWithHTTPInfo(
indexName: String,
batchWriteParams: BatchWriteParams,
batchWriteParams: SearchBatchWriteParams,
requestOptions userRequestOptions: RequestOptions? = nil
) async throws -> Response<BatchResponse> {
guard !indexName.isEmpty else {
Expand Down

0 comments on commit 3532b6c

Please sign in to comment.