├── .gitignore ├── .travis.yml ├── HttpUtility.podspec ├── HttpUtility.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── xcshareddata │ └── xcschemes │ └── HttpUtility.xcscheme ├── HttpUtility ├── Extensions │ └── EncodableExtension.swift ├── HUHttpMethods.swift ├── HUNetworkError.swift ├── HURequest.swift ├── HttpUtility.h ├── HttpUtility.swift └── Info.plist ├── HttpUtilityTests ├── ExtensionsTests │ └── EncodableExtensionUnitTest.swift ├── Info.plist ├── IntegrationTests │ └── HttpUtilityIntegrationTests.swift ├── TestModel │ ├── Requests.swift │ └── Response.swift └── batman.jpg ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## User settings 6 | xcuserdata/ 7 | 8 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) 9 | *.xcscmblueprint 10 | *.xccheckout 11 | 12 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) 13 | build/ 14 | DerivedData/ 15 | *.moved-aside 16 | *.pbxuser 17 | !default.pbxuser 18 | *.mode1v3 19 | !default.mode1v3 20 | *.mode2v3 21 | !default.mode2v3 22 | *.perspectivev3 23 | !default.perspectivev3 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | 28 | ## App packaging 29 | *.ipa 30 | *.dSYM.zip 31 | *.dSYM 32 | 33 | ## Playgrounds 34 | timeline.xctimeline 35 | playground.xcworkspace 36 | 37 | # Swift Package Manager 38 | # 39 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 40 | # Packages/ 41 | # Package.pins 42 | # Package.resolved 43 | # *.xcodeproj 44 | # 45 | # Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata 46 | # hence it is not needed unless you have added a package configuration file to your project 47 | # .swiftpm 48 | 49 | .build/ 50 | 51 | # CocoaPods 52 | # 53 | # We recommend against adding the Pods directory to your .gitignore. However 54 | # you should judge for yourself, the pros and cons are mentioned at: 55 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 56 | # 57 | # Pods/ 58 | # 59 | # Add this line if you want to avoid checking in source code from the Xcode workspace 60 | # *.xcworkspace 61 | 62 | # Carthage 63 | # 64 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 65 | # Carthage/Checkouts 66 | 67 | Carthage/Build/ 68 | 69 | # Accio dependency management 70 | Dependencies/ 71 | .accio/ 72 | 73 | # fastlane 74 | # 75 | # It is recommended to not store the screenshots in the git repo. 76 | # Instead, use fastlane to re-generate the screenshots whenever they are needed. 77 | # For more information about the recommended setup visit: 78 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 79 | 80 | fastlane/report.xml 81 | fastlane/Preview.html 82 | fastlane/screenshots/**/*.png 83 | fastlane/test_output 84 | 85 | # Code Injection 86 | # 87 | # After new code Injection tools there's a generated folder /iOSInjectionProject 88 | # https://github.com/johnno1962/injectionforxcode 89 | 90 | iOSInjectionProject/ 91 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | os: osx 2 | osx_image: xcode10.2 3 | language: swift 4 | 5 | xcode_project: HttpUtility.xcodeproj 6 | xcode_scheme: HttpUtility 7 | xcode_sdk: iphonesimulator12.2 8 | xcode_destination: platform=iOS Simulator,OS=12.2,name=iPhone X 9 | branches: 10 | only: 11 | - master 12 | -------------------------------------------------------------------------------- /HttpUtility.podspec: -------------------------------------------------------------------------------- 1 | # 2 | # Be sure to run `pod spec lint HttpUtility.podspec' to ensure this is a 3 | # valid spec and to remove all comments including this before submitting the spec. 4 | # 5 | # To learn more about Podspec attributes see https://guides.cocoapods.org/syntax/podspec.html 6 | # To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/ 7 | # 8 | 9 | Pod::Spec.new do |spec| 10 | 11 | # ――― Spec Metadata ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 12 | # 13 | # These will help people to find your library, and whilst it 14 | # can feel like a chore to fill in it's definitely to your advantage. The 15 | # summary should be tweet-length, and the description more in depth. 16 | # 17 | 18 | spec.name = "HttpUtility" 19 | spec.version = "1.2.4" 20 | spec.summary = "HttpUtility is helpful in making HTTP requests in iOS application" 21 | 22 | # This description is used to generate tags and improve search results. 23 | # * Think: What does it do? Why did you write it? What is the focus? 24 | # * Try to keep it short, snappy and to the point. 25 | # * Write the description between the DESC delimiters below. 26 | # * Finally, don't worry about the indent, CocoaPods strips it! 27 | spec.description = <<-DESC 28 | HttpUtility is an open source MIT license project which is helpful in making HTTP requests and parsing the JSON response received from server 29 | DESC 30 | 31 | spec.homepage = "https://github.com/codecat15/HttpUtility" 32 | # spec.screenshots = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif" 33 | 34 | 35 | # ――― Spec License ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 36 | # 37 | # Licensing your code is important. See https://choosealicense.com for more info. 38 | # CocoaPods will detect a license file if there is a named LICENSE* 39 | # Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'. 40 | # 41 | spec.license = { :type => "MIT", :file => "LICENSE" } 42 | 43 | 44 | # ――― Author Metadata ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 45 | # 46 | # Specify the authors of the library, with email addresses. Email addresses 47 | # of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also 48 | # accepts just a name if you'd rather not provide an email address. 49 | # 50 | # Specify a social_media_url where others can refer to, for example a twitter 51 | # profile URL. 52 | # 53 | 54 | spec.author = { "codecat15" => "codecat15@gmail.com" } 55 | # Or just: spec.author = "codecat15" 56 | # spec.authors = { "codecat15" => "codecat15@gmail.com" } 57 | # spec.social_media_url = "https://twitter.com/codecat15" 58 | 59 | # ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 60 | # 61 | # If this Pod runs only on iOS or OS X, then specify the platform and 62 | # the deployment target. You can optionally include the target after the platform. 63 | # 64 | 65 | spec.platform = :ios, "11.0" 66 | spec.swift_version = "5.0" 67 | # spec.platform = :ios, "5.0" 68 | 69 | # When using multiple platforms 70 | spec.ios.deployment_target = "11.0" 71 | # spec.osx.deployment_target = "10.7" 72 | # spec.watchos.deployment_target = "2.0" 73 | # spec.tvos.deployment_target = "9.0" 74 | 75 | 76 | # ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 77 | # 78 | # Specify the location from where the source should be retrieved. 79 | # Supports git, hg, bzr, svn and HTTP. 80 | # 81 | 82 | spec.source = { :git => "https://github.com/codecat15/HttpUtility.git", :tag => "#{spec.version}" } 83 | 84 | 85 | # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 86 | # 87 | # CocoaPods is smart about how it includes source code. For source files 88 | # giving a folder will include any swift, h, m, mm, c & cpp files. 89 | # For header files it will include any header in the folder. 90 | # Not including the public_header_files will make all headers public. 91 | # 92 | 93 | spec.source_files = "HttpUtility/**/*.{h,m,swift}" 94 | #spec.exclude_files = "Classes/Exclude" 95 | 96 | # spec.public_header_files = "Classes/**/*.h" 97 | 98 | 99 | # ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 100 | # 101 | # A list of resources included with the Pod. These are copied into the 102 | # target bundle with a build phase script. Anything else will be cleaned. 103 | # You can preserve files from being cleaned, please don't preserve 104 | # non-essential files like tests, examples and documentation. 105 | # 106 | 107 | # spec.resource = "icon.png" 108 | # spec.resources = "Resources/*.png" 109 | 110 | # spec.preserve_paths = "FilesToSave", "MoreFilesToSave" 111 | 112 | 113 | # ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 114 | # 115 | # Link your library with frameworks, or libraries. Libraries do not include 116 | # the lib prefix of their name. 117 | # 118 | 119 | # spec.framework = "SomeFramework" 120 | # spec.frameworks = "SomeFramework", "AnotherFramework" 121 | 122 | # spec.library = "iconv" 123 | # spec.libraries = "iconv", "xml2" 124 | 125 | 126 | # ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― # 127 | # 128 | # If your library depends on compiler flags you can set them in the xcconfig hash 129 | # where they will only apply to your library. If you depend on other Podspecs 130 | # you can include multiple dependencies to ensure it works. 131 | 132 | # spec.requires_arc = true 133 | 134 | # spec.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" } 135 | # spec.dependency "JSONKit", "~> 1.4" 136 | 137 | end 138 | -------------------------------------------------------------------------------- /HttpUtility.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 86010D0725CE240300A4E362 /* batman.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 86010D0625CE240300A4E362 /* batman.jpg */; }; 11 | 86521B5625C6FD7200E05422 /* HURequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86521B5525C6FD7100E05422 /* HURequest.swift */; }; 12 | 8656BC582483E3C60023549D /* EncodableExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8656BC572483E3C60023549D /* EncodableExtension.swift */; }; 13 | 8656BC5B2483E43D0023549D /* EncodableExtensionUnitTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8656BC5A2483E43D0023549D /* EncodableExtensionUnitTest.swift */; }; 14 | 8656BC5E2484313F0023549D /* HttpUtilityIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8656BC5D2484313F0023549D /* HttpUtilityIntegrationTests.swift */; }; 15 | 8656BC61248495700023549D /* Response.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8656BC60248495700023549D /* Response.swift */; }; 16 | 86719EA024720BD1002A2AB0 /* HttpUtility.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 86719E9624720BD1002A2AB0 /* HttpUtility.framework */; }; 17 | 86719EA724720BD1002A2AB0 /* HttpUtility.h in Headers */ = {isa = PBXBuildFile; fileRef = 86719E9924720BD1002A2AB0 /* HttpUtility.h */; settings = {ATTRIBUTES = (Public, ); }; }; 18 | 86719EB124720E40002A2AB0 /* HttpUtility.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86719EB024720E40002A2AB0 /* HttpUtility.swift */; }; 19 | 86CAEFE625BBBE98006A7791 /* HUNetworkError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86CAEFE525BBBE98006A7791 /* HUNetworkError.swift */; }; 20 | 86CAEFEA25BBBEDE006A7791 /* HUHttpMethods.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86CAEFE925BBBEDE006A7791 /* HUHttpMethods.swift */; }; 21 | 86E9B56424883E9100B78521 /* Requests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86E9B56324883E9100B78521 /* Requests.swift */; }; 22 | /* End PBXBuildFile section */ 23 | 24 | /* Begin PBXContainerItemProxy section */ 25 | 86719EA124720BD1002A2AB0 /* PBXContainerItemProxy */ = { 26 | isa = PBXContainerItemProxy; 27 | containerPortal = 86719E8D24720BD1002A2AB0 /* Project object */; 28 | proxyType = 1; 29 | remoteGlobalIDString = 86719E9524720BD1002A2AB0; 30 | remoteInfo = HttpUtility; 31 | }; 32 | /* End PBXContainerItemProxy section */ 33 | 34 | /* Begin PBXFileReference section */ 35 | 86010D0625CE240300A4E362 /* batman.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = batman.jpg; sourceTree = ""; }; 36 | 86521B5525C6FD7100E05422 /* HURequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HURequest.swift; sourceTree = ""; }; 37 | 8656BC572483E3C60023549D /* EncodableExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EncodableExtension.swift; sourceTree = ""; }; 38 | 8656BC5A2483E43D0023549D /* EncodableExtensionUnitTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EncodableExtensionUnitTest.swift; sourceTree = ""; }; 39 | 8656BC5D2484313F0023549D /* HttpUtilityIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HttpUtilityIntegrationTests.swift; sourceTree = ""; }; 40 | 8656BC60248495700023549D /* Response.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Response.swift; sourceTree = ""; }; 41 | 86719E9624720BD1002A2AB0 /* HttpUtility.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = HttpUtility.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 42 | 86719E9924720BD1002A2AB0 /* HttpUtility.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HttpUtility.h; sourceTree = ""; }; 43 | 86719E9A24720BD1002A2AB0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 44 | 86719E9F24720BD1002A2AB0 /* HttpUtilityTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = HttpUtilityTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 45 | 86719EA624720BD1002A2AB0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 46 | 86719EB024720E40002A2AB0 /* HttpUtility.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HttpUtility.swift; sourceTree = ""; }; 47 | 86CAEFE525BBBE98006A7791 /* HUNetworkError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HUNetworkError.swift; sourceTree = ""; }; 48 | 86CAEFE925BBBEDE006A7791 /* HUHttpMethods.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HUHttpMethods.swift; sourceTree = ""; }; 49 | 86E9B56324883E9100B78521 /* Requests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Requests.swift; sourceTree = ""; }; 50 | /* End PBXFileReference section */ 51 | 52 | /* Begin PBXFrameworksBuildPhase section */ 53 | 86719E9324720BD1002A2AB0 /* Frameworks */ = { 54 | isa = PBXFrameworksBuildPhase; 55 | buildActionMask = 2147483647; 56 | files = ( 57 | ); 58 | runOnlyForDeploymentPostprocessing = 0; 59 | }; 60 | 86719E9C24720BD1002A2AB0 /* Frameworks */ = { 61 | isa = PBXFrameworksBuildPhase; 62 | buildActionMask = 2147483647; 63 | files = ( 64 | 86719EA024720BD1002A2AB0 /* HttpUtility.framework in Frameworks */, 65 | ); 66 | runOnlyForDeploymentPostprocessing = 0; 67 | }; 68 | /* End PBXFrameworksBuildPhase section */ 69 | 70 | /* Begin PBXGroup section */ 71 | 8656BC562483E3B20023549D /* Extensions */ = { 72 | isa = PBXGroup; 73 | children = ( 74 | 8656BC572483E3C60023549D /* EncodableExtension.swift */, 75 | ); 76 | path = Extensions; 77 | sourceTree = ""; 78 | }; 79 | 8656BC592483E4200023549D /* ExtensionsTests */ = { 80 | isa = PBXGroup; 81 | children = ( 82 | 8656BC5A2483E43D0023549D /* EncodableExtensionUnitTest.swift */, 83 | ); 84 | path = ExtensionsTests; 85 | sourceTree = ""; 86 | }; 87 | 8656BC5C248431200023549D /* IntegrationTests */ = { 88 | isa = PBXGroup; 89 | children = ( 90 | 8656BC5D2484313F0023549D /* HttpUtilityIntegrationTests.swift */, 91 | ); 92 | path = IntegrationTests; 93 | sourceTree = ""; 94 | }; 95 | 8656BC5F248495620023549D /* TestModel */ = { 96 | isa = PBXGroup; 97 | children = ( 98 | 8656BC60248495700023549D /* Response.swift */, 99 | 86E9B56324883E9100B78521 /* Requests.swift */, 100 | ); 101 | path = TestModel; 102 | sourceTree = ""; 103 | }; 104 | 86719E8C24720BD1002A2AB0 = { 105 | isa = PBXGroup; 106 | children = ( 107 | 86719E9824720BD1002A2AB0 /* HttpUtility */, 108 | 86719EA324720BD1002A2AB0 /* HttpUtilityTests */, 109 | 86719E9724720BD1002A2AB0 /* Products */, 110 | ); 111 | sourceTree = ""; 112 | }; 113 | 86719E9724720BD1002A2AB0 /* Products */ = { 114 | isa = PBXGroup; 115 | children = ( 116 | 86719E9624720BD1002A2AB0 /* HttpUtility.framework */, 117 | 86719E9F24720BD1002A2AB0 /* HttpUtilityTests.xctest */, 118 | ); 119 | name = Products; 120 | sourceTree = ""; 121 | }; 122 | 86719E9824720BD1002A2AB0 /* HttpUtility */ = { 123 | isa = PBXGroup; 124 | children = ( 125 | 86CAEFE525BBBE98006A7791 /* HUNetworkError.swift */, 126 | 86CAEFE925BBBEDE006A7791 /* HUHttpMethods.swift */, 127 | 86521B5525C6FD7100E05422 /* HURequest.swift */, 128 | 8656BC562483E3B20023549D /* Extensions */, 129 | 86719E9924720BD1002A2AB0 /* HttpUtility.h */, 130 | 86719E9A24720BD1002A2AB0 /* Info.plist */, 131 | 86719EB024720E40002A2AB0 /* HttpUtility.swift */, 132 | ); 133 | path = HttpUtility; 134 | sourceTree = ""; 135 | }; 136 | 86719EA324720BD1002A2AB0 /* HttpUtilityTests */ = { 137 | isa = PBXGroup; 138 | children = ( 139 | 86010D0625CE240300A4E362 /* batman.jpg */, 140 | 8656BC5F248495620023549D /* TestModel */, 141 | 8656BC5C248431200023549D /* IntegrationTests */, 142 | 8656BC592483E4200023549D /* ExtensionsTests */, 143 | 86719EA624720BD1002A2AB0 /* Info.plist */, 144 | ); 145 | path = HttpUtilityTests; 146 | sourceTree = ""; 147 | }; 148 | /* End PBXGroup section */ 149 | 150 | /* Begin PBXHeadersBuildPhase section */ 151 | 86719E9124720BD1002A2AB0 /* Headers */ = { 152 | isa = PBXHeadersBuildPhase; 153 | buildActionMask = 2147483647; 154 | files = ( 155 | 86719EA724720BD1002A2AB0 /* HttpUtility.h in Headers */, 156 | ); 157 | runOnlyForDeploymentPostprocessing = 0; 158 | }; 159 | /* End PBXHeadersBuildPhase section */ 160 | 161 | /* Begin PBXNativeTarget section */ 162 | 86719E9524720BD1002A2AB0 /* HttpUtility */ = { 163 | isa = PBXNativeTarget; 164 | buildConfigurationList = 86719EAA24720BD1002A2AB0 /* Build configuration list for PBXNativeTarget "HttpUtility" */; 165 | buildPhases = ( 166 | 86719E9124720BD1002A2AB0 /* Headers */, 167 | 86719E9224720BD1002A2AB0 /* Sources */, 168 | 86719E9324720BD1002A2AB0 /* Frameworks */, 169 | 86719E9424720BD1002A2AB0 /* Resources */, 170 | ); 171 | buildRules = ( 172 | ); 173 | dependencies = ( 174 | ); 175 | name = HttpUtility; 176 | productName = HttpUtility; 177 | productReference = 86719E9624720BD1002A2AB0 /* HttpUtility.framework */; 178 | productType = "com.apple.product-type.framework"; 179 | }; 180 | 86719E9E24720BD1002A2AB0 /* HttpUtilityTests */ = { 181 | isa = PBXNativeTarget; 182 | buildConfigurationList = 86719EAD24720BD1002A2AB0 /* Build configuration list for PBXNativeTarget "HttpUtilityTests" */; 183 | buildPhases = ( 184 | 86719E9B24720BD1002A2AB0 /* Sources */, 185 | 86719E9C24720BD1002A2AB0 /* Frameworks */, 186 | 86719E9D24720BD1002A2AB0 /* Resources */, 187 | ); 188 | buildRules = ( 189 | ); 190 | dependencies = ( 191 | 86719EA224720BD1002A2AB0 /* PBXTargetDependency */, 192 | ); 193 | name = HttpUtilityTests; 194 | productName = HttpUtilityTests; 195 | productReference = 86719E9F24720BD1002A2AB0 /* HttpUtilityTests.xctest */; 196 | productType = "com.apple.product-type.bundle.unit-test"; 197 | }; 198 | /* End PBXNativeTarget section */ 199 | 200 | /* Begin PBXProject section */ 201 | 86719E8D24720BD1002A2AB0 /* Project object */ = { 202 | isa = PBXProject; 203 | attributes = { 204 | LastSwiftUpdateCheck = 1140; 205 | LastUpgradeCheck = 1220; 206 | ORGANIZATIONNAME = CodeCat15; 207 | TargetAttributes = { 208 | 86719E9524720BD1002A2AB0 = { 209 | CreatedOnToolsVersion = 11.4.1; 210 | LastSwiftMigration = 1140; 211 | }; 212 | 86719E9E24720BD1002A2AB0 = { 213 | CreatedOnToolsVersion = 11.4.1; 214 | }; 215 | }; 216 | }; 217 | buildConfigurationList = 86719E9024720BD1002A2AB0 /* Build configuration list for PBXProject "HttpUtility" */; 218 | compatibilityVersion = "Xcode 9.3"; 219 | developmentRegion = en; 220 | hasScannedForEncodings = 0; 221 | knownRegions = ( 222 | en, 223 | Base, 224 | ); 225 | mainGroup = 86719E8C24720BD1002A2AB0; 226 | productRefGroup = 86719E9724720BD1002A2AB0 /* Products */; 227 | projectDirPath = ""; 228 | projectRoot = ""; 229 | targets = ( 230 | 86719E9524720BD1002A2AB0 /* HttpUtility */, 231 | 86719E9E24720BD1002A2AB0 /* HttpUtilityTests */, 232 | ); 233 | }; 234 | /* End PBXProject section */ 235 | 236 | /* Begin PBXResourcesBuildPhase section */ 237 | 86719E9424720BD1002A2AB0 /* Resources */ = { 238 | isa = PBXResourcesBuildPhase; 239 | buildActionMask = 2147483647; 240 | files = ( 241 | ); 242 | runOnlyForDeploymentPostprocessing = 0; 243 | }; 244 | 86719E9D24720BD1002A2AB0 /* Resources */ = { 245 | isa = PBXResourcesBuildPhase; 246 | buildActionMask = 2147483647; 247 | files = ( 248 | 86010D0725CE240300A4E362 /* batman.jpg in Resources */, 249 | ); 250 | runOnlyForDeploymentPostprocessing = 0; 251 | }; 252 | /* End PBXResourcesBuildPhase section */ 253 | 254 | /* Begin PBXSourcesBuildPhase section */ 255 | 86719E9224720BD1002A2AB0 /* Sources */ = { 256 | isa = PBXSourcesBuildPhase; 257 | buildActionMask = 2147483647; 258 | files = ( 259 | 86CAEFE625BBBE98006A7791 /* HUNetworkError.swift in Sources */, 260 | 86719EB124720E40002A2AB0 /* HttpUtility.swift in Sources */, 261 | 8656BC582483E3C60023549D /* EncodableExtension.swift in Sources */, 262 | 86521B5625C6FD7200E05422 /* HURequest.swift in Sources */, 263 | 86CAEFEA25BBBEDE006A7791 /* HUHttpMethods.swift in Sources */, 264 | ); 265 | runOnlyForDeploymentPostprocessing = 0; 266 | }; 267 | 86719E9B24720BD1002A2AB0 /* Sources */ = { 268 | isa = PBXSourcesBuildPhase; 269 | buildActionMask = 2147483647; 270 | files = ( 271 | 8656BC61248495700023549D /* Response.swift in Sources */, 272 | 86E9B56424883E9100B78521 /* Requests.swift in Sources */, 273 | 8656BC5B2483E43D0023549D /* EncodableExtensionUnitTest.swift in Sources */, 274 | 8656BC5E2484313F0023549D /* HttpUtilityIntegrationTests.swift in Sources */, 275 | ); 276 | runOnlyForDeploymentPostprocessing = 0; 277 | }; 278 | /* End PBXSourcesBuildPhase section */ 279 | 280 | /* Begin PBXTargetDependency section */ 281 | 86719EA224720BD1002A2AB0 /* PBXTargetDependency */ = { 282 | isa = PBXTargetDependency; 283 | target = 86719E9524720BD1002A2AB0 /* HttpUtility */; 284 | targetProxy = 86719EA124720BD1002A2AB0 /* PBXContainerItemProxy */; 285 | }; 286 | /* End PBXTargetDependency section */ 287 | 288 | /* Begin XCBuildConfiguration section */ 289 | 86719EA824720BD1002A2AB0 /* Debug */ = { 290 | isa = XCBuildConfiguration; 291 | buildSettings = { 292 | ALWAYS_SEARCH_USER_PATHS = NO; 293 | CLANG_ANALYZER_NONNULL = YES; 294 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 295 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 296 | CLANG_CXX_LIBRARY = "libc++"; 297 | CLANG_ENABLE_MODULES = YES; 298 | CLANG_ENABLE_OBJC_ARC = YES; 299 | CLANG_ENABLE_OBJC_WEAK = YES; 300 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 301 | CLANG_WARN_BOOL_CONVERSION = YES; 302 | CLANG_WARN_COMMA = YES; 303 | CLANG_WARN_CONSTANT_CONVERSION = YES; 304 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 305 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 306 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 307 | CLANG_WARN_EMPTY_BODY = YES; 308 | CLANG_WARN_ENUM_CONVERSION = YES; 309 | CLANG_WARN_INFINITE_RECURSION = YES; 310 | CLANG_WARN_INT_CONVERSION = YES; 311 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 312 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 313 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 314 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 315 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 316 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 317 | CLANG_WARN_STRICT_PROTOTYPES = YES; 318 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 319 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 320 | CLANG_WARN_UNREACHABLE_CODE = YES; 321 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 322 | COPY_PHASE_STRIP = NO; 323 | CURRENT_PROJECT_VERSION = 1; 324 | DEBUG_INFORMATION_FORMAT = dwarf; 325 | ENABLE_STRICT_OBJC_MSGSEND = YES; 326 | ENABLE_TESTABILITY = YES; 327 | GCC_C_LANGUAGE_STANDARD = gnu11; 328 | GCC_DYNAMIC_NO_PIC = NO; 329 | GCC_NO_COMMON_BLOCKS = YES; 330 | GCC_OPTIMIZATION_LEVEL = 0; 331 | GCC_PREPROCESSOR_DEFINITIONS = ( 332 | "DEBUG=1", 333 | "$(inherited)", 334 | ); 335 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 336 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 337 | GCC_WARN_UNDECLARED_SELECTOR = YES; 338 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 339 | GCC_WARN_UNUSED_FUNCTION = YES; 340 | GCC_WARN_UNUSED_VARIABLE = YES; 341 | IPHONEOS_DEPLOYMENT_TARGET = 12.0; 342 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 343 | MTL_FAST_MATH = YES; 344 | ONLY_ACTIVE_ARCH = YES; 345 | SDKROOT = iphoneos; 346 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 347 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 348 | VERSIONING_SYSTEM = "apple-generic"; 349 | VERSION_INFO_PREFIX = ""; 350 | }; 351 | name = Debug; 352 | }; 353 | 86719EA924720BD1002A2AB0 /* Release */ = { 354 | isa = XCBuildConfiguration; 355 | buildSettings = { 356 | ALWAYS_SEARCH_USER_PATHS = NO; 357 | CLANG_ANALYZER_NONNULL = YES; 358 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 359 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 360 | CLANG_CXX_LIBRARY = "libc++"; 361 | CLANG_ENABLE_MODULES = YES; 362 | CLANG_ENABLE_OBJC_ARC = YES; 363 | CLANG_ENABLE_OBJC_WEAK = YES; 364 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 365 | CLANG_WARN_BOOL_CONVERSION = YES; 366 | CLANG_WARN_COMMA = YES; 367 | CLANG_WARN_CONSTANT_CONVERSION = YES; 368 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 369 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 370 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 371 | CLANG_WARN_EMPTY_BODY = YES; 372 | CLANG_WARN_ENUM_CONVERSION = YES; 373 | CLANG_WARN_INFINITE_RECURSION = YES; 374 | CLANG_WARN_INT_CONVERSION = YES; 375 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 376 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 377 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 378 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 379 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 380 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 381 | CLANG_WARN_STRICT_PROTOTYPES = YES; 382 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 383 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 384 | CLANG_WARN_UNREACHABLE_CODE = YES; 385 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 386 | COPY_PHASE_STRIP = NO; 387 | CURRENT_PROJECT_VERSION = 1; 388 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 389 | ENABLE_NS_ASSERTIONS = NO; 390 | ENABLE_STRICT_OBJC_MSGSEND = YES; 391 | GCC_C_LANGUAGE_STANDARD = gnu11; 392 | GCC_NO_COMMON_BLOCKS = YES; 393 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 394 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 395 | GCC_WARN_UNDECLARED_SELECTOR = YES; 396 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 397 | GCC_WARN_UNUSED_FUNCTION = YES; 398 | GCC_WARN_UNUSED_VARIABLE = YES; 399 | IPHONEOS_DEPLOYMENT_TARGET = 12.0; 400 | MTL_ENABLE_DEBUG_INFO = NO; 401 | MTL_FAST_MATH = YES; 402 | SDKROOT = iphoneos; 403 | SWIFT_COMPILATION_MODE = wholemodule; 404 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 405 | VALIDATE_PRODUCT = YES; 406 | VERSIONING_SYSTEM = "apple-generic"; 407 | VERSION_INFO_PREFIX = ""; 408 | }; 409 | name = Release; 410 | }; 411 | 86719EAB24720BD1002A2AB0 /* Debug */ = { 412 | isa = XCBuildConfiguration; 413 | buildSettings = { 414 | CLANG_ENABLE_MODULES = YES; 415 | CODE_SIGN_STYLE = Automatic; 416 | DEFINES_MODULE = YES; 417 | DYLIB_COMPATIBILITY_VERSION = 1; 418 | DYLIB_CURRENT_VERSION = 1; 419 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 420 | INFOPLIST_FILE = HttpUtility/Info.plist; 421 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 422 | LD_RUNPATH_SEARCH_PATHS = ( 423 | "$(inherited)", 424 | "@executable_path/Frameworks", 425 | "@loader_path/Frameworks", 426 | ); 427 | PRODUCT_BUNDLE_IDENTIFIER = com.codecat15.HttpUtility; 428 | PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; 429 | SKIP_INSTALL = YES; 430 | SUPPORTS_MACCATALYST = NO; 431 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 432 | SWIFT_VERSION = 5.0; 433 | TARGETED_DEVICE_FAMILY = "1,2"; 434 | }; 435 | name = Debug; 436 | }; 437 | 86719EAC24720BD1002A2AB0 /* Release */ = { 438 | isa = XCBuildConfiguration; 439 | buildSettings = { 440 | CLANG_ENABLE_MODULES = YES; 441 | CODE_SIGN_STYLE = Automatic; 442 | DEFINES_MODULE = YES; 443 | DYLIB_COMPATIBILITY_VERSION = 1; 444 | DYLIB_CURRENT_VERSION = 1; 445 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 446 | INFOPLIST_FILE = HttpUtility/Info.plist; 447 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 448 | LD_RUNPATH_SEARCH_PATHS = ( 449 | "$(inherited)", 450 | "@executable_path/Frameworks", 451 | "@loader_path/Frameworks", 452 | ); 453 | PRODUCT_BUNDLE_IDENTIFIER = com.codecat15.HttpUtility; 454 | PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; 455 | SKIP_INSTALL = YES; 456 | SUPPORTS_MACCATALYST = NO; 457 | SWIFT_VERSION = 5.0; 458 | TARGETED_DEVICE_FAMILY = "1,2"; 459 | }; 460 | name = Release; 461 | }; 462 | 86719EAE24720BD1002A2AB0 /* Debug */ = { 463 | isa = XCBuildConfiguration; 464 | buildSettings = { 465 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 466 | CODE_SIGN_STYLE = Automatic; 467 | INFOPLIST_FILE = HttpUtilityTests/Info.plist; 468 | LD_RUNPATH_SEARCH_PATHS = ( 469 | "$(inherited)", 470 | "@executable_path/Frameworks", 471 | "@loader_path/Frameworks", 472 | ); 473 | PRODUCT_BUNDLE_IDENTIFIER = com.codecat15.HttpUtilityTests; 474 | PRODUCT_NAME = "$(TARGET_NAME)"; 475 | SWIFT_VERSION = 5.0; 476 | TARGETED_DEVICE_FAMILY = "1,2"; 477 | }; 478 | name = Debug; 479 | }; 480 | 86719EAF24720BD1002A2AB0 /* Release */ = { 481 | isa = XCBuildConfiguration; 482 | buildSettings = { 483 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 484 | CODE_SIGN_STYLE = Automatic; 485 | INFOPLIST_FILE = HttpUtilityTests/Info.plist; 486 | LD_RUNPATH_SEARCH_PATHS = ( 487 | "$(inherited)", 488 | "@executable_path/Frameworks", 489 | "@loader_path/Frameworks", 490 | ); 491 | PRODUCT_BUNDLE_IDENTIFIER = com.codecat15.HttpUtilityTests; 492 | PRODUCT_NAME = "$(TARGET_NAME)"; 493 | SWIFT_VERSION = 5.0; 494 | TARGETED_DEVICE_FAMILY = "1,2"; 495 | }; 496 | name = Release; 497 | }; 498 | /* End XCBuildConfiguration section */ 499 | 500 | /* Begin XCConfigurationList section */ 501 | 86719E9024720BD1002A2AB0 /* Build configuration list for PBXProject "HttpUtility" */ = { 502 | isa = XCConfigurationList; 503 | buildConfigurations = ( 504 | 86719EA824720BD1002A2AB0 /* Debug */, 505 | 86719EA924720BD1002A2AB0 /* Release */, 506 | ); 507 | defaultConfigurationIsVisible = 0; 508 | defaultConfigurationName = Release; 509 | }; 510 | 86719EAA24720BD1002A2AB0 /* Build configuration list for PBXNativeTarget "HttpUtility" */ = { 511 | isa = XCConfigurationList; 512 | buildConfigurations = ( 513 | 86719EAB24720BD1002A2AB0 /* Debug */, 514 | 86719EAC24720BD1002A2AB0 /* Release */, 515 | ); 516 | defaultConfigurationIsVisible = 0; 517 | defaultConfigurationName = Release; 518 | }; 519 | 86719EAD24720BD1002A2AB0 /* Build configuration list for PBXNativeTarget "HttpUtilityTests" */ = { 520 | isa = XCConfigurationList; 521 | buildConfigurations = ( 522 | 86719EAE24720BD1002A2AB0 /* Debug */, 523 | 86719EAF24720BD1002A2AB0 /* Release */, 524 | ); 525 | defaultConfigurationIsVisible = 0; 526 | defaultConfigurationName = Release; 527 | }; 528 | /* End XCConfigurationList section */ 529 | }; 530 | rootObject = 86719E8D24720BD1002A2AB0 /* Project object */; 531 | } 532 | -------------------------------------------------------------------------------- /HttpUtility.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /HttpUtility.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /HttpUtility.xcodeproj/xcshareddata/xcschemes/HttpUtility.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 32 | 33 | 39 | 40 | 41 | 42 | 44 | 50 | 51 | 52 | 53 | 54 | 64 | 65 | 71 | 72 | 78 | 79 | 80 | 81 | 83 | 84 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /HttpUtility/Extensions/EncodableExtension.swift: -------------------------------------------------------------------------------- 1 | // 2 | // EncodableExtension.swift 3 | // HttpUtility 4 | // 5 | // Created by CodeCat15 on 5/31/20. 6 | // Copyright © 2020 CodeCat15. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | extension Encodable 12 | { 13 | func convertToQueryStringUrl(urlString: String) -> URL? 14 | { 15 | var components = URLComponents(string: urlString) 16 | if(components != nil) 17 | { 18 | let requestDictionary = convertToDictionary() 19 | 20 | if(requestDictionary != nil) 21 | { 22 | var queryItems: [URLQueryItem] = [] 23 | requestDictionary?.forEach({ (key, value) in 24 | if(value != nil){ 25 | let strValue = value.map { String(describing: $0) } 26 | if(strValue != nil && strValue?.count != 0){ 27 | queryItems.append(URLQueryItem(name: key, value: strValue)) 28 | } 29 | } 30 | }) 31 | 32 | components?.queryItems = queryItems 33 | return components?.url! 34 | } 35 | } 36 | 37 | debugPrint("convertToQueryStringUrl => Error => Conversion failed, please make sure to pass a valid urlString and try again") 38 | 39 | return nil 40 | } 41 | 42 | func convertToDictionary() -> [String: Any?]? 43 | { 44 | do { 45 | let encoder = try JSONEncoder().encode(self) 46 | let result = (try? JSONSerialization.jsonObject(with: encoder, options: .allowFragments)).flatMap{$0 as? [String: Any?]} 47 | 48 | return result 49 | 50 | } catch let error { 51 | debugPrint(error) 52 | } 53 | 54 | return nil 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /HttpUtility/HUHttpMethods.swift: -------------------------------------------------------------------------------- 1 | // 2 | // HUHttpMethods.swift 3 | // HttpUtility 4 | // 5 | // Created by CodeCat15 on 1/22/21. 6 | // Copyright © 2021 CodeCat15. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | public enum HUHttpMethods : String 12 | { 13 | case get = "GET" 14 | case post = "POST" 15 | case put = "PUT" 16 | case delete = "DELETE" 17 | } 18 | -------------------------------------------------------------------------------- /HttpUtility/HUNetworkError.swift: -------------------------------------------------------------------------------- 1 | // 2 | // HUNetworkError.swift 3 | // HttpUtility 4 | // 5 | // Created by CodeCat15 on 1/22/21. 6 | // Copyright © 2021 CodeCat15. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | public struct HUNetworkError : Error 12 | { 13 | let reason: String? 14 | let httpStatusCode: Int? 15 | let requestUrl: URL? 16 | let requestBody: String? 17 | let serverResponse: String? 18 | 19 | init(withServerResponse response: Data? = nil, forRequestUrl url: URL, withHttpBody body: Data? = nil, errorMessage message: String, forStatusCode statusCode: Int) 20 | { 21 | self.serverResponse = response != nil ? String(data: response!, encoding: .utf8) : nil 22 | self.requestUrl = url 23 | self.requestBody = body != nil ? String(data: body!, encoding: .utf8) : nil 24 | self.httpStatusCode = statusCode 25 | self.reason = message 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /HttpUtility/HURequest.swift: -------------------------------------------------------------------------------- 1 | // 2 | // HURequest.swift 3 | // HttpUtility 4 | // 5 | // Created by CodeCat15 on 1/31/21. 6 | // Copyright © 2021 CodeCat15. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | protocol Request { 12 | var url: URL { get set } 13 | var method: HUHttpMethods { get set } 14 | } 15 | 16 | public struct HURequest : Request { 17 | var url: URL 18 | var method: HUHttpMethods 19 | var requestBody: Data? = nil 20 | 21 | public init(withUrl url: URL, forHttpMethod method: HUHttpMethods, requestBody: Data? = nil) { 22 | self.url = url 23 | self.method = method 24 | self.requestBody = requestBody != nil ? requestBody : nil 25 | } 26 | } 27 | 28 | public struct HUMultiPartRequest : Request { 29 | 30 | var url: URL 31 | var method: HUHttpMethods 32 | var request : Encodable 33 | 34 | public init(withUrl url: URL, forHttpMethod method: HUHttpMethods, requestBody: Encodable) { 35 | self.url = url 36 | self.method = method 37 | self.request = requestBody 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /HttpUtility/HttpUtility.h: -------------------------------------------------------------------------------- 1 | // 2 | // HttpUtility.h 3 | // HttpUtility 4 | // 5 | // Created by CodeCat15 on 5/17/20. 6 | // Copyright © 2020 CodeCat15. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for HttpUtility. 12 | FOUNDATION_EXPORT double HttpUtilityVersionNumber; 13 | 14 | //! Project version string for HttpUtility. 15 | FOUNDATION_EXPORT const unsigned char HttpUtilityVersionString[]; 16 | 17 | // In this header, you should import all the public headers of your framework using statements like #import 18 | 19 | 20 | -------------------------------------------------------------------------------- /HttpUtility/HttpUtility.swift: -------------------------------------------------------------------------------- 1 | // 2 | // HttpUtility.swift 3 | // HttpUtility 4 | // 5 | // Created by CodeCat15 on 5/17/20. 6 | // Copyright © 2020 CodeCat15. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | public class HttpUtility 12 | { 13 | public static let shared = HttpUtility() 14 | public var authenticationToken : String? = nil 15 | public var customJsonDecoder : JSONDecoder? = nil 16 | 17 | private init(){} 18 | 19 | public func request(huRequest: HURequest, resultType: T.Type, completionHandler:@escaping(Result)-> Void) 20 | { 21 | switch huRequest.method 22 | { 23 | case .get: 24 | getData(requestUrl: huRequest.url, resultType: resultType) { completionHandler($0)} 25 | break 26 | 27 | case .post: 28 | postData(request: huRequest, resultType: resultType) { completionHandler($0)} 29 | break 30 | 31 | case .put: 32 | putData(requestUrl: huRequest.url, resultType: resultType) { completionHandler($0)} 33 | break 34 | 35 | case .delete: 36 | deleteData(requestUrl: huRequest.url, resultType: resultType) { completionHandler($0)} 37 | break 38 | } 39 | } 40 | 41 | // MARK: - Multipart 42 | public func requestWithMultiPartFormData(multiPartRequest: HUMultiPartRequest, responseType: T.Type, completionHandler:@escaping(Result)-> Void) { 43 | postMultiPartFormData(request: multiPartRequest) { completionHandler($0) } 44 | } 45 | 46 | // MARK: - Private functions 47 | private func createJsonDecoder() -> JSONDecoder 48 | { 49 | let decoder = customJsonDecoder != nil ? customJsonDecoder! : JSONDecoder() 50 | if(customJsonDecoder == nil) { 51 | decoder.dateDecodingStrategy = .iso8601 52 | } 53 | return decoder 54 | } 55 | 56 | private func createUrlRequest(requestUrl: URL) -> URLRequest 57 | { 58 | var urlRequest = URLRequest(url: requestUrl) 59 | if(authenticationToken != nil) { 60 | urlRequest.setValue(authenticationToken!, forHTTPHeaderField: "authorization") 61 | } 62 | 63 | return urlRequest 64 | } 65 | 66 | private func decodeJsonResponse(data: Data, responseType: T.Type) -> T? 67 | { 68 | let decoder = createJsonDecoder() 69 | do { 70 | return try decoder.decode(responseType, from: data) 71 | }catch let error { 72 | debugPrint("error while decoding JSON response =>\(error.localizedDescription)") 73 | } 74 | return nil 75 | } 76 | 77 | // MARK: - GET Api 78 | private func getData(requestUrl: URL, resultType: T.Type, completionHandler:@escaping(Result)-> Void) 79 | { 80 | var urlRequest = self.createUrlRequest(requestUrl: requestUrl) 81 | urlRequest.httpMethod = HUHttpMethods.get.rawValue 82 | 83 | performOperation(requestUrl: urlRequest, responseType: T.self) { (result) in 84 | completionHandler(result) 85 | } 86 | } 87 | 88 | // MARK: - POST Api 89 | private func postData(request: HURequest, resultType: T.Type, completionHandler:@escaping(Result)-> Void) 90 | { 91 | var urlRequest = self.createUrlRequest(requestUrl: request.url) 92 | urlRequest.httpMethod = HUHttpMethods.post.rawValue 93 | urlRequest.httpBody = request.requestBody 94 | urlRequest.addValue("application/json", forHTTPHeaderField: "content-type") 95 | 96 | performOperation(requestUrl: urlRequest, responseType: T.self) { (result) in 97 | completionHandler(result) 98 | } 99 | } 100 | 101 | private func postMultiPartFormData(request: HUMultiPartRequest, completionHandler:@escaping(Result)-> Void) 102 | { 103 | let boundary = "-----------------------------\(UUID().uuidString)" 104 | let lineBreak = "\r\n" 105 | var urlRequest = self.createUrlRequest(requestUrl: request.url) 106 | urlRequest.httpMethod = HUHttpMethods.post.rawValue 107 | urlRequest.addValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") 108 | 109 | var postBody = Data() 110 | 111 | let requestDictionary = request.request.convertToDictionary() 112 | if(requestDictionary != nil) 113 | { 114 | requestDictionary?.forEach({ (key, value) in 115 | if(value != nil) { 116 | let strValue = value.map { String(describing: $0) } 117 | if(strValue != nil && strValue?.count != 0) { 118 | postBody.append("--\(boundary + lineBreak)" .data(using: .utf8)!) 119 | postBody.append("Content-Disposition: form-data; name=\"\(key)\" \(lineBreak + lineBreak)" .data(using: .utf8)!) 120 | postBody.append("\(strValue! + lineBreak)".data(using: .utf8)!) 121 | } 122 | } 123 | }) 124 | 125 | // TODO: Next release 126 | // if(huRequest.media != nil) { 127 | // huRequest.media?.forEach({ (media) in 128 | // postBody.append("--\(boundary + lineBreak)" .data(using: .utf8)!) 129 | // postBody.append("Content-Disposition: form-data; name=\"\(media.parameterName)\"; filename=\"\(media.fileName)\" \(lineBreak + lineBreak)" .data(using: .utf8)!) 130 | // postBody.append("Content-Type: \(media.mimeType + lineBreak + lineBreak)" .data(using: .utf8)!) 131 | // postBody.append(media.data) 132 | // postBody.append(lineBreak .data(using: .utf8)!) 133 | // }) 134 | // } 135 | 136 | postBody.append("--\(boundary)--\(lineBreak)" .data(using: .utf8)!) 137 | 138 | urlRequest.addValue("\(postBody.count)", forHTTPHeaderField: "Content-Length") 139 | urlRequest.httpBody = postBody 140 | 141 | performOperation(requestUrl: urlRequest, responseType: T.self) { (result) in 142 | completionHandler(result) 143 | } 144 | } 145 | } 146 | 147 | // MARK: - PUT Api 148 | private func putData(requestUrl: URL, resultType: T.Type, completionHandler:@escaping(Result)-> Void) 149 | { 150 | var urlRequest = self.createUrlRequest(requestUrl: requestUrl) 151 | urlRequest.httpMethod = HUHttpMethods.put.rawValue 152 | 153 | performOperation(requestUrl: urlRequest, responseType: T.self) { (result) in 154 | completionHandler(result) 155 | } 156 | } 157 | 158 | // MARK: - DELETE Api 159 | private func deleteData(requestUrl: URL, resultType: T.Type, completionHandler:@escaping(Result)-> Void) 160 | { 161 | var urlRequest = self.createUrlRequest(requestUrl: requestUrl) 162 | urlRequest.httpMethod = HUHttpMethods.delete.rawValue 163 | 164 | performOperation(requestUrl: urlRequest, responseType: T.self) { (result) in 165 | completionHandler(result) 166 | } 167 | } 168 | 169 | // MARK: - Perform data task 170 | private func performOperation(requestUrl: URLRequest, responseType: T.Type, completionHandler:@escaping(Result) -> Void) 171 | { 172 | URLSession.shared.dataTask(with: requestUrl) { (data, httpUrlResponse, error) in 173 | 174 | let statusCode = (httpUrlResponse as? HTTPURLResponse)?.statusCode 175 | if(error == nil && data != nil && data?.count != 0) { 176 | let response = self.decodeJsonResponse(data: data!, responseType: responseType) 177 | if(response != nil) { 178 | completionHandler(.success(response)) 179 | }else { 180 | completionHandler(.failure(HUNetworkError(withServerResponse: data, forRequestUrl: requestUrl.url!, withHttpBody: requestUrl.httpBody, errorMessage: error.debugDescription, forStatusCode: statusCode!))) 181 | } 182 | } 183 | else { 184 | let networkError = HUNetworkError(withServerResponse: data, forRequestUrl: requestUrl.url!, withHttpBody: requestUrl.httpBody, errorMessage: error.debugDescription, forStatusCode: statusCode!) 185 | completionHandler(.failure(networkError)) 186 | } 187 | 188 | }.resume() 189 | } 190 | } 191 | -------------------------------------------------------------------------------- /HttpUtility/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | $(PRODUCT_BUNDLE_PACKAGE_TYPE) 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | $(CURRENT_PROJECT_VERSION) 21 | 22 | 23 | -------------------------------------------------------------------------------- /HttpUtilityTests/ExtensionsTests/EncodableExtensionUnitTest.swift: -------------------------------------------------------------------------------- 1 | // 2 | // EncodableExtensionUnitTest.swift 3 | // HttpUtilityTests 4 | // 5 | // Created by CodeCat15 on 5/31/20. 6 | // Copyright © 2020 CodeCat15. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | @testable import HttpUtility 11 | 12 | class EncodableExtensionUnitTest: XCTestCase { 13 | 14 | private let exampleUrl = "https://www.example.com" 15 | 16 | func test_convertToQueryStringUrl_With_SimpleStructure_Returuns_QueryStringUrl() 17 | { 18 | // ARRANGE 19 | struct Simple : Encodable {let name, description: String} 20 | let objSimple = Simple(name: UUID().uuidString, description: UUID().uuidString) 21 | 22 | // ACT 23 | let result = objSimple.convertToQueryStringUrl(urlString: exampleUrl)! 24 | 25 | // ASSERT 26 | XCTAssertNotNil(result) 27 | } 28 | 29 | func test_convertToQueryStringUrl_With_IntegerValue_Returuns_QueryStringUrl() 30 | { 31 | // ARRANGE 32 | struct simple : Encodable { 33 | let id: Int 34 | let name: String 35 | 36 | } 37 | let objSimple = simple(id: 1, name: UUID().uuidString) 38 | 39 | // ACT 40 | let result = objSimple.convertToQueryStringUrl(urlString: exampleUrl) 41 | 42 | // ASSERT 43 | XCTAssertNotNil(result) 44 | XCTAssertTrue(result!.absoluteString.contains("name=\(objSimple.name)")) 45 | XCTAssertTrue(result!.absoluteString.contains("id=\(objSimple.id)")) 46 | 47 | } 48 | 49 | //todo: need to test arrays 50 | func test_convertToQueryStringUrl_With_array_Returuns_QueryStringUrl() 51 | { 52 | // ARRANGE 53 | struct simple : Encodable { 54 | let id: [Int] 55 | let name: String 56 | 57 | } 58 | let objSimple = simple(id: [1,2,3], name: UUID().uuidString) 59 | 60 | // ACT 61 | let result = objSimple.convertToQueryStringUrl(urlString: exampleUrl) 62 | 63 | // ASSERT 64 | XCTAssertNotNil(result) 65 | XCTAssertTrue(result!.absoluteString.contains("name=\(objSimple.name)")) 66 | } 67 | 68 | func test_convertToQueryStringUrl_With_Multiple_DataType_Returuns_QueryStringUrl() 69 | { 70 | // ARRANGE 71 | struct simple : Encodable { 72 | let id: Int 73 | let name: String 74 | let salary: Double 75 | let isOnContract: Bool 76 | } 77 | 78 | let objSimple = simple(id: 1, name: "codecat15", salary: 25000.0, isOnContract: false) 79 | 80 | // ACT 81 | let result = objSimple.convertToQueryStringUrl(urlString: exampleUrl) 82 | 83 | // ASSERT 84 | XCTAssertNotNil(result) 85 | XCTAssertTrue(result!.absoluteString.contains("name=\(objSimple.name)")) 86 | XCTAssertTrue(result!.absoluteString.contains("id=\(objSimple.id)")) 87 | XCTAssertTrue(result!.absoluteString.contains("salary=25000")) 88 | XCTAssertTrue(result!.absoluteString.contains("isOnContract=0")) 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /HttpUtilityTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | $(PRODUCT_BUNDLE_PACKAGE_TYPE) 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /HttpUtilityTests/IntegrationTests/HttpUtilityIntegrationTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // HttpUtilityIntegrationTests.swift 3 | // HttpUtilityTests 4 | // 5 | // Created by CodeCat15 on 5/31/20. 6 | // Copyright © 2020 CodeCat15. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | @testable import HttpUtility 11 | 12 | struct MultiPartPostRequest : Encodable 13 | { 14 | let name, lastName: String 15 | } 16 | 17 | class HttpUtilityIntegrationTests: XCTestCase { 18 | 19 | private let _utility = HttpUtility.shared 20 | 21 | func test_getApiData_With_Valid_Request_Returns_Success() 22 | { 23 | // ARRANGE 24 | let requestUrl = URL(string: "https://api-dev-scus-demo.azurewebsites.net/api/Animal/GetAnimals") 25 | let expectation = XCTestExpectation(description: "Data received from server") 26 | let request = HURequest(withUrl: requestUrl!, forHttpMethod: .get) 27 | 28 | _utility.request(huRequest: request, resultType: AnimalResponse.self) { (response) in 29 | switch response 30 | { 31 | case .success(let animal): 32 | 33 | // ASSERT 34 | XCTAssertNotNil(animal) 35 | XCTAssertNotNil(animal?.data) 36 | 37 | case .failure(let error): 38 | 39 | // ASSERT 40 | XCTAssertNil(error) 41 | } 42 | 43 | expectation.fulfill() 44 | } 45 | 46 | wait(for: [expectation], timeout: 10.0) 47 | } 48 | 49 | func test_postApiData_With_Valid_Request_Returns_Success() 50 | { 51 | // ARRANGE 52 | let requestUrl = URL(string: "https://api-dev-scus-demo.azurewebsites.net/api/User/RegisterUser") 53 | let registerUserRequest = RegisterUserRequest(firstName: "code", lastName: "cat15", email: "codecat15@gmail.com", password: "1234") 54 | let registerUserBody = try! JSONEncoder().encode(registerUserRequest) 55 | let expectation = XCTestExpectation(description: "Data received from server") 56 | 57 | let request = HURequest(withUrl: requestUrl!, forHttpMethod: .post, requestBody: registerUserBody) 58 | // ACT 59 | _utility.request(huRequest: request, resultType: RegisterResponse.self) { (response) in 60 | switch response 61 | { 62 | case .success(let registerResponse): 63 | 64 | // ASSERT 65 | XCTAssertNotNil(registerResponse) 66 | 67 | case .failure(let error): 68 | 69 | // ASSERT 70 | XCTAssertNil(error) 71 | } 72 | expectation.fulfill() 73 | } 74 | 75 | wait(for: [expectation], timeout: 10.0) 76 | } 77 | 78 | func test_getApiData_WithQueryItem_Returns_Collection() 79 | { 80 | // ARRANGE 81 | let expectation = XCTestExpectation(description: "Data received from server") 82 | let request = PhoneRequest(color: "Red", manufacturer: nil) 83 | let requestUrl = request.convertToQueryStringUrl(urlString:"https://api-dev-scus-demo.azurewebsites.net/api/Product/GetSmartPhone") 84 | let huRequest = HURequest(withUrl: requestUrl!, forHttpMethod: .get) 85 | 86 | // ACT 87 | _utility.request(huRequest: huRequest, resultType: PhoneResponse.self) { (response) in 88 | 89 | switch response 90 | { 91 | case .success(let phoneResponse): 92 | 93 | // ASSERT 94 | XCTAssertNotNil(phoneResponse) 95 | phoneResponse?.data?.forEach({ (phone) in 96 | XCTAssertEqual(request.color,phone.color) 97 | }) 98 | 99 | case .failure(let error): 100 | XCTAssertNil(error) 101 | } 102 | expectation.fulfill() 103 | 104 | } 105 | 106 | wait(for: [expectation], timeout: 10.0) 107 | } 108 | 109 | func test_putService_Returns_Success() 110 | { 111 | // ARRANGE 112 | let expectation = XCTestExpectation(description: "Data received from server") 113 | let requestUrl = URL(string: "https://httpbin.org/put") 114 | let huRequest = HURequest(withUrl: requestUrl!, forHttpMethod: .put) 115 | 116 | // ACT 117 | _utility.request(huRequest: huRequest, resultType: Response.self) { (response) in 118 | 119 | switch response 120 | { 121 | case .success(let serviceResponse): 122 | 123 | // ASSERT 124 | XCTAssertNotNil(serviceResponse) 125 | 126 | case .failure(let error): 127 | XCTAssertNil(error) 128 | } 129 | expectation.fulfill() 130 | 131 | } 132 | 133 | wait(for: [expectation], timeout: 10.0) 134 | } 135 | 136 | func test_deleteService_Returns_Success() 137 | { 138 | // ARRANGE 139 | let expectation = XCTestExpectation(description: "Data received from server") 140 | let requestUrl = URL(string: "https://httpbin.org/delete") 141 | let huRequest = HURequest(withUrl: requestUrl!, forHttpMethod: .delete) 142 | 143 | // ACT 144 | _utility.request(huRequest: huRequest, resultType: Response.self) { (response) in 145 | 146 | switch response 147 | { 148 | case .success(let serviceResponse): 149 | 150 | // ASSERT 151 | XCTAssertNotNil(serviceResponse) 152 | 153 | case .failure(let error): 154 | XCTAssertNil(error) 155 | } 156 | expectation.fulfill() 157 | 158 | } 159 | 160 | wait(for: [expectation], timeout: 10.0) 161 | } 162 | 163 | func test_requestWithMultiPartFormData_WithSmallRequestBody_Returns_Success() 164 | { 165 | // ARRANGE 166 | let expectation = XCTestExpectation(description: "Multipart form data test") 167 | let requestUrl = URL(string: "https://api-dev-scus-demo.azurewebsites.net/TestMultiPart") 168 | 169 | let myStruct = MultiPartPostRequest(name: "Bruce", lastName: "Wayne") 170 | let multiPartRequest = HUMultiPartRequest(withUrl: requestUrl!, forHttpMethod: .post, requestBody: myStruct) 171 | 172 | // ACT 173 | _utility.requestWithMultiPartFormData(multiPartRequest: multiPartRequest, responseType: TestMultiPartResponse.self) { (response) in 174 | switch response 175 | { 176 | case .success(let serviceResponse): 177 | 178 | // ASSERT 179 | XCTAssertNotNil(serviceResponse) 180 | XCTAssertNotNil(serviceResponse?.data) 181 | XCTAssertEqual(myStruct.name, serviceResponse?.data.name) 182 | XCTAssertEqual(myStruct.lastName, serviceResponse?.data.lastName) 183 | 184 | case .failure(let error): 185 | XCTAssertNil(error.reason) 186 | } 187 | expectation.fulfill() 188 | } 189 | 190 | wait(for: [expectation], timeout: 10.0) 191 | } 192 | 193 | func test_requestWithMultiPartFormData_WithValidRequest_Returns_Success() 194 | { 195 | // ARRANGE 196 | let expectation = XCTestExpectation(description: "Multipart form data test") 197 | let requestUrl = URL(string: "https://api-dev-scus-demo.azurewebsites.net/api/Employee/MultiPartCodeChallenge") 198 | 199 | let multiPartFormRequest = MultiPartFormRequest(name: "Bruce", lastName: "Wayne", gender: "Male", departmentName: "Tech", managerName: "James Gordan", dateOfJoining: "01-09-2020", dateOfBirth: "07-07-1988") 200 | 201 | let multiPartRequest = HUMultiPartRequest(withUrl: requestUrl!, forHttpMethod: .post, requestBody: multiPartFormRequest) 202 | 203 | // ACT 204 | _utility.requestWithMultiPartFormData(multiPartRequest: multiPartRequest, responseType: MultiPartResponse.self) { (response) in 205 | // ASSERT 206 | switch response 207 | { 208 | case .success(let serviceResponse): 209 | 210 | // ASSERT 211 | XCTAssertNotNil(serviceResponse) 212 | XCTAssertNotNil(serviceResponse?.data) 213 | 214 | case .failure(let error): 215 | XCTAssertNil(error.reason) 216 | } 217 | expectation.fulfill() 218 | } 219 | 220 | wait(for: [expectation], timeout: 10.0) 221 | } 222 | 223 | func test_requestWithMultiPartFormData_WithMediaImage_Returns_Success() 224 | { 225 | // ARRANGE 226 | let expectation = XCTestExpectation(description: "Multipart form data test media upload") 227 | let requestUrl = URL(string: "https://api-dev-scus-demo.azurewebsites.net/api/Image/UploadImageMultiPartForm") 228 | let testImageFile = Bundle(for: type(of: self)).path(forResource: "batman", ofType: ".jpg") 229 | let imageData = UIImage(contentsOfFile: testImageFile!)?.jpegData(compressionQuality: 0.7) 230 | 231 | let fileUploadRequest = MultiPartFormFileUploadRequest(attachment: imageData!, fileName: "utilityTest") 232 | 233 | let multiPartRequest = HUMultiPartRequest(withUrl: requestUrl!, forHttpMethod: .post, requestBody: fileUploadRequest) 234 | 235 | // ACT 236 | _utility.requestWithMultiPartFormData(multiPartRequest: multiPartRequest, responseType: MultiPartImageUploadResponse.self) { (response) in 237 | // ASSERT 238 | switch response 239 | { 240 | case .success(let serviceResponse): 241 | 242 | // ASSERT 243 | XCTAssertNotNil(serviceResponse) 244 | XCTAssertNotNil(serviceResponse?.path) 245 | 246 | case .failure(let error): 247 | XCTAssertNil(error.reason) 248 | } 249 | expectation.fulfill() 250 | } 251 | 252 | wait(for: [expectation], timeout: 10.0) 253 | } 254 | } 255 | -------------------------------------------------------------------------------- /HttpUtilityTests/TestModel/Requests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Requests.swift 3 | // HttpUtilityTests 4 | // 5 | // Created by CodeCat15 on 6/3/20. 6 | // Copyright © 2020 CodeCat15. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | // MARK: - RegisterUserRequest 12 | struct RegisterUserRequest: Encodable 13 | { 14 | let firstName, lastName, email, password: String 15 | 16 | enum CodingKeys: String, CodingKey { 17 | case firstName = "First_Name" 18 | case lastName = "Last_Name" 19 | case email = "Email" 20 | case password = "Password" 21 | } 22 | } 23 | 24 | // MARK: - PhoneRequest 25 | 26 | struct PhoneRequest: Encodable 27 | { 28 | let color, manufacturer: String? 29 | } 30 | 31 | struct MultiPartFormRequest: Encodable 32 | { 33 | let name, lastName, gender, departmentName, managerName: String 34 | let dateOfJoining, dateOfBirth: String 35 | 36 | enum CodingKeys: String, CodingKey { 37 | 38 | case name = "Name" 39 | case lastName = "LastName" 40 | case dateOfJoining = "DateOfJoining" 41 | case dateOfBirth = "DateOfBirth" 42 | case gender = "Gender" 43 | case departmentName = "DepartmentName" 44 | case managerName = "ManagerName" 45 | } 46 | } 47 | 48 | struct MultiPartFormFileUploadRequest : Encodable 49 | { 50 | let attachment: Data 51 | let fileName : String 52 | enum CodingKeys : String, CodingKey { 53 | case fileName = "FileName" 54 | case attachment = "Attachment" 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /HttpUtilityTests/TestModel/Response.swift: -------------------------------------------------------------------------------- 1 | // 2 | // EmployeeResponse.swift 3 | // HttpUtilityTests 4 | // 5 | // Created by CodeCat15 on 5/31/20. 6 | // Copyright © 2020 CodeCat15. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | // MARK: - AnimalResponse 12 | struct AnimalResponse: Decodable { 13 | let errorMessage: String? 14 | let data: [Animal]? 15 | } 16 | 17 | // MARK: - Datum 18 | struct Animal: Decodable { 19 | let name: String 20 | let image: String 21 | } 22 | 23 | // MARK: - RegisterResponse 24 | struct RegisterResponse: Codable { 25 | let errorMessage: String? 26 | let data: EmployeeRegisterResponse? 27 | } 28 | 29 | // MARK: - EmployeeRegisterResponse 30 | struct EmployeeRegisterResponse: Codable { 31 | let name, email, id, joining: String? 32 | } 33 | 34 | // MARK: - PhoneResponse 35 | struct PhoneResponse: Decodable { 36 | let errorMessage: String? 37 | let data: [Phone]? 38 | } 39 | 40 | // MARK: - Phone 41 | struct Phone: Decodable { 42 | let name, operatingSystem, manufacturer, color: String? 43 | } 44 | 45 | // MARK: - DataClass 46 | struct TestMultiPartResponse: Decodable { 47 | let errorMessage: String 48 | let data: DataClass 49 | } 50 | 51 | // MARK: - DataClass 52 | struct DataClass: Decodable { 53 | let name, lastName: String 54 | } 55 | 56 | // MARK: - MultiPartResponse 57 | struct MultiPartResponse: Decodable { 58 | let errorMessage: String? 59 | let data: MultipartMessage? 60 | } 61 | 62 | // MARK: - MultipartMessage 63 | struct MultipartMessage: Decodable { 64 | let message: String? 65 | } 66 | 67 | // MARK: - Employee 68 | struct Response: Decodable { 69 | let args: Args? 70 | let data: String? 71 | let files, form: Args? 72 | let headers: Headers? 73 | let employeeJSON, origin: String? 74 | let url: String? 75 | 76 | enum CodingKeys: String, CodingKey { 77 | case args, data, files, form, headers 78 | case employeeJSON 79 | case origin, url 80 | } 81 | } 82 | 83 | // MARK: - Args 84 | struct Args: Decodable { 85 | } 86 | 87 | // MARK: - Headers 88 | struct Headers: Decodable { 89 | let accept, acceptEncoding, acceptLanguage, contentLength: String? 90 | let host: String? 91 | let origin, referer: String? 92 | let secFetchDest, secFetchMode, secFetchSite, userAgent: String? 93 | let xAmznTraceID: String? 94 | 95 | enum CodingKeys: String, CodingKey { 96 | case accept 97 | case acceptEncoding 98 | case acceptLanguage 99 | case contentLength 100 | case host 101 | case origin 102 | case referer 103 | case secFetchDest 104 | case secFetchMode 105 | case secFetchSite 106 | case userAgent 107 | case xAmznTraceID 108 | } 109 | } 110 | 111 | // MARK: - Multipart image upload model 112 | struct MultiPartImageUploadResponse : Decodable { 113 | let path : String 114 | } 115 | -------------------------------------------------------------------------------- /HttpUtilityTests/batman.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codecat15/HttpUtility/9ac1c9ae7ae49f4fb5ba8d33d521673f8fd26323/HttpUtilityTests/batman.jpg -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 codecat15 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HttpUtility 2 | 3 | HttpUtility is a light weight open source MIT license project which is helpful in making HTTP requests to the server. It uses URLSession to make requests to the API and returns the [Result enum](https://developer.apple.com/documentation/swift/result) containing the decoded object in case of success or a error incase of failure. Right now this utility only decodes JSON response returned by the server. 4 | 5 | [![Build Status](https://travis-ci.com/codecat15/HttpUtility.svg?branch=master)](https://travis-ci.com/codecat15/HttpUtility) [![Twitter](https://img.shields.io/badge/twitter-@codecat15-blue.svg?style=flat)](https://twitter.com/codecat15) 6 | 7 | # Purpose of usage 8 | 9 | Most of the time iOS application just perform simple HTTP operations which include sending request to the server and getting a response and displaying it to the user. If your iOS app does that then you may use this utility which does not do too much of heavy lifting and just pushes your request to the server and returns you a decoded object. 10 | 11 | # Installation 12 | 13 | ## CocoaPods 14 | 15 | [CocoaPods](https://cocoapods.org/) is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate HttpUtility into your Xcode project using CocoaPods, specify it in your Podfile: 16 | 17 | ``` 18 | pod 'HttpUtility', '~> 1.2' 19 | ``` 20 | 21 | # Using HttpUtility 22 | 23 | ## Introduction 24 | 25 | HttpUtility can be used for basic http operations like get, post, put and delete. It uses [URLSession](https://developer.apple.com/documentation/foundation/urlsession) to perform operations and is just a wrapper around it. 26 | 27 | The best thing about this utility is that it takes a simple URL and returns you a decoded object if the request is successful and returns an error if the request fails. Say good bye to writing loops and custom code to parse JSON response. 28 | 29 | Given are the some of the examples on how you can make use of this utility 30 | 31 | 1. [Get request](https://github.com/codecat15/HttpUtility#get-request-example) 32 | 2. [Post request](https://github.com/codecat15/HttpUtility#post-request-example) 33 | 3. [Request with query string parameters](https://github.com/codecat15/HttpUtility#get-request-with-query-string-parameters) 34 | 4. [Request with MultiPartFormData](https://github.com/codecat15/HttpUtility#post-request-with-multipartformdata) 35 | 5. [Request with authentication token](https://github.com/codecat15/HttpUtility#authentication-token) 36 | 6. [Customize JSONDecoder in the Utility](https://github.com/codecat15/HttpUtility#token-and-custom-jsondecoder) 37 | 7. [HUNetworkError](https://github.com/codecat15/HttpUtility/tree/multipart-form-data-format#hunetworkerror) 38 | 39 | ## GET Request example 40 | 41 | ```swift 42 | let utility = HTTPUtility.shared // using the shared instance of the utility to make the API call 43 | let requestUrl = URL(string: "http://demo0333988.mockable.io/Employees") 44 | let request = HURequest(withUrl: requestUrl!, forHttpMethod: .get) 45 | 46 | utility.request(huRequest: request, resultType: Employees.self) { (response) in 47 | switch response 48 | { 49 | case .success(let employee): 50 | // code to handle the response 51 | 52 | case .failure(let error): 53 | // your code here to handle error 54 | 55 | } 56 | } 57 | ``` 58 | 59 | ## POST request example 60 | 61 | The httpUtility has an extra parameter "requestBody" where you should attach the data that you have to post to the server, in the given example the RegisterUserRequest is a struct inheriting from the [Encodable protocol](https://developer.apple.com/documentation/swift/encodable) 62 | 63 | ```swift 64 | let utiltiy = HttpUtility.shared // using the shared instance of the utility to make the API call 65 | 66 | let requestUrl = URL(string: "https://api-dev-scus-demo.azurewebsites.net/api/User/RegisterUser") 67 | let registerUserRequest = RegisterUserRequest(firstName: "code", lastName: "cat15", email: "codecat15@gmail.com", password: "1234") 68 | 69 | let registerUserBody = try! JSONEncoder().encode(registerUserRequest) 70 | let request = HURequest(withUrl: requestUrl!, forHttpMethod: .post, requestBody: registerUserBody) 71 | 72 | utility.request(huRequest: request, resultType: RegisterResponse.self) { (response) in 73 | switch response 74 | { 75 | case .success(let registerResponse): 76 | // code to handle the response 77 | 78 | case .failure(let error): 79 | // your code here to handle error 80 | 81 | } 82 | ``` 83 | 84 | ## GET request with Query string parameters 85 | 86 | ```swift 87 | let utiltiy = HttpUtility.shared // using the shared instance of the utility to make the API call 88 | let request = PhoneRequest(color: "Red", manufacturer: nil) 89 | 90 | // using the extension to convert the encodable request structure to a query string url 91 | let requestUrl = request.convertToQueryStringUrl(urlString:"https://api-dev-scus-demo.azurewebsites.net/api/Product/GetSmartPhone") 92 | 93 | let request = HURequest(withUrl: requestUrl!, forHttpMethod: .get) 94 | utility.request(huRequest: request, resultType: PhoneResponse.self) { (response) in 95 | 96 | switch response 97 | { 98 | case .success(let phoneResponse): 99 | // code to handle the response 100 | 101 | case .failure(let error): 102 | // your code here to handle error 103 | 104 | } 105 | } 106 | ``` 107 | 108 | ## POST request with MultiPartFormData 109 | 110 | ```swift 111 | 112 | let utiltiy = HttpUtility.shared // using the shared instance of the utility to make the API call 113 | let requestUrl = URL(string: "https://api-dev-scus-demo.azurewebsites.net/TestMultiPart") 114 | 115 | // your request model struct should implement the encodable protocol 116 | let requestModel = RequestModel(name: "Bruce", lastName: "Wayne") 117 | 118 | let multiPartRequest = HUMultiPartRequest(url: requestUrl!, method: .post, request: requestModel) 119 | 120 | utility.requestWithMultiPartFormData(multiPartRequest: multiPartRequest, responseType: TestMultiPartResponse.self) { (response) in 121 | switch response 122 | { 123 | case .success(let serviceResponse): 124 | // code to handle the response 125 | 126 | case .failure(let error): 127 | // code to handle failure 128 | } 129 | } 130 | ``` 131 | 132 | ## Authentication Token 133 | 134 | ```swift 135 | let utility = HttpUtility.shared 136 | let token = "your_token" 137 | utility.authenticationToken = token 138 | ``` 139 | 140 | if you are using a basic or a bearer token then make sure you put basic or bearer before your token starts 141 | 142 | ### Example: Basic token 143 | 144 | ```swift 145 | let basicToken = "basic your_token" 146 | let utility = HttpUtility.shared 147 | utility.authenticationToken = basicToken 148 | ``` 149 | 150 | ### Example: Bearer token 151 | 152 | ```swift 153 | let bearerToken = "bearer your_token" 154 | let utility = HttpUtility.shared 155 | utility.authenticationToken = bearerToken 156 | ``` 157 | 158 | ## Custom JSONDecoder 159 | 160 | At times it may happen that you may need to control the behaviour of the default [JSONDecoder](https://developer.apple.com/documentation/foundation/jsondecoder) being used to decode the JSON, for such scenarios the HTTPUtility provides a default init method where you can pass your own custom JSONDecoder and the HTTPUtility will make use of that Decoder and here's how you can do it 161 | 162 | ```swift 163 | let customJsonDecoder = JSONDecoder() 164 | customJsonDecoder.dateEncoding = .millisecondsSince1970 165 | let utility = HttpUtility.shared 166 | utility.customJsonDecoder = customJsonDecoder 167 | ``` 168 | 169 | ## Token and Custom JSONDecoder 170 | 171 | At times when you pass the token and the default JSONDecoder is just not enough, then you may use the init method of the utility to pass the token and a custom JSONDecoder both to make the API request and parse the JSON response 172 | 173 | ```swift 174 | let utility = HttpUtility.shared 175 | let customJsonDecoder = JSONDecoder() 176 | customJsonDecoder.dateEncoding = .millisecondsSince1970 177 | 178 | let bearerToken = "bearer your_token" 179 | 180 | utility.customJsonDecoder = customJsonDecoder 181 | utility.authenticationToken = bearerToken 182 | 183 | ``` 184 | 185 | ## HUNetworkError 186 | 187 | The HUNetworkError structure provides in detail description beneficial for debugging purpose, given are the following properties that will be populated in case an error occurs 188 | 189 | 1. **Status:** This will contain the HTTPStatus code for the request that we receive from the server. 190 | 191 | 2. **ServerResponse:** This will be the JSON string of the response you received from the server. (not to be confused with error parameter) on error if server returns the error JSON data that message will be decoded to human readable string. 192 | 193 | 3. **RequestUrl:** The request URL that you just called. 194 | 195 | 4. **RequestBody:** If you get failure on POST request this property would contain a string representation of the HTTPBody that was sent to the server. 196 | 197 | 5. **Reason:** This property would contain the debug description from the error closure parameter. 198 | 199 | This utility is for performing basic tasks, and is currently evolving, but if you have any specific feature in mind then please feel free to drop a request and I will try my best to implement it 200 | --------------------------------------------------------------------------------