Skip to content

Commit

Permalink
Allow Status Code Decoding (#30)
Browse files Browse the repository at this point in the history
* setting up network calls

* refactoring service
  • Loading branch information
leogdion authored Oct 12, 2023
1 parent 2a6dd91 commit b9329ba
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Sources/Prch/Service.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public protocol Service<SessionType>: ServiceProtocol {
}

extension Service {
public var authorizationManager: any SessionAuthenticationManager {
NullAuthorizationManager()
}
public func request<RequestType>(
_ request: RequestType
) async throws -> RequestType.SuccessType.DecodableType
Expand All @@ -35,6 +38,7 @@ extension Service {

return try request.resolveDecoder(with: api).decodeContent(
RequestType.SuccessType.self,
code: response.statusCode,
from: response.data
)
}
Expand Down
1 change: 1 addition & 0 deletions Sources/PrchModel/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extension Array: ContentDecodable
where Element: ContentDecodable & Decodable, Element.DecodableType == Element {
public static func decode<DecoderType, DataType>(
_ data: DataType,
code: Int,
using coder: DecoderType
) throws -> [Element.DecodableType] where DecoderType: Decoder<DataType> {
try coder.decode([Element.DecodableType].self, from: data)
Expand Down
1 change: 1 addition & 0 deletions Sources/PrchModel/ContentDecodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public protocol ContentDecodable {
init(decoded: DecodableType) throws
static func decode<CoderType: Decoder>(
_ data: CoderType.DataType,
code: Int,
using coder: CoderType
) throws -> DecodableType
}
1 change: 1 addition & 0 deletions Sources/PrchModel/Decodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extension Decodable

public static func decode<CoderType>(
_ data: CoderType.DataType,
code: Int,
using coder: CoderType
) throws -> Self where CoderType: Decoder {
try coder.decode(Self.self, from: data)
Expand Down
3 changes: 2 additions & 1 deletion Sources/PrchModel/Decoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ public protocol Decoder<DataType> {
extension Decoder {
public func decodeContent<CodableType: ContentDecodable>(
_: CodableType.Type,
code: Int,
from data: DataType
)
throws -> CodableType.DecodableType {
try CodableType.decode(data, using: self)
try CodableType.decode(data, code: code, using: self)
}
}
2 changes: 2 additions & 0 deletions Sources/PrchModel/Empty.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
public struct Empty: ContentDecodable, ContentEncodable, Equatable {
public static func decode<CoderType>(
_: CoderType.DataType,
code: Int,
using _: CoderType

) throws where CoderType: Decoder {}

public static var decodable: Void.Type {
Expand Down
4 changes: 2 additions & 2 deletions Sources/PrchModel/ServiceCall.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public protocol ServiceCall {
func isValidStatusCode(_ statusCode: Int) -> Bool
}

extension ServiceCall {
public extension ServiceCall {
var parameters: [String: String] { [:] }

var method: RequestMethod { .GET }
Expand All @@ -22,7 +22,7 @@ extension ServiceCall {

static var requiresCredentials: Bool { false }

public func isValidStatusCode(
func isValidStatusCode(
_ statusCode: Int
) -> Bool {
statusCode / 100 == 2
Expand Down

0 comments on commit b9329ba

Please sign in to comment.