husnjak / IGDB-SWIFT-API

A Swift wrapper for the IGDB.com Free Video Game Database API.
MIT License
60 stars 12 forks source link
apicalypse swift-wrapper wrapper

License CocoaPods test

IGDB SWIFT-API

A Swift wrapper for the IGDB.com Free Video Game Database API.

IMPORTANT

This wrapper is compatible with ONLY their newest release V4.

About IGDB

One of the principles behind IGDB.com is accessibility of data. We wish to share the data with anyone who wants to build cool video game oriented websites, apps and services. This means that the information you contribute to IGDB.com can be used by other projects as well.

Thus, you are not only contributing to the value of this site but to thousands of other projects as well. We are looking forward to see what exciting game related projects you come up with. Happy coding!

More info here:

Information about the Querying language APICalypse:

About the wrapper

This wrapper is written and tested in Swift 4.

The Wrapper can handle both the IGDB generated classes and JSON (Strings), I have chosen to make the Generated classes the standard way because it will make it easier to use as you don't have to create your own classes to hold the information.

Installation and setup

Swift Package

You can either import this library using Xcode by simply pasting this repository link:
XCode -> Swift Packages -> Add Package Dependecy
Or if you have a Package.swift file you can add this:

dependencies: [
.package(url: "https://github.com/husnjak/IGDB-SWIFT-API.git", from: "0.4.5"),
],
targets: [
    .target(name: "MyTarget", dependencies: ["IGDB-SWIFT-API"]),
]

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate IGDBWrapper into your Xcode project using CocoaPods, specify it in your Podfile:

$ pod 'IGDB-SWIFT-API', git: "https://github.com/husnjak/IGDB-API-SWIFT.git"

Then, run the following command:

$ pod install

Using your Twitch Developer Credentials

Authentication on iOS

It is not recommended to create new Access Tokens on the Users devices as you don't want to create multiple access_tokens for each device. It is recommended to create your token on a server and then use a proxy api to call the IGDB api, where you append the Bearer token for each request.

IGDB provides a free AWS CloudFormation template that you can deploy for this purpose with instructions on how to use it.

How to use the wrapper

The wrapper has two "wrapping" functions and a lot of helper functions (one for each endpoint..)
The two main functions are called apiRequest and apiJsonRequest and they handle all of the requests to the api.
The class APICalypse handles the new querying language, so that you don't need to care about structure and syntax as much.

NOTE
These examples above are only here to show you how to use the "manual" part of the wrapper. This wrapper comes with complete functions for each endpoint in the API :) so you don't have to deal with the manual stuff..

There are two functions for each endpoint, one for classes and one for json, for quick access. The difference between them is the name see the examples below:

wrapper.games(apiCalypse: APICalypse(), result: ([Proto_Game]) -> (Void), errorResponse: (RequestException) -> (Void))
wrapper.platforms(apiCalypse: APICalypse(), result: ([Proto_Platforms]) -> (Void), errorResponse: (RequestException) -> (Void))
wrapper.genres(apiCalypse: APICalypse(), result: ([Proto_Genres]) -> (Void), errorResponse: (RequestException) -> (Void))
...
wrapper.jsonGames(apiCalypse: APICalypse(), result: (String) -> (Void), errorResponse: (RequestException) -> (Void))
wrapper.jsonPlatforms(apiCalypse: APICalypse(), result: (String) -> (Void), errorResponse: (RequestException) -> (Void))
wrapper.jsonGenres(apiCalypse: APICalypse(), result: (String) -> (Void), errorResponse: (RequestException) -> (Void))
...

ImageBuilder

To simplify the process of building the image URLs for IGDB images there is a new function called imageBuilder which is a helping tool in requesting the perfect sized images for your project. The function requires you to get the image_id then set your desired size (resolution), set your desired image format (default is set to PNG).


let image_id = "mnljdjtrh44x4snmierh"
let imageURL = imageBuilder(imageID: image_id, size: .SCREENSHOT_HUGE, imageType: .PNG)

/*
* Result: 
* imageURL = https://images.igdb.com/igdb/image/upload/t_screenshot_huge/mnljdjtrh44x4snmierh.png
*/

More information about images can be found here

Exceptions

The wrapper returns an RequestException on every exception from the API. This exception hold three things:

Code Examples

let apicalypse = APICalypse() .fields(fields: "*") .sort(field: "release_dates.date", order: .DESCENDING)

wrapper.games(apiCalypse: apicalypse, result: { games in // Do something.. }) { error in // Do something.. }

* Search in the API:
__NOTE__  
Search objects contain the objects from search ex: Characters, Collections, Games, Platforms, and Themes.  
Search does not work with Sorting!
```swift
let wrapper = IGDBWrapper(clientID: "CLIENT_ID", accessToken: "ACCESS_TOKEN")

let apicalypse = APICalypse()
    .search(searchQuery: "Halo")
    .fields(fields: "*")
    .sort(field: "release_dates.date", order: .ASCENDING)

wrapper.search(apiCalypse: apicalypse, result: { searchResults in
    // Do something..
}) { error in
    // Do something..
}

let apicalypse = APICalypse() .fields(fields: "*") .sort(field: "release_dates.date", order: .DESCENDING) .where(query: "themes != 42")

wrapper.games(apiCalypse: apicalypse, result: { games in // Do something.. }) { error in // Do something.. }


## License
IGDB-SWIFT-API is available under the MIT license. See the LICENSE file for more info.