-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9f93131
Showing
22 changed files
with
1,170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)")! | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)! | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.