Skip to content

Commit dcfea5d

Browse files
authored
updating the readme file
1 parent e09173b commit dcfea5d

File tree

1 file changed

+131
-1
lines changed

1 file changed

+131
-1
lines changed

README.md

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,134 @@
11
# HttpUtility
2-
HttpUtility is an open source MIT license project which is can be helpful in parsing API in iOS project. It uses URLSession to parse the API and returns a decodable object. Right now this utility only parses JSON.
2+
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.
33

44
[![Build Status](https://travis-ci.com/codecat15/HttpUtility.svg?branch=master)](https://travis-ci.com/codecat15/HttpUtility)
5+
6+
# Purpose of usage
7+
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.
8+
9+
# Using HttpUtility
10+
## Introduction
11+
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.
12+
13+
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.
14+
15+
Given are the some of the examples on how you can make use of this utility
16+
17+
1. [Get request](https://github.com/codecat15/HttpUtility#GET%20Request%20example)
18+
2. [Post request](https://github.com/codecat15/HttpUtility#POST%20request%20example)
19+
3. [Request with query string parameters](https://github.com/codecat15/HttpUtility#GET%20request%20with%20Query%20string%20parameters)
20+
4. [Request with authentication token](https://github.com/codecat15/HttpUtility#Authentication%20Token)
21+
5. [Customize JSONDecoder](https://github.com/codecat15/HttpUtility#Custom%20JSONDecoder)
22+
23+
## GET Request example
24+
25+
```swift
26+
let requestUrl = URL(string: "http://demo0333988.mockable.io/Employees")
27+
let utility = HTTPUtility()
28+
utility.getData(requestUrl: requestUrl!, resultType: Employees.self) { (response) in
29+
switch response
30+
{
31+
case .success(let employee):
32+
// your code here to display data
33+
34+
case .failure(let error):
35+
// your code here to handle error
36+
37+
}
38+
}
39+
```
40+
41+
## POST request example
42+
43+
```swift
44+
let requestUrl = URL(string: "https://api-dev-scus-demo.azurewebsites.net/api/User/RegisterUser")
45+
let registerUserRequest = RegisterUserRequest(firstName: "code", lastName: "cat15", email: "[email protected]", password: "1234")
46+
let registerUserBody = try! JSONEncoder().encode(registerUserRequest)
47+
48+
let utiltiy = HttpUtility()
49+
utility.postData(requestUrl: requestUrl!, requestBody: registerUserBody, resultType: RegisterResponse.self) { (response) in
50+
switch response
51+
{
52+
case .success(let registerResponse):
53+
// your code here to display data
54+
55+
case .failure(let error):
56+
// your code here to handle error
57+
58+
}
59+
```
60+
61+
## GET request with Query string parameters
62+
63+
```swift
64+
let request = PhoneRequest(color: "Red", manufacturer: nil)
65+
66+
// using the extension to convert the encodable request structure to a query string url
67+
let requestUrl = request.convertToQueryStringUrl(urlString:"https://api-dev-scus-demo.azurewebsites.net/api/Product/GetSmartPhone")
68+
69+
let utility = HttpUtility()
70+
utility.getData(requestUrl: requestUrl!, resultType: PhoneResponse.self) { (response) in
71+
72+
switch response
73+
{
74+
case .success(let phoneResponse):
75+
// your code here to display data
76+
77+
case .failure(let error):
78+
// your code here to handle error
79+
80+
}
81+
}
82+
```
83+
84+
## Authentication Token
85+
86+
```swift
87+
let requestUrl = URL(string: "http://demo0333988.mockable.io/Employees")
88+
let utility = HttpUtility(token: "your authentication token")
89+
utility.getData(requestUrl: requestUrl!, resultType: Employees.self) { (response) in
90+
switch response
91+
{
92+
case .success(let employee):
93+
// your code here to display data
94+
95+
case .failure(let error):
96+
// your code here to handle error
97+
98+
}
99+
}
100+
```
101+
if you are using a basic or a bearer token then make sure you put basic or bearer before your token starts
102+
103+
### Example: Basic token
104+
```swift
105+
let basicToken = "basic your_token"
106+
let utility = HttpUtility(token: basicToken)
107+
```
108+
109+
### Example: Bearer token
110+
```
111+
let bearerToken = "bearer your_token"
112+
let utility = HttpUtility(token: bearerToken)
113+
```
114+
115+
## Custom JSONDecoder
116+
117+
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
118+
119+
```swift
120+
let customJsonDecoder = JSONDecoder()
121+
customJsonDecoder.dateEncoding = .millisecondsSince1970
122+
let utility = HttpUtility(WithJsonDecoder: customJsonDecoder)
123+
```
124+
## Token and Custom JSONDecoder
125+
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
126+
127+
```swift
128+
let customJsonDecoder = JSONDecoder()
129+
customJsonDecoder.dateEncoding = .millisecondsSince1970
130+
let utility = HttpUtility(token: "your_token", decoder: customJsonDecoder)
131+
132+
```
133+
134+
This utility is for performing basic tasks, and is currently evolving,

0 commit comments

Comments
 (0)