Skip to content

Commit

Permalink
[Refactoring] Added @discardableResult and use shared session from fa…
Browse files Browse the repository at this point in the history
…ctory (#36)
  • Loading branch information
joerghartmann authored Jan 8, 2025
1 parent 0ba3a7c commit 12b5c5d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class AdaptableApi {
let requestBuilder: URLRequestBuilder
let session: URLSession

public init(requestBuilder: URLRequestBuilder = URLRequestBuilder(), withSession session: URLSession = URLSession.shared) {
public init(requestBuilder: URLRequestBuilder = URLRequestBuilder(), withSession session: URLSession = Cumulocity.Core.shared.session) {
self.requestBuilder = requestBuilder
self.session = session
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,32 @@ public class URLRequestBuilder {
self.queryItems = with.queryItems
}

@discardableResult
public func set(scheme: String) -> URLRequestBuilder {
self.components.scheme = scheme
return self
}

@discardableResult
public func set(host: String) -> URLRequestBuilder {
self.components.host = host
return self
}

@discardableResult
public func set(resourcePath: String) -> URLRequestBuilder {
self.components.path = resourcePath
return self
}

@discardableResult
public func set(httpMethod: String) -> URLRequestBuilder {
self.httpMethod = httpMethod
return self
}

public func add<Subject>(header: String, value: Subject?) -> URLRequestBuilder {
@discardableResult
public func add<Subject>(header: String, value: Subject?) -> URLRequestBuilder {
if let v = value {
let valueAsString = String(describing: v)
guard !valueAsString.isEmpty else {
Expand All @@ -65,8 +70,9 @@ public class URLRequestBuilder {
return self
}

public func set(authorization userName: String, password: String) -> URLRequestBuilder {
let credentials = "\(userName):\(password)"
@discardableResult
public func set(authorization userName: String, password: String) -> URLRequestBuilder {
let credentials = "\(userName):\(password)"
if let encodedCredentials = credentials.data(using: .utf8) {
self.requestHeaders["Authorization"] = "Basic " + encodedCredentials.base64EncodedString()
}
Expand All @@ -76,7 +82,8 @@ public class URLRequestBuilder {
/// Appends a ``URLQueryItem`` based on the passed ``key``/``value`` pair if ``value`` is not nil.
///
/// The parameter will be serialized as `?key=value`.
public func add<Subject>(queryItem key: String, value: Subject?) -> URLRequestBuilder {
@discardableResult
public func add<Subject>(queryItem key: String, value: Subject?) -> URLRequestBuilder {
if let v = value {
let valueAsString = String(describing: v)
guard !valueAsString.isEmpty else {
Expand All @@ -92,6 +99,7 @@ public class URLRequestBuilder {
/// The parameter `explode` defines the serialisation method:
/// - If `true`, parameter will be serialized as `?key=1&key=2&key=3`.
/// - If `false`, parameter will be serialized as `?key=1,2,3`.
@discardableResult
public func add<Subject>(queryItem key: String, value: [Subject]?, explode: ParameterSerializationType = .exploded) -> URLRequestBuilder {
if let v = value {
if explode == .exploded {
Expand All @@ -105,7 +113,8 @@ public class URLRequestBuilder {
return self
}

public func set(httpBody: Data?) -> URLRequestBuilder {
@discardableResult
public func set(httpBody: Data?) -> URLRequestBuilder {
self.httpBody = httpBody
return self
}
Expand Down

0 comments on commit 12b5c5d

Please sign in to comment.