GithubHelp home page GithubHelp logo

gca3020 / gaboardgamegeek Goto Github PK

View Code? Open in Web Editor NEW
8.0 6.0 5.0 226 KB

BoardGameGeek XMLAPI2 Swift Framework for interacting with games and collections on BGG

License: MIT License

Swift 98.84% Ruby 1.16%

gaboardgamegeek's Introduction

GABoardGameGeek

gitlab-workflow Version Swift version swift-package-manager Platform License Twitter

BoardGameGeek XMLAPI2 Swift Framework for interacting with games and collections on BGG

Features

  • Read-only access to user collections and games
  • Site Searching
  • TODO! Forums, Videos, Users, Geeklists, etc...
  • Comprehensive Test Coverage & Automated Coverage Reports

Requirements

  • iOS 10.0+, macOS 10.12+, tvOS 10.0+, watchOS 3.0+
  • Swift 5.0

Dependencies

Communication

  • If you'd like to ask a question, use Twitter.
  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

Example & Unit Tests

To run the unit tests, and see some additional usage details, clone the repo, and run pod install from the Example directory first.

Installation

GABoardGameGeek is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "GABoardGameGeek"

Usage

One of the things that I wanted to accomplish with this library is making the common things you would want to do very easy, as well as "Swifty". To that end, here are a number of the things you can do with this library.

Searching for a Game

One of the very first things you might want to do is to search for a game by name:

import GABoardGameGeek

GABoardGameGeek().searchFor("pandemic") { result in
    switch(result) {
    case .success(let searchResults):
        print(searchResults)
    case .failure(let error):
        print(error)
    }
}

Alternately, you might want to narrow down your search by only searching for exact matches, or only for items of a specific type, like expansions:

import GABoardGameGeek

GABoardGameGeek().searchFor("pandemic: on the brink", searchType: "boardgameexpansion", exactMatch: true) { result in
    switch(result) {
    case .success(let searchResults):
        print(searchResults)
    case .failure(let error):
        print(error)
    }
}

I have some plans for how the search type is specified, as these handful of strings for "type" are used pretty commonly throughout the API, but for now, specifying the string manually gets ths job done.

Reading a Game by ID

Once you have a game ID, getting the details for that game is easy:

GABoardGameGeek().getGameById(12345) { result in
    switch(result) {
    case .success(let game):
        print(game)
    case .failure(let error):
        print(error)
    }
}

Of course, you also might want to request a bunch of games at once:

GABoardGameGeek().getGamesById([1, 232, 41415, 12]) { result in
    switch(result) {
    case .success(let gameList):
        print(gameList)
    case .failure(let error):
        print(error)
    }
}

Additionally, you might want game statistics. These can be requested as well for either a single game, or the list of games

GABoardGameGeek().getGameById(12123, stats: true) { result in
    switch(result) {
    case .success(let game):
        print(game)
    case .failure(let error):
        print(error)
    }
}

Getting a User's Collection

Likewise, getting a user's collection is also quite easy. Simply specify their username. The result is an ApiResult containing a CollectionBoardGame array

GABoardGameGeek().getUserCollection("userName") { result in
    switch(result) {
    case .success(let gameCollection):
        print(gameCollection)
    case .failure(let error):
        print(error)
    }
}

Collection requests have a number of additional, optional parameters. You can request a "brief" collection, which will generally be returned and parsed much quicker, especially for users with large collections, but will not contain as many details about a game as a standard request. You can also request a collection with statistics, which will contain additional information about a game, such as it's overall rating, position in various rankings, and what this particular user has rated it. You can even combine these two parameters, and request brief game details, with a subset of game statistics.

Finally, BoardGameGeek's API generally takes a while to respond to Collection Requests, especially for users with very large collections. As a result, the call to request a collection has a default timeout of 90 seconds, during which time it will retry, as long as the server continues to respond with a 202 error code.

GABoardGameGeek().getUserCollection("userName", brief: true, stats: true, timeout: 120) { result in
    switch(result) {
    case .success(let gameCollection):
        print(gameCollection)
    case .failure(let error):
        print(error)
    }
}

Handling Results

With heavy inspiration taken from common Swift libraries like Alamofire, the primary way that API results are returned is in an ApiResult container. This container uses Swift Generics to hold values of different types.

If you don't like the Switch syntax, you can also access the results using some computed properties. The following two examples are equivalent:

// Use switch to handle results/errors
GABoardGameGeek().getGameById(12123) { result in
    switch(result) {
    case .success(let game):
        print(game)
    case .failure(let error):
        print(error)
    }
}

// Use accessors to handle results/erros
GABoardGameGeek().getGameById(12123) { result in
    if(result.isSuccess) {
        print(result.value!)
    }
    else {
        print(result.error!)
    }
}

So feel free to choose the syntax you prefer.

Error Handling

Whenever you are dealing with Networking APIs, there are a number of errors that can occur. I've done my best to prevent the ones I can, but there's nothing I can do when the API goes down, or a network connection is unavaialable. When that happens, you can either add code to the .failure case of the ApiResult enum, or check result.isFailure.

There are a few classes of error in the BggError enumeration:

  • connectionError: Something went wrong with the network connection itself, and the API could not be reached.
  • serverNotReady: Seen when querying a user's collection. The server is not ready yet, but our timeout has expired.
  • apiError: There was an error in the results of the API, things like invalid usernames will cause this.
  • xmlError: There was an error parsing the XML response from the API. If you see one of these, please Contact Me or create an issue with the request you were making and the detail text from the error.

Changelog

See CHANGELOG for a list of all changes and their corresponding versions.

License

GABoardGameGeek is released under the MIT license. See LICENSE for details.

gaboardgamegeek's People

Contributors

airtoxin avatar gca3020 avatar toobiz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

gaboardgamegeek's Issues

Error on search with multiple words

Thanks for the API!

I have two errors when I do a search with several words:
"7 wonders" or "Pandemic legacy", I have no results when I print searchResults... With just "wonders" it's working.
And when I type just a number I have no results too :/

Enhance BggError.connectionError to add details

With the AlamoFire update changing the error handling, the error details are no longer passed through the connectionError enum. It would be nice to add some details, or perhaps just wrap around the entire AFError to include it, so the user could display a valid error message when something is wrong with the network.

Update to latest Alamofire and SWXMLHash

First, thanks for the library, I've been using it for a while and it's great!

The problem is that it is outdated, I managed to make it work on my old iMac modifying the pod file to get older versions of Alamofire and WSXMLHash. But now I'm working with a new Mini, new OSX, new Swift, the hassle was getting to big.
I'm not a programmer, not by a long stretch, but I managed to hunt down the problems, and everything falls in place with just 4 edits in one file. Problem is, I can barely manage git repositories on my own, I have no idea how to show my changes to others (pull requests???), so I'll go old school (stone age old school).

the file is ApiAdapter.swift
line 69 -> AF.request(baseUrl, parameters: params)
line 74 -> case .success(let value):
line 79 -> closure(.success(value))
line 106 -> let xmlIndexer = XMLHash.parse(xml)

(please, don't tell anybody on stack overflow about this, I already have a super noob reputation :) )

The above changes get rid of the errors, and the library seems to work. There are a few warnings on the Deployment Targets and Swift 5, I managed tot week those manually in Xcode, and have no idea where to look for those in the code.

Can you please (please, please, please, please) find the time to update the master branch?

Convert URL string constants to Enumerations

Swift enumerations support assigning string constants to their cases. Rather than declaring a bunch of let constants for the URL base addresses, it will allow stronger type checking to pass them as enumerations.

SWXMLHash / Xcode 9

Hi there,

with the recent version of SWXMLHash it doesn't work anymore.
GABoardGameGeek has a dependency to SWXMLHash and requires 3.0.0 (SWXMLHash ~> '3.0.0').
But the recent version of SWXMLHash is 4.0.0.
So could you update the repo so that its supporting SWXMLHash 4.0.0?

Thanks!

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.