CocoaPods trunk is moving to be read-only. Read more on the blog, there are 18 months to go.
TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Jan 2018 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✗ |
Maintained by Maks Tishchenko.
Shram - is easy to use but powerful framework for communication with RESTful web services via HTTP requests. Shram will help you send a request to a server in just a few lines of code. The main advantage of this framework is clarity and easy of use.
Using cocoapods you need to write pod ’Shram’, '0.1.1’
in you podfile
As mentioned above Shram is very simple to use. Let's sort it out.
You can create object of ShramRequest
var request = ShramRequest(URL: "http://www.sample.com/api/method", method: "GET")
Then you can set the following parameters:
For sending request you can use method:
send(completion, failure)
for general requests
download(completion, progress, failure)
for downloading requests with progress
upload(completion, progress, failure)
for uploading requests with progress
For example:
let params = [
"someKey" : "someValue"
]
var request = ShramRequest(URL: "http://www.sample.com/api/method", method: "GET")
request.parameters = params as [String : AnyObject]?
req.send(completion: { (req, data, resp) in
}) { (req, err, resp) in
}
#Another way#
###Method GET###
To send simple GET request you need to write the following:
Shram.GET("http://www.sample.com/api/method",
completion: { (request, data, response) in
},
failure: { (request, error, response) in
}
)
Really easy. But what if we want to pass some parameters or headers? Since Swift allows to set default values for the function parameters you don't need to looking for other methods for it. The full version of GET method looks as follows:
Shram.GET("http://www.sample.com/api/method",
params: params,
headers: headers,
withParseKeys: ["firstKey", "secondKey", ...],
completion: { (request, data, response) in
},
failure: { (request, error, response) in
}
)
Let's look at the parameters:
{
"data" : {
"name": "userName",
"age": "userAge"
}
}
for getting needed data from response you need to set withParseKeys: ["data"]
as parameter to GET function.
completion: Closure is called when server has responded successfully. Returns:
failure: Closure is called when request is failed or server has responded with an error. Returns:
As you can see "params", "headers", "withParseKeys" are optional and you don't need to pass it to function without the need.
###Method POST###
Method POST is similar. There are short and full versions:
Shram.POST("http://www.sample.com/api/method",
contentType: contentType,
completion: { (request, data, response) in
},
failure: { (request, error, response) in
}
)
Shram.POST("http://www.sample.com/api/method",
params: params,
contentType: contentType,
headers: headers,
withParseKeys: ["firstKey", "secondKey", ...],
completion: { (request, data, response) in
},
failure: { (request, error, response) in
}
)
In this method you can see new parameter:
###Method PUT###
Shram.PUT(http://www.sample.com/api/method,
params: params,
contentType: contentType,
completion: { (request, data, response) in
},
failure: { (request, error, response) in
}
)
Shram.PUT(http://www.sample.com/api/method,
params: params,
contentType: contentType,
headers: headers,
withParseKeys: ["firstKey", "secondKey", ...],
completion: { (request, data, response) in
},
failure: {(request, error, response) in
}
)
###Method DELETE###
Shram.DELETE("http://www.sample.com/api/method",
completion: { (request, data, response) in
},
failure: { (request, error, response) in
}
)
Shram.DELETE("http://www.sample.com/api/method",
params: params,
headers: headers,
withParseKeys: ["firstKey", "secondKey", ...],
completion: { (request, data, response) in
},
failure: { (request, error, response) in
}
)