Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
neonichu committed Sep 28, 2015
0 parents commit 9f93131
Show file tree
Hide file tree
Showing 22 changed files with 1,170 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
.gutter.json
*.gcda
*.gcno
*.xccheckout
xcuserdata
.idea
Pods
build
19 changes: 19 additions & 0 deletions Code/Asset.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Asset.swift
// Contentful
//
// Created by Boris Bügling on 18/08/15.
// Copyright © 2015 Contentful GmbH. All rights reserved.
//

public class Asset : Resource {
var scheme = "https"

public var fields: NSDictionary { return json["fields"] as! NSDictionary }

public var URL: NSURL {

let urlString = self.fields["file"]?["url"] as! String
return NSURL(string: "\(scheme):\(urlString)")!
}
}
34 changes: 34 additions & 0 deletions Code/CDAArray.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// CDAArray.swift
// Contentful
//
// Created by Boris Bügling on 18/08/15.
// Copyright © 2015 Contentful GmbH. All rights reserved.
//

import Foundation

public struct CDAArray<T : Resource> {
let errors: [ErrorType]? = nil

let items: [T]

let limit: UInt
let skip: UInt
let total: UInt

init(data: NSData) {
let json = try! NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers) as! NSDictionary

limit = json["limit"] as! UInt
skip = json["skip"] as! UInt
total = json["total"] as! UInt

var items = [T]()
for item in (json["items"] as! [NSDictionary]) {
let data = try! NSJSONSerialization.dataWithJSONObject(item, options: [])
items.append(T(data: data))
}
self.items = items
}
}
109 changes: 109 additions & 0 deletions Code/CDAClient.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//
// CDAClient.swift
// Contentful
//
// Created by Boris Bügling on 18/08/15.
// Copyright © 2015 Contentful GmbH. All rights reserved.
//

import Interstellar

public class CDAClient {
private let configuration: Configuration
private let session: NSURLSession
private let spaceIdentifier: String

private var scheme: String { return configuration.secure ? "https" : "http" }

public init(spaceIdentifier: String, accessToken: String, configuration: Configuration = Configuration()) {
let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
sessionConfiguration.HTTPAdditionalHeaders = [ "Authorization": "Bearer \(accessToken)" ]

session = NSURLSession(configuration: sessionConfiguration)

self.configuration = configuration
self.spaceIdentifier = spaceIdentifier
}

public func fetchAsset(identifier: String, completion: Result<Asset> -> Void) -> NSURLSessionDataTask {
let task = session.dataTaskWithURL(URLForFragment("assets/\(identifier)")) { (data, response, error) in
if let data = data {
let asset = Asset(data: data)
asset.scheme = self.scheme
completion(.Success(asset))
} else {
if let error = error {
completion(.Error(error))
}
}
}

task.resume()
return task
}

public func fetchContentType(identifier: String, completion: Result<ContentType> -> Void) -> NSURLSessionDataTask {
let task = session.dataTaskWithURL(URLForFragment("content_types/\(identifier)")) { (data, response, error) in
if let data = data {
completion(.Success(ContentType(data: data)))
} else {
if let error = error {
completion(.Error(error))
}
}
}

task.resume()
return task
}

public func fetchEntries(completion: Result<CDAArray<Entry>> -> Void) -> NSURLSessionDataTask {
let task = session.dataTaskWithURL(URLForFragment("entries")) { (data, response, error) in
if let data = data {
completion(.Success(CDAArray(data: data)))
} else {
if let error = error {
completion(.Error(error))
}
}
}

task.resume()
return task
}

public func fetchEntry(identifier: String, completion: Result<Entry> -> Void) -> NSURLSessionDataTask {
let task = session.dataTaskWithURL(URLForFragment("entries/\(identifier)")) { (data, response, error) in
if let data = data {
completion(.Success(Entry(data: data)))
} else {
if let error = error {
completion(.Error(error))
}
}
}

task.resume()
return task
}

public func fetchSpace(completion: Result<Space> -> Void) -> NSURLSessionDataTask {
let task = session.dataTaskWithURL(URLForFragment()) { (data, response, error) in
if let data = data {
completion(.Success(Space(data: data)))
} else {
if let error = error {
completion(.Error(error))
}
}
}

task.resume()
return task
}

private func URLForFragment(fragment: String = "", parameters: [NSObject: AnyObject]? = nil) -> NSURL {
let components = NSURLComponents(string: "\(scheme)://\(configuration.server)/spaces/\(spaceIdentifier)/\(fragment)")
return (components!.URL)!
}
}
28 changes: 28 additions & 0 deletions Code/Configuration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// Configuration.swift
// Contentful
//
// Created by Boris Bügling on 18/08/15.
// Copyright © 2015 Contentful GmbH. All rights reserved.
//

public struct Configuration {
public var previewMode = false
public var rateLimiting = false
public var secure = true
public var server = "cdn.contentful.com"
public var userAgentClient = "contentful.swift/0.1.0"

public var userAgent : String {
var osName = "iOS"
let osVersion: AnyObject = NSProcessInfo.processInfo().operatingSystemVersionString ?? "Unknown"

#if os(OSX)
osName = "OS X"
#elseif os(watchOS)
osName = "watch OS"
#endif

return "\(userAgentClient) (\(osName) \(osVersion))"
}
}
13 changes: 13 additions & 0 deletions Code/ContentType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// ContentType.swift
// Contentful
//
// Created by Boris Bügling on 18/08/15.
// Copyright © 2015 Contentful GmbH. All rights reserved.
//

import Foundation

public class ContentType : Resource {
public var fields: [NSDictionary] { return self.json["fields"] as! [NSDictionary] }
}
11 changes: 11 additions & 0 deletions Code/Entry.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// Entry.swift
// Contentful
//
// Created by Boris Bügling on 18/08/15.
// Copyright © 2015 Contentful GmbH. All rights reserved.
//

public class Entry : Resource {
public var fields: NSDictionary { return json["fields"] as! NSDictionary }
}
21 changes: 21 additions & 0 deletions Code/Resource.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Resource.swift
// Contentful
//
// Created by Boris Bügling on 18/08/15.
// Copyright © 2015 Contentful GmbH. All rights reserved.
//

import Foundation

public class Resource {
let json : NSDictionary

public var sys: NSDictionary { return json["sys"] as! NSDictionary }
public var identifier: String { return self.sys["id"] as! String }
public var type: String { return self.sys["type"] as! String }

public required init(data: NSData) {
json = try! NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers) as! NSDictionary
}
}
11 changes: 11 additions & 0 deletions Code/Space.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// Space.swift
// Contentful
//
// Created by Boris Bügling on 18/08/15.
// Copyright © 2015 Contentful GmbH. All rights reserved.
//

public class Space : Resource {
public var name: String { return self.json["name"] as! String }
}
26 changes: 26 additions & 0 deletions Contentful.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Pod::Spec.new do |s|
s.name = "Contentful"
s.version = "0.1.0"
s.summary = "Swift SDK for Contentful's Content Delivery API."
s.homepage = "https://github.com/contentful/contentful.swift/"
s.social_media_url = 'https://twitter.com/contentful'

s.license = {
:type => 'MIT',
:file => 'LICENSE'
}

s.authors = { "Boris Bügling" => "[email protected]" }
s.source = { :git => "https://github.com/contentful/contentful.swift.git",
:tag => s.version.to_s }
s.requires_arc = true

s.source_files = 'Code/*.swift'

s.ios.deployment_target = '9.0'
s.osx.deployment_target = '10.11'
#s.watchos.deployment_target = '2.0'
#s.tvos.deployment_target = '9.0'

s.dependency 'Interstellar', '~> 1.1.0'
end
Loading

0 comments on commit 9f93131

Please sign in to comment.