Makes building HTTP URLs and requests easy.

Overview

ModestProposal 2.0

Makes building HTTP URLs and requests easy. Can be used with any networking library that accepts NSURLRequest as a parameter.

Features

  • URL building helpers
  • Request building helpers
  • Basic authentication encoding
  • Validation of HTTP response, status codes, and content types
  • Enums for common HTTP status codes, content types, methods, and header fields

URL Building

let baseURL = NSURL(string: "http://test.com")!

// http://test.com/login
let loginURL = baseURL.relativeToPath("/login")

// http://test.com/data?id=100&page=3
let dataURL = baseURL.relativeToPath("/data", parameters: ["id": "100", "page": "3"])

Request building

let baseRequest = NSMutableURLRequest(URL: baseURL)

// Set custom header for all requests
baseRequest["Custom-Header"] = "Custom value"

// URL will have the parameters added to the end of it
let readRequest = baseRequest.GET(path: "/object", parameters: ["id": "100", "page": "3"])
readRequest[.Accept] = "application/json"

// HTTPBody will be set to parameters, content type will be "application/x-www-form-urlencoded"
// and length will be set to match the size of the data generated by parameters
let createRequest = baseRequest.POST(path: "/create", parameters: ["id": "100", "page": "3"])

// HTTPBody will be set to JSON data, content type will be "application/json"
// and length will be set appropriately
let updateRequest = baseRequest.PUT(path: "/update", JSONObject: ["test": 100])

// HTTPBody will be set to data, content type will be "application/octet-stream"
// and length will be set to the length of the supplied data
let data = NSData(contentsOfFile: "file")
let dataRequest = baseRequest.POST(path: "/data", body: data)

Basic authentication

let baseRequest = NSURLRequest(URL: baseURL)
let loginRequest = baseRequest.POST(path: "/login")
loginRequest.basicAuthorization(username: "test", password: "test")

Response validation

let response: NSURLResponse = ...

do {
    try response.validateIsSuccessfulJSON()
    // OR
    try response.validateIsSuccessfulImage()
    // OR
    try response.validateIsHTTP(statusCode: .OK, contentType: .ApplicationJSON)
    // OR
    try response.validateIsHTTP(statusCode: 200, contentType: "application/json")
    // OR
    try validate(when: respone.isHTTP, otherwise: HTTPError.UnexpectedResponse(response))
    try validate(when: respone.HTTP.statusCode == 200, otherwise: HTTPError.UnexpectedStatusCode(respone.HTTP.statusCode))
    try validate(when: respone.HTTP.MIMEType == "application/json", otherwise: HTTPError.UnexpectedContentType(respone.HTTP.MIMEType))
}
catch {
    // Handle error
}
You might also like...
Written in pure Swift, QuickLayout offers a simple and easy way to manage Auto Layout in code.
Written in pure Swift, QuickLayout offers a simple and easy way to manage Auto Layout in code.

QuickLayout QuickLayout offers an additional way, to easily manage the Auto Layout using only code. You can harness the power of QuickLayout to align

FrameLayoutKit is a super fast and easy to use autolayout kit
FrameLayoutKit is a super fast and easy to use autolayout kit

FrameLayoutKit FrameLayout is a super fast and easy to use layout library for iOS and tvOS. For Objective-C version: NKFrameLayoutKit (Deprecated, not

A Swift utility to make updating table views/collection views trivially easy and reliable.

ArrayDiff An efficient Swift utility to compute the difference between two arrays. Get the removedIndexes and insertedIndexes and pass them directly a

An extension that simplifies the work with Swift AutoLayout by writing cleaner, more natural and easy-reading code.

Installation For now you're able to clone repo in your project or download ZIP folder manually. git clone https://github.com/votehserxam/AutoLayout.gi

Auto Layout made easy
Auto Layout made easy

EasyPeasy is a Swift framework that lets you create Auto Layout constraints programmatically without headaches and never ending boilerplate code. Besi

Auto Layout In Swift Made Easy

Swiftstraints Swiftstraints can turn verbose auto-layout code: let constraint = NSLayoutConstraint(item: blueView, attr

An flexbox layout aimed at easy to use, which depend on Yoga.
An flexbox layout aimed at easy to use, which depend on Yoga.

DDFlexbox A flexbox framework for easy using. Install pod 'DDFlexbox' Template install Recommend using templates to create flexbox views. cd Script/

Easy Auto Layout

RKAutoLayout Easy AutoLayout TL;DR let view1: UIView = UIView() let view2: UIView = UIView() view1.addSubview(view2) /// Add all view1.rk_alAdd(

Auto Layout made easy with the Custom Layout.
Auto Layout made easy with the Custom Layout.

Auto Layout made easy with the Custom Layout. Getting started CocoaPods CocoaPods is a dependency manager for Cocoa projects. You can install it with

Releases(2.0.0)
Owner
Justin Kolb
Justin Kolb
Library that makes it easy to create multiple environments within a single app. You can switch environments without deleting the application.

AppContainer Library that makes it easy to create multiple environments within a single app. You can switch environments without deleting the applicat

null 8 Dec 15, 2022
CompositionalLayoutDSL, library to simplify the creation of UICollectionViewCompositionalLayout. It wraps the UIKit API and makes the code shorter and easier to read.

CompositionalLayoutDSL CompositionalLayoutDSL is a Swift library. It makes easier to create compositional layout for collection view. Requirements Doc

FABERNOVEL 44 Dec 27, 2022
TinyConstraints is the syntactic sugar that makes Auto Layout sweeter for human use.

TinyConstraints is the syntactic sugar that makes Auto Layout sweeter for human use. Features Pure Swift 5 sweetness. Everything you can do with Auto

Robert-Hein Hooijmans 3.8k Jan 5, 2023
SuperLayout is a Swift library that makes using Auto Layout a breeze.

SuperLayout is a library that adds a few custom operators to Swift that makes using the amazing NSLayoutAnchor API for Auto Layout a breeze. SuperLayo

Lionheart Software 53 Oct 12, 2022
✂ Easy to use and flexible library for manually laying out views and layers for iOS and tvOS. Supports AsyncDisplayKit.

ManualLayout Table of Contents Installation Usage API Cheat Sheet Installation Carthage Add the following line to your Cartfile. github "isair/ManualL

Baris Sencan 280 Sep 29, 2022
Breezz: an application with which it is easy and simple to practice breathing exercises and meditation

breezz will help you relax Breezz is an application with which it is easy and si

dumojo 1 Jan 18, 2022
Swift-picker-views - inline single and multi picker views for UIKit. Without tableview! Easy and simple

swift-picker-views Inline single and multiple picker views for UIKit. No tablevi

IBRAHIM YILMAZ 2 Jan 31, 2022
An easy way to create and layout UI components for iOS (Swift version).

Introduction Cupcake is a framework that allow you to easily create and layout UI components for iOS 8.0+. It use chaining syntax and provides some fr

nerdycat 288 Oct 9, 2022
MyLayout is a simple and easy objective-c framework for iOS view layout

MyLayout is a powerful iOS UI framework implemented by Objective-C. It integrates the functions with Android Layout,iOS AutoLayout,SizeClass, HTML CSS float and flexbox and bootstrap. So you can use LinearLayout,RelativeLayout,FrameLayout,TableLayout,FlowLayout,FloatLayout,PathLayout,GridLayout,LayoutSizeClass to build your App 自动布局 UIView UITableView UICollectionView RTL

欧阳大哥2013 4.2k Dec 30, 2022
FlightLayout is a light weight, and easy to learn layout framework as an extension of the UIView.

FlightLayout Introduction FlightLayout is a light weight, and easy to learn layout framework as an extension of the UIView. Functionally, it lives som

Anton 23 Apr 21, 2022