Skip to content

Commit c0d92aa

Browse files
committed
Inital commit for the http utility
1 parent 3ffac6c commit c0d92aa

File tree

8 files changed

+649
-0
lines changed

8 files changed

+649
-0
lines changed

HttpUtility.xcodeproj/project.pbxproj

Lines changed: 465 additions & 0 deletions
Large diffs are not rendered by default.

HttpUtility.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

HttpUtility/HttpUtility.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 <Foundation/Foundation.h>
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 <HttpUtility/PublicHeader.h>
18+
19+

HttpUtility/HttpUtility.swift

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
struct HttpMethodType {
12+
static let GET = "GET"
13+
static let POST = "POST"
14+
}
15+
16+
struct HttpHeaderFields
17+
{
18+
static let contentType = "content-type"
19+
}
20+
21+
struct HttpUtility
22+
{
23+
func getApiData<T:Decodable>(requestUrl: URL, resultType: T.Type, completionHandler:@escaping(_ result: T?)-> Void)
24+
{
25+
//todo: move this to a common function
26+
// need a date formatter to format the date response received
27+
// use url components for query string
28+
URLSession.shared.dataTask(with: requestUrl) { (responseData, httpUrlResponse, error) in
29+
30+
if(error == nil && responseData != nil && responseData?.count != 0)
31+
{
32+
//parse the responseData here
33+
let decoder = JSONDecoder()
34+
do {
35+
let result = try decoder.decode(T.self, from: responseData!)
36+
_=completionHandler(result)
37+
}
38+
catch let error{
39+
debugPrint("error occured while decoding = \(error.localizedDescription)")
40+
}
41+
}
42+
_=completionHandler(nil) //todo: you need to send error that you receive from server
43+
44+
}.resume()
45+
}
46+
47+
func postApiData<T:Decodable>(requestUrl: URL, requestBody: Data, resultType: T.Type, completionHandler:@escaping(_ result: T?)-> Void)
48+
{
49+
//todo: make this for posting multi-part form data
50+
var urlRequest = URLRequest(url: requestUrl)
51+
urlRequest.httpMethod = HttpMethodType.POST
52+
urlRequest.httpBody = requestBody
53+
urlRequest.addValue("application/json", forHTTPHeaderField: HttpHeaderFields.contentType)
54+
55+
URLSession.shared.dataTask(with: urlRequest) { (data, httpUrlResponse, error) in
56+
57+
if(data != nil && data?.count != 0)
58+
{
59+
do {
60+
61+
let response = try JSONDecoder().decode(T.self, from: data!)
62+
_=completionHandler(response)
63+
}
64+
catch let decodingError {
65+
debugPrint(decodingError)
66+
}
67+
}
68+
_=completionHandler(nil) //todo: you need to send error that you receive from server
69+
70+
}.resume()
71+
}
72+
}

HttpUtility/Info.plist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>$(DEVELOPMENT_LANGUAGE)</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleVersion</key>
20+
<string>$(CURRENT_PROJECT_VERSION)</string>
21+
</dict>
22+
</plist>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// HttpUtilityTests.swift
3+
// HttpUtilityTests
4+
//
5+
// Created by CodeCat15 on 5/17/20.
6+
// Copyright © 2020 CodeCat15. All rights reserved.
7+
//
8+
9+
import XCTest
10+
@testable import HttpUtility
11+
12+
class HttpUtilityTests: XCTestCase {
13+
14+
override func setUpWithError() throws {
15+
// Put setup code here. This method is called before the invocation of each test method in the class.
16+
}
17+
18+
override func tearDownWithError() throws {
19+
// Put teardown code here. This method is called after the invocation of each test method in the class.
20+
}
21+
22+
func testExample() throws {
23+
// This is an example of a functional test case.
24+
// Use XCTAssert and related functions to verify your tests produce the correct results.
25+
}
26+
27+
func testPerformanceExample() throws {
28+
// This is an example of a performance test case.
29+
self.measure {
30+
// Put the code you want to measure the time of here.
31+
}
32+
}
33+
34+
}

HttpUtilityTests/Info.plist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>$(DEVELOPMENT_LANGUAGE)</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleVersion</key>
20+
<string>1</string>
21+
</dict>
22+
</plist>

0 commit comments

Comments
 (0)