Skip to content

Commit 7261aaa

Browse files
committed
Update ReplicateAPI.swift
1 parent 8104188 commit 7261aaa

File tree

1 file changed

+14
-30
lines changed

1 file changed

+14
-30
lines changed

Sources/replicate-kit-swift/ReplicateAPI.swift

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ public struct ReplicateAPI{
7575
public func getCollections(collection_slug : String) async throws -> CollectionOfModels{
7676

7777
let path = "collections/\(collection_slug)"
78-
let result : Http.Response<CollectionOfModels> = try await client.get(path: path)
78+
let rule = [Http.Validate.status(.range(200..<300))]
79+
let result : Http.Response<CollectionOfModels> = try await client.get(path: path, validate: rule)
7980

80-
return try validate(result)
81+
return result.value
8182
}
8283

8384
/// Get a model
@@ -87,9 +88,10 @@ public struct ReplicateAPI{
8788
public func getModel(owner: String, name: String) async throws -> Model{
8889

8990
let path = "models/\(owner)/\(name)"
90-
let result : Http.Response<Model> = try await client.get(path: path)
91+
let rule = [Http.Validate.status(.range(200..<300))]
92+
let result : Http.Response<Model> = try await client.get(path: path, validate: rule)
9193

92-
return try validate(result)
94+
return result.value
9395
}
9496

9597
/// Create prediction
@@ -178,11 +180,13 @@ public struct ReplicateAPI{
178180
by id : String
179181
) async throws -> Prediction<Output>{
180182

183+
let rule = [Http.Validate.status(.range(200..<300))]
181184
let result : Http.Response<Prediction<Output>> = try await client.get(
182-
path: "predictions/\(id)"
185+
path: "predictions/\(id)",
186+
validate: rule
183187
)
184188

185-
return try validate(result)
189+
return result.value
186190
}
187191

188192
/// Calling this operation starts a new prediction for the version and inputs you provide. As models can take several seconds or more to run, the output will not be available immediately. To get the final result of the prediction you should either provide a webhook URL for us to call when the results are ready, or poll the get a prediction endpoint until it has one of the terminated statuses.
@@ -192,12 +196,14 @@ public struct ReplicateAPI{
192196
with body : HttpBody<Input>
193197
) async throws -> Prediction<Output>{
194198

199+
let rule = [Http.Validate.status(.range(200..<300))]
195200
let result : Http.Response<Prediction<Output>> = try await client.post(
196201
path: "predictions",
197-
body : body
202+
body : body,
203+
validate: rule
198204
)
199205

200-
return try validate(result)
206+
return result.value
201207
}
202208
}
203209

@@ -235,25 +241,3 @@ fileprivate func sessionCfg (_ token : String) -> URLSessionConfiguration{
235241

236242
return config
237243
}
238-
239-
/// Validate response
240-
/// - Parameters:
241-
/// - response: URLResponse
242-
/// - data: Data
243-
/// - Throws: ReplicateAPI.Errors.invalidResponse
244-
/// - Returns: Data
245-
fileprivate func validate<T>(_ result: Http.Response<T>) throws -> T{
246-
247-
guard let status = result.statusCode else{
248-
let str = String(data: result.data, encoding: .utf8)
249-
throw ReplicateAPI.Errors.invalidResponse(result.urlResponse, str)
250-
}
251-
252-
guard (200..<300).contains(status) else{
253-
let str = String(data: result.data, encoding: .utf8)
254-
throw ReplicateAPI.Errors.invalidResponse(result.urlResponse, str)
255-
}
256-
257-
return result.value
258-
}
259-

0 commit comments

Comments
 (0)