Skip to content

Enhancements: HttpRequestHandler, Project Settings Update, and Code Comments #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
23 changes: 19 additions & 4 deletions HttpUtility.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
archiveVersion = 1;
classes = {
};
objectVersion = 50;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
8280CC552B39647B0014F1D6 /* HttpRequestHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8280CC542B39647B0014F1D6 /* HttpRequestHandler.swift */; };
86010D0725CE240300A4E362 /* batman.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 86010D0625CE240300A4E362 /* batman.jpg */; };
86521B5625C6FD7200E05422 /* HURequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86521B5525C6FD7100E05422 /* HURequest.swift */; };
8656BC582483E3C60023549D /* EncodableExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8656BC572483E3C60023549D /* EncodableExtension.swift */; };
Expand All @@ -32,6 +33,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
8280CC542B39647B0014F1D6 /* HttpRequestHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HttpRequestHandler.swift; sourceTree = "<group>"; };
86010D0625CE240300A4E362 /* batman.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = batman.jpg; sourceTree = "<group>"; };
86521B5525C6FD7100E05422 /* HURequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HURequest.swift; sourceTree = "<group>"; };
8656BC572483E3C60023549D /* EncodableExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EncodableExtension.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -129,6 +131,7 @@
86719E9924720BD1002A2AB0 /* HttpUtility.h */,
86719E9A24720BD1002A2AB0 /* Info.plist */,
86719EB024720E40002A2AB0 /* HttpUtility.swift */,
8280CC542B39647B0014F1D6 /* HttpRequestHandler.swift */,
);
path = HttpUtility;
sourceTree = "<group>";
Expand Down Expand Up @@ -201,8 +204,9 @@
86719E8D24720BD1002A2AB0 /* Project object */ = {
isa = PBXProject;
attributes = {
BuildIndependentTargetsInParallel = YES;
LastSwiftUpdateCheck = 1140;
LastUpgradeCheck = 1220;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = CodeCat15;
TargetAttributes = {
86719E9524720BD1002A2AB0 = {
Expand Down Expand Up @@ -258,6 +262,7 @@
files = (
86CAEFE625BBBE98006A7791 /* HUNetworkError.swift in Sources */,
86719EB124720E40002A2AB0 /* HttpUtility.swift in Sources */,
8280CC552B39647B0014F1D6 /* HttpRequestHandler.swift in Sources */,
8656BC582483E3C60023549D /* EncodableExtension.swift in Sources */,
86521B5625C6FD7200E05422 /* HURequest.swift in Sources */,
86CAEFEA25BBBEDE006A7791 /* HUHttpMethods.swift in Sources */,
Expand Down Expand Up @@ -324,6 +329,7 @@
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
Expand All @@ -338,7 +344,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -388,6 +394,7 @@
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
Expand All @@ -396,7 +403,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = iphoneos;
Expand All @@ -412,18 +419,22 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "";
CODE_SIGN_STYLE = Automatic;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
ENABLE_MODULE_VERIFIER = YES;
INFOPLIST_FILE = HttpUtility/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MODULE_VERIFIER_SUPPORTED_LANGUAGES = "objective-c objective-c++";
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++14";
PRODUCT_BUNDLE_IDENTIFIER = com.codecat15.HttpUtility;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
Expand All @@ -438,18 +449,22 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "";
CODE_SIGN_STYLE = Automatic;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
ENABLE_MODULE_VERIFIER = YES;
INFOPLIST_FILE = HttpUtility/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MODULE_VERIFIER_SUPPORTED_LANGUAGES = "objective-c objective-c++";
MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu11 gnu++14";
PRODUCT_BUNDLE_IDENTIFIER = com.codecat15.HttpUtility;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1220"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
26 changes: 19 additions & 7 deletions HttpUtility/Extensions/EncodableExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ import Foundation

extension Encodable
{
/// Converts the encodable object to a URL with query string parameters.
///
/// - Parameters:
/// - urlString: The base URL string to which query string parameters will be appended.
/// - Returns: A URL with appended query string parameters, or `nil` if conversion fails.
///
/// This method converts the encodable object to a dictionary and appends the dictionary's key-value pairs as query string parameters to the provided URL string.
func convertToQueryStringUrl(urlString: String) -> URL?
{
var components = URLComponents(string: urlString)
if(components != nil)
{
let requestDictionary = convertToDictionary()

if(requestDictionary != nil)
{
var queryItems: [URLQueryItem] = []
Expand All @@ -28,29 +35,34 @@ extension Encodable
}
}
})

components?.queryItems = queryItems
return components?.url!
}
}

debugPrint("convertToQueryStringUrl => Error => Conversion failed, please make sure to pass a valid urlString and try again")

return nil
}

func convertToDictionary() -> [String: Any?]?
/// Converts the encodable object to a dictionary.
///
/// - Returns: A dictionary representation of the encodable object, or `nil` if conversion fails.
///
/// This method uses JSON encoding to convert the encodable object to a JSON data representation and then deserializes it into a dictionary.
func convertToDictionary() -> [String: Any?]?
{
do {
let encoder = try JSONEncoder().encode(self)
let result = (try? JSONSerialization.jsonObject(with: encoder, options: .allowFragments)).flatMap{$0 as? [String: Any?]}

return result

} catch let error {
debugPrint(error)
}

return nil
}
}
19 changes: 17 additions & 2 deletions HttpUtility/HUHttpMethods.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,25 @@

import Foundation

public enum HUHttpMethods : String
{
// MARK: - HUHttpMethods Enum

/// `HUHttpMethods` is an enumeration representing various HTTP methods.
///
/// For more information on HTTP methods, refer to [MDN Web Docs - HTTP Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods).
public enum HUHttpMethods: String {

// MARK: Cases

/// HTTP GET method.
case get = "GET"

/// HTTP POST method.
case post = "POST"

/// HTTP PUT method.
case put = "PUT"

/// HTTP DELETE method.
case delete = "DELETE"
}

40 changes: 35 additions & 5 deletions HttpUtility/HUNetworkError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,47 @@

import Foundation

public struct HUNetworkError : Error
{
// MARK: - HUNetworkError Struct

/// `HUNetworkError` is a struct representing an error that can occur during a network request.
public struct HUNetworkError: Error {

// MARK: Properties

/// A human-readable reason for the error.
let reason: String?

/// The HTTP status code associated with the error.
let httpStatusCode: Int?

/// The URL for which the network error occurred.
let requestUrl: URL?

/// The body of the HTTP request that resulted in the network error.
let requestBody: String?

/// The server response data in string format, if available.
let serverResponse: String?

init(withServerResponse response: Data? = nil, forRequestUrl url: URL, withHttpBody body: Data? = nil, errorMessage message: String, forStatusCode statusCode: Int)
{

// MARK: Initialization

/// Initializes an instance of `HUNetworkError` with the provided parameters.
/// - Parameters:
/// - response: The server response data in raw Data format.
/// - url: The URL for which the network error occurred.
/// - body: The body of the HTTP request that resulted in the network error.
/// - message: A human-readable error message.
/// - statusCode: The HTTP status code associated with the error.
init(withServerResponse response: Data? = nil,
forRequestUrl url: URL,
withHttpBody body: Data? = nil,
errorMessage message: String,
forStatusCode statusCode: Int) {

// Convert server response data to a string if available
self.serverResponse = response != nil ? String(data: response!, encoding: .utf8) : nil

// Set other properties with provided values
self.requestUrl = url
self.requestBody = body != nil ? String(data: body!, encoding: .utf8) : nil
self.httpStatusCode = statusCode
Expand Down
53 changes: 48 additions & 5 deletions HttpUtility/HURequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,75 @@

import Foundation

// MARK: - Request Protocol

/// `Request` protocol defines the basic structure for an HTTP request.
protocol Request {

/// The URL for the request.
var url: URL { get set }

/// The HTTP method for the request.
var method: HUHttpMethods { get set }
}

public struct HURequest : Request {
// MARK: - HURequest Struct

/// `HURequest` is a struct representing a basic HTTP request conforming to the `Request` protocol.
public struct HURequest: Request {

// MARK: Properties

/// The URL for the request.
var url: URL

/// The HTTP method for the request.
var method: HUHttpMethods

/// The body of the HTTP request.
var requestBody: Data? = nil


// MARK: Initialization

/// Initializes an instance of `HURequest` with the provided parameters.
/// - Parameters:
/// - url: The URL for the request.
/// - method: The HTTP method for the request.
/// - requestBody: The body of the HTTP request.
public init(withUrl url: URL, forHttpMethod method: HUHttpMethods, requestBody: Data? = nil) {
self.url = url
self.method = method
self.requestBody = requestBody != nil ? requestBody : nil
}
}

public struct HUMultiPartRequest : Request {
// MARK: - HUMultiPartRequest Struct

/// `HUMultiPartRequest` is a struct representing an HTTP request with multipart form data conforming to the `Request` protocol.
public struct HUMultiPartRequest: Request {

// MARK: Properties

/// The URL for the request.
var url: URL

/// The HTTP method for the request.
var method: HUHttpMethods
var request : Encodable


/// The body of the HTTP request, represented by an `Encodable` type.
var request: Encodable

// MARK: Initialization

/// Initializes an instance of `HUMultiPartRequest` with the provided parameters.
/// - Parameters:
/// - url: The URL for the request.
/// - method: The HTTP method for the request.
/// - requestBody: The body of the HTTP request, represented by an `Encodable` type.
public init(withUrl url: URL, forHttpMethod method: HUHttpMethods, requestBody: Encodable) {
self.url = url
self.method = method
self.request = requestBody
}
}

Loading