Coder Social home page Coder Social logo

fetcher's Introduction

Fetcher

"The most fun you'll have with a networking API in Swift!" - Author of this library

Overview

This library implements the WHAT-WG Fetch API in Swift. I started it as a project to learn this language while doing something (hopefully) useful. Since the fetch API will become common in browsers and web application programming, it seemed like a good project to use to "kick the tires" in Swift.

In addition, the fetch API makes use of Promises, so that was also a fun thing to learn.

Installing

For now, you need to include the files in your project, or reference the Xcode project.

How It Works

Fetcher defines a protocol, Fetch, with a method that implements the mechanics of a fetch. The primary implementation is provided by an extension to NSURLSession, which is the standard API for networking on macOS and iOS.

The return value to a fetch() call is a Promise of a Response. A Promise is a data structure used commonly in JavaScript and other environments; it models an asynchronous operation which returns a value in the future. It supports chaining and error handling. More information on promises can be found on the MDN Promise page.

Examples

The simplest example is making a fetch and accessing the response data as JSON. The JSON is the standard AnyObject representation returned by NSJSONSerialization.

fetch("https://api.github.com")
  .then { (response) in response.json() }
  .then { (json) -> AnyObject in 
    print(json)
    return json
  }
  catch_ { (error) in 
    print(error)
  }

In addition to the URL, you can specify several kinds of options for the request. For example, to change the HTTP method used:

fetch("https://api.github.com", RequestInit(method: .HEAD))
  .then { (response) -> Response in
     print(response.headers.get("Content-Type"))
     return response
  }

You can ask for JSON and verify it's there. (Note that the test for equality with the string 'application/json' is not completely correct since MIME types have special rules for equality checks.)

fetch("https://api.github.com", RequestInit(headers: Headers(["Accept": "application/json"])))
  .then { (response) in 
    if (response.headers.get("Content-Type")! != "application/json") {
        throw MyError("Not JSON!")
    }
    return response.json() 
  }
  .then { (json) -> AnyObject in 
    print(json)
    return json
  }

License

This library is licensed under the new BSD license.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.