Skip to content

Instantly share code, notes, and snippets.

@jpmartha
Last active December 13, 2015 13:16
Show Gist options
  • Save jpmartha/d9590ccbc8bfe3afe6fb to your computer and use it in GitHub Desktop.
Save jpmartha/d9590ccbc8bfe3afe6fb to your computer and use it in GitHub Desktop.
Swift

Send request to connpass API with APIKit 1.0.0 and Himotoki 1.3.

APIKit is a library for building type-safe web API client in Swift.

Himotoki is a type-safe JSON decoding library purely written in Swift.

//
// Connpass.swift
// MyConnpass
//
// Created by JPMartha on 2015/12/10.
// Copyright © 2015年 JPMartha. All rights reserved.
//
import Foundation
import APIKit
import Himotoki
protocol ConnpassRequestType: RequestType {
}
extension ConnpassRequestType {
var baseURL: NSURL {
return NSURL(string: "http://connpass.com/api/v1")!
}
}
struct GetEventSearchRequest: ConnpassRequestType {
typealias Response = ResponseField
var method: HTTPMethod {
return .GET
}
var path: String {
return "/event"
}
var parameters: [String: AnyObject] {
return ["keyword": "関西モバイルアプリ研究会"]
}
func responseFromObject(object: AnyObject, URLResponse: NSHTTPURLResponse) -> Response? {
return try? decode(object)
}
}
struct ResponseField: Decodable {
let results_returned: Int
let results_available: Int
let results_start: Int
let events: [Event]
static func decode(e: Extractor) throws -> ResponseField {
return try ResponseField(
results_returned: e <| "results_returned",
results_available: e <| "results_available",
results_start: e <| "results_start",
events: e <|| "events"
)
}
}
struct Event: Decodable {
let event_id: Int
let title: String
let description: String
let event_url: String
static func decode(e: Extractor) throws -> Event {
return try Event(
event_id: e <| "event_id",
title: e <| "title",
description: e <| "description",
event_url: e <| "event_url"
)
}
}
class Connpass {
func sendRequest() {
let request = GetEventSearchRequest()
Session.sendRequest(request) { result in
switch result {
case .Success(let responseField):
print("results_returned: \(responseField.results_returned)")
print("results_available: \(responseField.results_available)")
print("results_start: \(responseField.results_start)")
for event in responseField.events {
print("event_id: \(event.event_id)")
print("title: \(event.title)")
print("description: \(event.description)")
print("event_url: \(event.event_url)")
}
case .Failure(let error):
print("error: \(error)")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment