Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 1 addition & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ let package = Package(
targets: ["azureSwiftRuntime"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/ReactiveX/RxSwift.git", from: "3.4.2"),
.package(url: "https://github.com/ReactiveX/RxSwift.git", from: "5.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
4 changes: 1 addition & 3 deletions Sources/azureSwiftRuntime/ApplicationTokenCredentials.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import Foundation
import RxSwift
import RxBlocking

public struct AuthResults: Codable {
let ext_expires_in: String
Expand Down Expand Up @@ -85,8 +84,7 @@ public class ApplicationTokenCredentials: AzureTokenCredentials {

private func acquireTokenSync(forResource: String) throws -> AuthResults? {
let obs : Single<AuthResults> = self.acquireToken(forResource: forResource)
let obsBl = obs.toBlocking()
return try obsBl.single()
return SyncWrapper.EnsureCompletion(with: obs.asObservable())?.element;
}

private func acquireToken<T>(forResource: String) -> Single<T> where T:Decodable {
Expand Down
3 changes: 2 additions & 1 deletion Sources/azureSwiftRuntime/AuthFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public struct AuthFile {
throw AuthFileError.stringData
}

return try decoder.decode(AuthFileData.self, from: jsonData)
let authFileData = try decoder.decode(AuthFileData.self, from: jsonData)
return authFileData
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/azureSwiftRuntime/AzureClient-HandleErrors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

internal extension AzureClient {
internal func handleErrorCode(statusCode: Int, data: Data?) throws {
func handleErrorCode(statusCode: Int, data: Data?) throws {
if statusCode >= 400 {
if let data_ = data {
throw RuntimeError.errorStatusCode(code: statusCode, data: data_)
Expand Down
8 changes: 4 additions & 4 deletions Sources/azureSwiftRuntime/AzureClient-Interception.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import RxSwift

public extension AzureClient {

public func withRequestInterceptor(_ requestInterceptor: RequestInterceptor) -> AzureClient {
func withRequestInterceptor(_ requestInterceptor: RequestInterceptor) -> AzureClient {
self.requestInterceptors.append(requestInterceptor)
return self
}

public func withResponseInterceptor(_ responseInterceptor: ResponseInterceptor) -> AzureClient {
func withResponseInterceptor(_ responseInterceptor: ResponseInterceptor) -> AzureClient {
self.responseInterceptors.append(responseInterceptor)
return self
}
Expand All @@ -34,12 +34,12 @@ public extension AzureClient {
try self.handleErrorCode(statusCode: httpResponse.statusCode, data: data)
return (_httpResponse, _data)
}.retryWhen { (e: Observable<Error>) -> Observable<Int> in
return e.flatMapWithIndex { (e, i) -> Observable<Int> in
return e.enumerated().flatMap() { (i, e) -> Observable<Int> in
switch(e) {
case RuntimeError.errorStatusCode(let code, _):
if code >= 500 && i < 2 {
return Observable<Int>.just(i+1)
.delay(RxTimeInterval(self.retryDelay), scheduler: ConcurrentDispatchQueueScheduler(queue: self.queueWorker))
.delay(RxTimeInterval.milliseconds(self.retryDelay), scheduler: ConcurrentDispatchQueueScheduler(queue: self.queueWorker))
} else {
return Observable.error(e)
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/azureSwiftRuntime/AzureClient-LRO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import RxSwift

public extension AzureClient {

public func executeAsyncLRO<T>(command: BaseCommand, completionHandler: @escaping (T?, Error?) -> Void) where T : Decodable {
func executeAsyncLRO<T>(command: BaseCommand, completionHandler: @escaping (T?, Error?) -> Void) where T : Decodable {
self.createExecuteObservableLRO(command: command)
.subscribe(
onNext: { (httpResponse, data) in
Expand All @@ -26,7 +26,7 @@ public extension AzureClient {
).disposed(by: disposeBag)
}

public func executeAsyncLRO(command: BaseCommand, completionHandler: @escaping (Error?) -> Void) {
func executeAsyncLRO(command: BaseCommand, completionHandler: @escaping (Error?) -> Void) {
self.createExecuteObservableLRO(command: command)
.subscribe(
onNext: { (httpResponse, data) in
Expand Down Expand Up @@ -111,12 +111,12 @@ public extension AzureClient {

return (httpResponse, data)
}.retryWhen { (e: Observable<Error>) -> Observable<Int> in
return e.flatMapWithIndex { (e, i) -> Observable<Int> in
return e.enumerated().flatMap() { (i, e) -> Observable<Int> in
switch(e) {
case RetryConditionError.needRetry:
print("===", "Delay")
return Observable<Int>.just(i+1)
.delay(RxTimeInterval(self.retryDelay), scheduler: ConcurrentDispatchQueueScheduler(queue: self.queueWorker))
.delay(RxTimeInterval.milliseconds(self.retryDelay), scheduler: ConcurrentDispatchQueueScheduler(queue: self.queueWorker))
default:
return Observable.error(e)
}
Expand Down
38 changes: 21 additions & 17 deletions Sources/azureSwiftRuntime/AzureClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import Foundation
import RxSwift
import RxBlocking

open class AzureClient: RuntimeClient {
private static let DEFAULT_USER_AGENT = "AutoRest-Swift";
Expand Down Expand Up @@ -47,20 +46,25 @@ open class AzureClient: RuntimeClient {

let (url, method, headers, body) = try self.prepareRequest(command: command)

guard let (httpResponse, data) = try self.executeRequestWithInterception(url: url, method: method, headers: headers, body: body)
.toBlocking()
.single() else {

throw RuntimeError.general(message: "Request returned nil")
}

try self.handleErrorCode(statusCode: httpResponse.statusCode, data: data)
let result = self.executeRequestWithInterception(url: url, method: method, headers: headers, body: body)
//throw RuntimeError.general(message: "Request returned nil")


do {
let decodable = try command.returnFunc(data: data!)
return decodable
} catch DecodeError.nilData { // to return nil not empty string or data
return nil
if let responseData = SyncWrapper.EnsureCompletion(with: result.asObservable()) {
if let error = responseData.error {
throw error
}

do {
let (httpResponse, data) = responseData.element!
try self.handleErrorCode(statusCode: httpResponse.statusCode, data: data)
let decodable = try command.returnFunc(data: data!)
return decodable
} catch DecodeError.nilData { // to return nil not empty string or data
return nil
}
} else {
throw RuntimeError.general(message: "Request returned nil")
}
}

Expand All @@ -86,10 +90,10 @@ open class AzureClient: RuntimeClient {
}
}

public var retryDelay = 0.01
public var retryDelay = 1

open func withRetryDelay(_ retryDelay: TimeInterval) {
self.retryDelay = retryDelay
open func withRetryDelay(_ milliseconds: Int) {
self.retryDelay = milliseconds
}

public var requestInterceptors: [RequestInterceptor] = []
Expand Down
2 changes: 1 addition & 1 deletion Sources/azureSwiftRuntime/DateConverter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class DateConverter {
}

public extension Date {
public init?(fromString: String, format: AzureDateFormat) {
init?(fromString: String, format: AzureDateFormat) {
let dateStr = fromString.uppercased()
if let date = format.toDate(dateStr: dateStr) {
self = date
Expand Down
27 changes: 27 additions & 0 deletions Sources/azureSwiftRuntime/SyncWrapper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// SyncWrapper.swift
// RxSwift
//
// Created by Alva D Bandy on 6/22/19.
//

import Foundation
import RxSwift

public class SyncWrapper {
public class func EnsureCompletion<T>(with: Observable<T>) -> Event<T>? {
let group = DispatchGroup()
group.enter()
var retVal: Event<T>?
// avoid deadlocks by not using .main queue here
_ = with.subscribe( { event in
if retVal == nil {
retVal = event
group.leave()
}
})

group.wait()
return retVal
}
}
2 changes: 1 addition & 1 deletion Sources/azureSwiftRuntime/XMLDecodingError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal extension DecodingError {
/// - parameter expectation: The type expected to be encountered.
/// - parameter reality: The value that was encountered instead of the expected type.
/// - returns: A `DecodingError` with the appropriate path and debug description.
internal static func _typeMismatch(at path: [CodingKey], expectation: Any.Type, reality: Any) -> DecodingError {
static func _typeMismatch(at path: [CodingKey], expectation: Any.Type, reality: Any) -> DecodingError {
let description = "Expected to decode \(expectation) but found \(_typeDescription(of: reality)) instead."
return .typeMismatch(expectation, Context(codingPath: path, debugDescription: description))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ struct PagingNamespace {
}

public override func returnFunc(data: Data) throws -> Decodable? {
return try CoderFactory.decoder(for: .json).decode(ProductResultProtocol?.self, from: data)
return try CoderFactory.decoder(for: .json).decode(ProductResultData?.self, from: data)
}

let firstPage = "/paging/single/failure"
Expand Down