-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4cb8f8f
commit 4040867
Showing
4 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// | ||
// Endpoint.swift | ||
// iosApp | ||
// | ||
// Created by Sarvesh Sharma on 11/12/24. | ||
// Copyright © 2024 orgName. All rights reserved. | ||
// | ||
import Foundation | ||
|
||
enum Tranport { | ||
case http | ||
case grpc | ||
} | ||
|
||
enum HTTPMethod: String { | ||
case get | ||
case post | ||
case put | ||
case delete | ||
} | ||
|
||
struct Endpoint { | ||
let transport: Tranport | ||
let name: String | ||
|
||
// HTTP fields | ||
let baseURL: URL? | ||
let path: String? | ||
let httpMethod: HTTPMethod? | ||
let headers: [String: String]? | ||
let queryItems: [URLQueryItem]? | ||
let httpBody: Data? | ||
|
||
// grpc fields | ||
let serviceName: String? | ||
let methodName: String? | ||
let grpcRequestData: Data? | ||
|
||
// HTTP initializer | ||
init(http name: String, | ||
baseURL: URL, | ||
path: String, | ||
method: HTTPMethod, | ||
queryItems: [URLQueryItem]? = nil, | ||
headers: [String: String]? = nil, | ||
body: Data? = nil) { | ||
self.transport = .http | ||
self.name = name | ||
self.baseURL = baseURL | ||
self.path = path | ||
self.httpMethod = method | ||
self.queryItems = queryItems | ||
self.headers = headers | ||
self.httpBody = body | ||
|
||
// Unused for HTTP | ||
self.serviceName = nil | ||
self.methodName = nil | ||
self.grpcRequestData = nil | ||
} | ||
|
||
// gRPC initializer | ||
init(grpc name: String, | ||
serviceName: String, | ||
methodName: String, | ||
requestData: Data) { | ||
self.transport = .grpc | ||
self.name = name | ||
self.serviceName = serviceName | ||
self.methodName = methodName | ||
self.grpcRequestData = requestData | ||
|
||
// Unused for gRPC | ||
self.baseURL = nil | ||
self.path = nil | ||
self.httpMethod = nil | ||
self.queryItems = nil | ||
self.headers = nil | ||
self.httpBody = nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// | ||
// HTTPService.swift | ||
// iosApp | ||
// | ||
// Created by Sarvesh Sharma on 11/12/24. | ||
// Copyright © 2024 orgName. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
class HTTPService: NetworkService { | ||
func performRequest(for endPoint: Endpoint) async throws -> Data { | ||
guard endPoint.transport == .http, | ||
let baseURL = endPoint.baseURL, | ||
let path = endPoint.path, | ||
let method = endPoint.httpMethod else { | ||
throw NetworkError.invalidRequest | ||
} | ||
var urlComponents = URLComponents(url: baseURL.appendingPathComponent(path), resolvingAgainstBaseURL: false) | ||
urlComponents?.queryItems = endPoint.queryItems | ||
guard let finalURL = urlComponents?.url else { | ||
throw NetworkError.invalidRequest | ||
} | ||
|
||
var request = URLRequest(url: finalURL) | ||
request.httpMethod = method.rawValue | ||
request.allHTTPHeaderFields = endPoint.headers | ||
request.httpBody = endPoint.httpBody | ||
let (data, response) = try await URLSession.shared.data(for: request) | ||
guard let httpResponse = response as? HTTPURLResponse else { | ||
throw NetworkError.invalidResponse("") | ||
} | ||
guard HTTPResponse.from(statusCode: httpResponse.statusCode) == .success else { | ||
throw NetworkError.invalidResponse(httpResponse.description) | ||
} | ||
return data | ||
} | ||
|
||
func performRequest<T>(for endPoint: Endpoint, decodeAs type: T.Type) async throws -> T where T: Decodable { | ||
let data = try await performRequest(for: endPoint) | ||
do { | ||
let decoded = try JSONDecoder().decode(type, from: data) | ||
return decoded | ||
} catch { | ||
throw NetworkError.decodingFailed(error) | ||
} | ||
} | ||
} | ||
|
||
enum HTTPResponse { | ||
case success | ||
case clientError | ||
case serverError | ||
case informational | ||
case redirection | ||
case unknown | ||
|
||
static func from(statusCode: Int) -> HTTPResponse { | ||
switch statusCode { | ||
case 200...299: | ||
return .success | ||
case 400...499: | ||
return .clientError | ||
case 500...599: | ||
return .serverError | ||
case 100...199: | ||
return .informational | ||
case 300...399: | ||
return .redirection | ||
default: | ||
return .unknown | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// NetworkService.swift | ||
// iosApp | ||
// | ||
// Created by Sarvesh Sharma on 11/12/24. | ||
// Copyright © 2024 orgName. All rights reserved. | ||
// | ||
import Foundation | ||
|
||
protocol NetworkService { | ||
func performRequest(for endPoint: Endpoint) async throws -> Data | ||
func performRequest<T: Decodable>(for endPoint: Endpoint, decodeAs type: T.Type) async throws -> T | ||
} | ||
|
||
enum NetworkError: Error { | ||
case invalidRequest | ||
case invalidResponse(String) | ||
case decodingFailed(Error) | ||
case transportError(String) | ||
} |