Skip to content

Commit

Permalink
Update to newly built Swift Client from GH Run 181
Browse files Browse the repository at this point in the history
  • Loading branch information
openapi-bot committed Nov 21, 2022
1 parent 079830c commit b0c3551
Show file tree
Hide file tree
Showing 62 changed files with 1,333 additions and 508 deletions.
30 changes: 8 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,47 +78,33 @@ C8yAlarm.registerAdditionalProperty<C: Codable>(typeName: String, for type: C.Ty
Each of the extensible objects contains a dictionary object holding instances of custom fragments. Use the custom fragment's key to access it's value.

In addition, developers may create subclasses. The client implementation respects subclassing by using generic type arguments.
### Working with errors

Use `sink(receiveCompletion:receiveValue:)` to observe values received by the publisher and process them using a closure you specify. HTTP error codes will be forwarded and can be accessed using a completion handler. The client describes two error types:

```swift
enum Errors: Error {
case undescribedError(statusCode: Int, response: HTTPURLResponse)
case badResponseError(statusCode: Int, reason: Codable)
}
```
### Working with errors

The type `badResponseError` is used when there is dedicated error message sent by the server. The `reason` may be a `string` or any other `Codable` instance. When there is no dedicated error message, `undescribedError` will be used.
Use `sink(receiveCompletion:receiveValue:)` to observe values received by the publisher and process them using a closure you specify. HTTP error codes will be forwarded and can be accessed using a completion handler.

```swift
sink(receiveCompletion: { completion in
switch completion {
case .finished:
// handle completion
case .failure(let error):
switch(error) {
case Errors.undescribedError(let statusCode, let response):
print(statusCode)
case Errors.badResponseError(let statusCode, let reason):
print(statusCode)
default:
/// handleError(error)
}
// handle error
}
}, receiveValue: { data in
})
```

Error handling can be simplified by calling one of the following extension methods:
Error handling can be simplified by calling the following extension method:

```swift
sink(receiveCompletion: { completion in
/// access the error message
let error = completion.error()
/// error?.response(): HTTPURLResponse for advanced error processing
/// error?.statusCode(): HTTP status code
/// error?.reason(): Codable reason sent by the server
/// access HTTP response
error?.httpResponse
/// access HTTP status code via the HttpResponse
error?.httpResponse?.statusCode
})
```

Expand Down
43 changes: 29 additions & 14 deletions Sources/CumulocityCoreLibrary/Api/AlarmsApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ public class AlarmsApi: AdaptableApi {
}
guard (200..<300) ~= httpResponse.statusCode else {
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
throw Errors.badResponseError(response: httpResponse, reason: c8yError)
c8yError.httpResponse = httpResponse
throw c8yError
}
throw Errors.undescribedError(response: httpResponse)
throw BadResponseError(with: httpResponse)
}
return element.data
}).decode(type: C8yAlarmCollection.self, decoder: JSONDecoder()).eraseToAnyPublisher()
Expand Down Expand Up @@ -192,9 +193,10 @@ public class AlarmsApi: AdaptableApi {
}
guard (200..<300) ~= httpResponse.statusCode else {
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
throw Errors.badResponseError(response: httpResponse, reason: c8yError)
c8yError.httpResponse = httpResponse
throw c8yError
}
throw Errors.undescribedError(response: httpResponse)
throw BadResponseError(with: httpResponse)
}
return element.data
}).eraseToAnyPublisher()
Expand Down Expand Up @@ -267,9 +269,10 @@ public class AlarmsApi: AdaptableApi {
}
guard (200..<300) ~= httpResponse.statusCode else {
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
throw Errors.badResponseError(response: httpResponse, reason: c8yError)
c8yError.httpResponse = httpResponse
throw c8yError
}
throw Errors.undescribedError(response: httpResponse)
throw BadResponseError(with: httpResponse)
}
return element.data
}).decode(type: C8yAlarm.self, decoder: JSONDecoder()).eraseToAnyPublisher()
Expand Down Expand Up @@ -340,9 +343,10 @@ public class AlarmsApi: AdaptableApi {
}
guard (200..<300) ~= httpResponse.statusCode else {
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
throw Errors.badResponseError(response: httpResponse, reason: c8yError)
c8yError.httpResponse = httpResponse
throw c8yError
}
throw Errors.undescribedError(response: httpResponse)
throw BadResponseError(with: httpResponse)
}
return element.data
}).eraseToAnyPublisher()
Expand Down Expand Up @@ -379,9 +383,14 @@ public class AlarmsApi: AdaptableApi {
}
guard (200..<300) ~= httpResponse.statusCode else {
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
throw Errors.badResponseError(response: httpResponse, reason: c8yError)
c8yError.httpResponse = httpResponse
throw c8yError
}
throw Errors.undescribedError(response: httpResponse)
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
c8yError.httpResponse = httpResponse
throw c8yError
}
throw BadResponseError(with: httpResponse)
}
return element.data
}).decode(type: C8yAlarm.self, decoder: JSONDecoder()).eraseToAnyPublisher()
Expand Down Expand Up @@ -442,9 +451,14 @@ public class AlarmsApi: AdaptableApi {
}
guard (200..<300) ~= httpResponse.statusCode else {
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
throw Errors.badResponseError(response: httpResponse, reason: c8yError)
c8yError.httpResponse = httpResponse
throw c8yError
}
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
c8yError.httpResponse = httpResponse
throw c8yError
}
throw Errors.undescribedError(response: httpResponse)
throw BadResponseError(with: httpResponse)
}
return element.data
}).decode(type: C8yAlarm.self, decoder: JSONDecoder()).eraseToAnyPublisher()
Expand Down Expand Up @@ -504,9 +518,10 @@ public class AlarmsApi: AdaptableApi {
}
guard (200..<300) ~= httpResponse.statusCode else {
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
throw Errors.badResponseError(response: httpResponse, reason: c8yError)
c8yError.httpResponse = httpResponse
throw c8yError
}
throw Errors.undescribedError(response: httpResponse)
throw BadResponseError(with: httpResponse)
}
return element.data
}).decode(type: Int.self, decoder: JSONDecoder()).eraseToAnyPublisher()
Expand Down
24 changes: 16 additions & 8 deletions Sources/CumulocityCoreLibrary/Api/ApplicationBinariesApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ public class ApplicationBinariesApi: AdaptableApi {
}
guard (200..<300) ~= httpResponse.statusCode else {
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
throw Errors.badResponseError(response: httpResponse, reason: c8yError)
c8yError.httpResponse = httpResponse
throw c8yError
}
throw Errors.undescribedError(response: httpResponse)
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
c8yError.httpResponse = httpResponse
throw c8yError
}
throw BadResponseError(with: httpResponse)
}
return element.data
}).decode(type: C8yApplicationBinaries.self, decoder: JSONDecoder()).eraseToAnyPublisher()
Expand Down Expand Up @@ -93,9 +98,10 @@ public class ApplicationBinariesApi: AdaptableApi {
}
guard (200..<300) ~= httpResponse.statusCode else {
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
throw Errors.badResponseError(response: httpResponse, reason: c8yError)
c8yError.httpResponse = httpResponse
throw c8yError
}
throw Errors.undescribedError(response: httpResponse)
throw BadResponseError(with: httpResponse)
}
return element.data
}).decode(type: C8yApplication.self, decoder: JSONDecoder()).eraseToAnyPublisher()
Expand Down Expand Up @@ -131,9 +137,10 @@ public class ApplicationBinariesApi: AdaptableApi {
}
guard (200..<300) ~= httpResponse.statusCode else {
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
throw Errors.badResponseError(response: httpResponse, reason: c8yError)
c8yError.httpResponse = httpResponse
throw c8yError
}
throw Errors.undescribedError(response: httpResponse)
throw BadResponseError(with: httpResponse)
}
return element.data
}).eraseToAnyPublisher()
Expand Down Expand Up @@ -171,9 +178,10 @@ public class ApplicationBinariesApi: AdaptableApi {
}
guard (200..<300) ~= httpResponse.statusCode else {
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
throw Errors.badResponseError(response: httpResponse, reason: c8yError)
c8yError.httpResponse = httpResponse
throw c8yError
}
throw Errors.undescribedError(response: httpResponse)
throw BadResponseError(with: httpResponse)
}
return element.data
}).eraseToAnyPublisher()
Expand Down
66 changes: 46 additions & 20 deletions Sources/CumulocityCoreLibrary/Api/ApplicationsApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ public class ApplicationsApi: AdaptableApi {
}
guard (200..<300) ~= httpResponse.statusCode else {
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
throw Errors.badResponseError(response: httpResponse, reason: c8yError)
c8yError.httpResponse = httpResponse
throw c8yError
}
throw Errors.undescribedError(response: httpResponse)
throw BadResponseError(with: httpResponse)
}
return element.data
}).decode(type: C8yApplicationCollection.self, decoder: JSONDecoder()).eraseToAnyPublisher()
Expand Down Expand Up @@ -134,9 +135,14 @@ public class ApplicationsApi: AdaptableApi {
}
guard (200..<300) ~= httpResponse.statusCode else {
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
throw Errors.badResponseError(response: httpResponse, reason: c8yError)
c8yError.httpResponse = httpResponse
throw c8yError
}
throw Errors.undescribedError(response: httpResponse)
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
c8yError.httpResponse = httpResponse
throw c8yError
}
throw BadResponseError(with: httpResponse)
}
return element.data
}).decode(type: C8yApplication.self, decoder: JSONDecoder()).eraseToAnyPublisher()
Expand Down Expand Up @@ -171,9 +177,14 @@ public class ApplicationsApi: AdaptableApi {
}
guard (200..<300) ~= httpResponse.statusCode else {
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
throw Errors.badResponseError(response: httpResponse, reason: c8yError)
c8yError.httpResponse = httpResponse
throw c8yError
}
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
c8yError.httpResponse = httpResponse
throw c8yError
}
throw Errors.undescribedError(response: httpResponse)
throw BadResponseError(with: httpResponse)
}
return element.data
}).decode(type: C8yApplication.self, decoder: JSONDecoder()).eraseToAnyPublisher()
Expand Down Expand Up @@ -224,9 +235,14 @@ public class ApplicationsApi: AdaptableApi {
}
guard (200..<300) ~= httpResponse.statusCode else {
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
throw Errors.badResponseError(response: httpResponse, reason: c8yError)
c8yError.httpResponse = httpResponse
throw c8yError
}
throw Errors.undescribedError(response: httpResponse)
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
c8yError.httpResponse = httpResponse
throw c8yError
}
throw BadResponseError(with: httpResponse)
}
return element.data
}).decode(type: C8yApplication.self, decoder: JSONDecoder()).eraseToAnyPublisher()
Expand Down Expand Up @@ -271,9 +287,14 @@ public class ApplicationsApi: AdaptableApi {
}
guard (200..<300) ~= httpResponse.statusCode else {
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
throw Errors.badResponseError(response: httpResponse, reason: c8yError)
c8yError.httpResponse = httpResponse
throw c8yError
}
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
c8yError.httpResponse = httpResponse
throw c8yError
}
throw Errors.undescribedError(response: httpResponse)
throw BadResponseError(with: httpResponse)
}
return element.data
}).eraseToAnyPublisher()
Expand Down Expand Up @@ -315,9 +336,10 @@ public class ApplicationsApi: AdaptableApi {
}
guard (200..<300) ~= httpResponse.statusCode else {
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
throw Errors.badResponseError(response: httpResponse, reason: c8yError)
c8yError.httpResponse = httpResponse
throw c8yError
}
throw Errors.undescribedError(response: httpResponse)
throw BadResponseError(with: httpResponse)
}
return element.data
}).decode(type: C8yApplication.self, decoder: JSONDecoder()).eraseToAnyPublisher()
Expand Down Expand Up @@ -350,9 +372,10 @@ public class ApplicationsApi: AdaptableApi {
}
guard (200..<300) ~= httpResponse.statusCode else {
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
throw Errors.badResponseError(response: httpResponse, reason: c8yError)
c8yError.httpResponse = httpResponse
throw c8yError
}
throw Errors.undescribedError(response: httpResponse)
throw BadResponseError(with: httpResponse)
}
return element.data
}).decode(type: C8yApplicationCollection.self, decoder: JSONDecoder()).eraseToAnyPublisher()
Expand Down Expand Up @@ -385,9 +408,10 @@ public class ApplicationsApi: AdaptableApi {
}
guard (200..<300) ~= httpResponse.statusCode else {
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
throw Errors.badResponseError(response: httpResponse, reason: c8yError)
c8yError.httpResponse = httpResponse
throw c8yError
}
throw Errors.undescribedError(response: httpResponse)
throw BadResponseError(with: httpResponse)
}
return element.data
}).decode(type: C8yApplicationCollection.self, decoder: JSONDecoder()).eraseToAnyPublisher()
Expand Down Expand Up @@ -434,9 +458,10 @@ public class ApplicationsApi: AdaptableApi {
}
guard (200..<300) ~= httpResponse.statusCode else {
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
throw Errors.badResponseError(response: httpResponse, reason: c8yError)
c8yError.httpResponse = httpResponse
throw c8yError
}
throw Errors.undescribedError(response: httpResponse)
throw BadResponseError(with: httpResponse)
}
return element.data
}).decode(type: C8yApplicationCollection.self, decoder: JSONDecoder()).eraseToAnyPublisher()
Expand Down Expand Up @@ -483,9 +508,10 @@ public class ApplicationsApi: AdaptableApi {
}
guard (200..<300) ~= httpResponse.statusCode else {
if let c8yError = try? JSONDecoder().decode(C8yError.self, from: element.data) {
throw Errors.badResponseError(response: httpResponse, reason: c8yError)
c8yError.httpResponse = httpResponse
throw c8yError
}
throw Errors.undescribedError(response: httpResponse)
throw BadResponseError(with: httpResponse)
}
return element.data
}).decode(type: C8yApplicationCollection.self, decoder: JSONDecoder()).eraseToAnyPublisher()
Expand Down
Loading

0 comments on commit b0c3551

Please sign in to comment.