Skip to content

Commit

Permalink
Fixes Issue #19 : setup -> configure
Browse files Browse the repository at this point in the history
  • Loading branch information
colbylwilliams committed Dec 18, 2017
1 parent 2332f49 commit bb89f99
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 37 deletions.
10 changes: 5 additions & 5 deletions AzureData Sample/AzureData iOS Sample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
let accountName = UserDefaults.standard.string(forKey: databaseAccountNameKey) ?? Bundle.main.infoDictionary?[databaseAccountNameKey] as? String
let accountKey = UserDefaults.standard.string(forKey: databaseAccountKeyKey) ?? Bundle.main.infoDictionary?[databaseAccountKeyKey] as? String

storeDatabaseAccount(name: accountName, key: accountKey, andCallSetup: true)
storeDatabaseAccount(name: accountName, key: accountKey, andConfigure: true)
}


func storeDatabaseAccount(name: String?, key: String?, andCallSetup callSetup: Bool = false) {
func storeDatabaseAccount(name: String?, key: String?, andConfigure configure: Bool = false) {

print("storeDatabaseAccount name: \(name ?? "nil") key: \(key ?? "nil")")

UserDefaults.standard.set(name, forKey: databaseAccountNameKey)
UserDefaults.standard.set(key, forKey: databaseAccountKeyKey)

if let n = name, n != databaseAccountNameDefault, let k = key, k != databaseAccountKeyDefault {
if callSetup { AzureData.setup(forAccountNamed: n, withKey: k, ofType: .master) }
if configure { AzureData.configure(forAccountNamed: n, withKey: k, ofType: .master) }
} else {
AzureData.reset()
}
Expand All @@ -54,7 +54,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func showApiKeyAlert(_ application: UIApplication) {

if AzureData.isSetup() {
if AzureData.isConfigured() {

if let navController = window?.rootViewController as? UINavigationController, let databaseController = navController.topViewController as? DatabaseTableViewController {
databaseController.refreshData()
Expand Down Expand Up @@ -84,7 +84,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

alertController.addAction(UIAlertAction(title: "Done", style: .default) { a in

self.storeDatabaseAccount(name: alertController.textFields?.first?.text, key: alertController.textFields?.last?.text, andCallSetup: true)
self.storeDatabaseAccount(name: alertController.textFields?.first?.text, key: alertController.textFields?.last?.text, andConfigure: true)
})

window?.rootViewController?.present(alertController, animated: true) { }
Expand Down
2 changes: 1 addition & 1 deletion AzureData Sample/Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "colbylwilliams/Azure.iOS" "v0.5.4"
github "colbylwilliams/Azure.iOS" "v0.5.5"
18 changes: 9 additions & 9 deletions AzureData/Source/AzureData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation


// MARK: - Setup
// MARK: - Configure

/// Print responses, resources, etc. to output log
public var verboseLogging: Bool {
Expand All @@ -18,28 +18,28 @@ public var verboseLogging: Bool {
}


/// Whether or not `setup` has been called on the client
public func isSetup() -> Bool { return DocumentClient.default.setup }
/// Whether or not `configure` has been called on the client
public func isConfigured() -> Bool { return DocumentClient.default.isConfigured }


/// Sets up the client. This should be called before performing any CRUD operations
/// Configures the client. This should be called before performing any CRUD operations
///
/// - Parameters:
/// - name: The name of the Cosmos DB account - used to create resource urls
/// - key: A master read/read-write key for the account, or a permission token for a resource
/// - keyType: The type of key - `.master` read/read-write key or a `.resource` permission token
public func setup (forAccountNamed name: String, withKey key: String, ofType keyType: TokenType) {
return DocumentClient.default.setup (forAccountNamed: name, withKey: key, ofType: keyType)
public func configure (forAccountNamed name: String, withKey key: String, ofType keyType: TokenType) {
return DocumentClient.default.configure (forAccountNamed: name, withKey: key, ofType: keyType)
}

/// Sets up the client. This should be called before performing any CRUD operations
/// Configures the client. This should be called before performing any CRUD operations
///
/// - Parameters:
/// - name: The custom domain of the Cosmos DB account - used to create resource urls
/// - key: A master read/read-write key for the account, or a permission token for a resource
/// - keyType: The type of key - `.master` read/read-write key or a `.resource` permission token
public func setup (forAccountAt url: URL, withKey key: String, ofType keyType: TokenType) {
return DocumentClient.default.setup (forAccountAt: url, withKey: key, ofType: keyType)
public func configure (forAccountAt url: URL, withKey key: String, ofType keyType: TokenType) {
return DocumentClient.default.configure (forAccountAt: url, withKey: key, ofType: keyType)
}


Expand Down
24 changes: 12 additions & 12 deletions AzureData/Source/DocumentClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ public class DocumentClient {

// MARK: - Setup

public var setup: Bool { return baseUri != nil }
public var isConfigured: Bool { return baseUri != nil }

public func setup (forAccountNamed name: String, withKey key: String, ofType keyType: TokenType) {
public func configure (forAccountNamed name: String, withKey key: String, ofType keyType: TokenType) {
baseUri = ResourceUri(forAccountNamed: name)
tokenProvider = TokenProvider(key: key, keyType: keyType, tokenVersion: "1.0")
}

public func setup (forAccountAt url: URL, withKey key: String, ofType keyType: TokenType) {
public func configure (forAccountAt url: URL, withKey key: String, ofType keyType: TokenType) {
baseUri = ResourceUri(forAccountAt: url)
tokenProvider = TokenProvider(key: key, keyType: keyType, tokenVersion: "1.0")
}
Expand Down Expand Up @@ -768,7 +768,7 @@ public class DocumentClient {
// list
fileprivate func resources<T> (resourceUri: (URL, String)?, callback: @escaping (ListResponse<T>) -> ()) {

guard setup else { callback(ListResponse<T>(DocumentClientError(withKind: .setupError))); return }
guard isConfigured else { callback(ListResponse<T>(DocumentClientError(withKind: .configureError))); return }

let request = dataRequest(T.self, .get, resourceUri: resourceUri!)

Expand All @@ -778,7 +778,7 @@ public class DocumentClient {
// get
fileprivate func resource<T>(resourceUri: (URL, String)?, callback: @escaping (Response<T>) -> ()) {

guard setup else { callback(Response<T>(DocumentClientError(withKind: .setupError))); return }
guard isConfigured else { callback(Response<T>(DocumentClientError(withKind: .configureError))); return }

let request = dataRequest(T.self, .get, resourceUri: resourceUri!)

Expand All @@ -788,7 +788,7 @@ public class DocumentClient {
// refresh
func refresh<T>(_ resource: T, callback: @escaping (Response<T>) -> ()) {

guard setup else { callback(Response(DocumentClientError(withKind: .setupError))); return }
guard isConfigured else { callback(Response(DocumentClientError(withKind: .configureError))); return }

guard !resource.selfLink.isNilOrEmpty && !resource.resourceId.isEmpty else { callback(Response(DocumentClientError(withKind: .incompleteIds))); return }

Expand All @@ -802,7 +802,7 @@ public class DocumentClient {
// delete
fileprivate func delete<T:CodableResource>(_ type: T.Type = T.self, resourceUri: (URL, String)?, callback: @escaping (DataResponse) -> ()) {

guard setup else { callback(DataResponse(DocumentClientError(withKind: .setupError))); return }
guard isConfigured else { callback(DataResponse(DocumentClientError(withKind: .configureError))); return }

let request = dataRequest(T.self, .delete, resourceUri: resourceUri!)

Expand All @@ -811,7 +811,7 @@ public class DocumentClient {

func delete<T:CodableResource>(_ resource: T, callback: @escaping (DataResponse) -> ()) {

guard setup else { callback(DataResponse(DocumentClientError(withKind: .setupError))); return }
guard isConfigured else { callback(DataResponse(DocumentClientError(withKind: .configureError))); return }

guard !resource.selfLink.isNilOrEmpty && !resource.resourceId.isEmpty else { callback(DataResponse(DocumentClientError(withKind: .incompleteIds))); return }

Expand Down Expand Up @@ -844,7 +844,7 @@ public class DocumentClient {
// query
fileprivate func query<T> (_ query: Query, at resourceUri: (URL, String)?, callback: @escaping (ListResponse<T>) -> ()) {

guard setup else { callback(ListResponse<T>(DocumentClientError(withKind: .setupError))); return }
guard isConfigured else { callback(ListResponse<T>(DocumentClientError(withKind: .configureError))); return }

if self.verboseLogging { query.printQuery(); print() }

Expand All @@ -864,7 +864,7 @@ public class DocumentClient {
// execute
fileprivate func execute<T:CodableResource, R: Encodable>(_ type: T.Type, withBody body: R? = nil, resourceUri: (URL, String)?, callback: @escaping (DataResponse) -> ()) {

guard setup else { callback(DataResponse(DocumentClientError(withKind: .setupError))); return }
guard isConfigured else { callback(DataResponse(DocumentClientError(withKind: .configureError))); return }

do {

Expand All @@ -882,7 +882,7 @@ public class DocumentClient {
// create or replace
fileprivate func createOrReplace<T, R:Encodable> (_ body: R, at resourceUri: (URL, String)?, replacing: Bool = false, additionalHeaders: [String:HttpRequestHeader]? = nil, callback: @escaping (Response<T>) -> ()) {

guard setup else { callback(Response<T>(DocumentClientError(withKind: .setupError))); return }
guard isConfigured else { callback(Response<T>(DocumentClientError(withKind: .configureError))); return }

do {

Expand All @@ -900,7 +900,7 @@ public class DocumentClient {

fileprivate func createOrReplace<T> (_ body: Data, at resourceUri: (URL, String)?, replacing: Bool = false, additionalHeaders: [String:HttpRequestHeader]? = nil, callback: @escaping (Response<T>) -> ()) {

guard setup else { callback(Response<T>(DocumentClientError(withKind: .setupError))); return }
guard isConfigured else { callback(Response<T>(DocumentClientError(withKind: .configureError))); return }

var request = dataRequest(T.self, replacing ? .put : .post, resourceUri: resourceUri!, additionalHeaders: additionalHeaders)

Expand Down
4 changes: 2 additions & 2 deletions AzureData/Source/DocumentClientError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public struct DocumentClientError : Error {
public enum ErrorKind {
case unknownError
case internalError
case setupError
case configureError
case invalidId
case incompleteIds
case badRequest
Expand All @@ -33,7 +33,7 @@ public struct DocumentClientError : Error {
switch self {
case .unknownError: return "An unknown error occured."
case .internalError: return "An internal error occured."
case .setupError: return "AzureData is not setup. Must call AzureData.setup() before attempting CRUD operations on resources."
case .configureError: return "AzureData is not configured. Must call AzureData.configure() before attempting CRUD operations on resources."
case .invalidId: return "Cosmos DB Resource IDs must not exceed 255 characters and cannot contain whitespace"
case .incompleteIds: return "This resource is missing the selfLink and/or resourceId properties. Use an override that takes parent resource or ids instead."
default: return ""
Expand Down
2 changes: 1 addition & 1 deletion AzureData/Source/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.5.4</string>
<string>0.5.5</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
9 changes: 5 additions & 4 deletions AzureData/Tests/AzureDataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,19 @@ class AzureDataTests: XCTestCase {
override func setUp() {
super.setUp()

// AzureData.setup(forAccountNamed: "<Database Name>", withKey: "<Database Master Key OR Resource Permission Token>", ofType: "<Master Key or Resource Token>")
// AzureData.configure(forAccountNamed: "<Database Name>", withKey: "<Database Master Key OR Resource Permission Token>", ofType: "<Master Key or Resource Token>")


AzureData.verboseLogging = true

if !AzureData.isSetup() {
if !AzureData.isConfigured() {

let bundle = Bundle(for: type(of: self))

if let accountName = bundle.infoDictionary?["ADDatabaseAccountName"] as? String, accountName != "AZURE_COSMOS_DB_ACCOUNT_NAME",
let accountKey = bundle.infoDictionary?["ADDatabaseAccountKey"] as? String, accountKey != "AZURE_COSMOS_DB_ACCOUNT_Key" {

AzureData.setup(forAccountNamed: accountName, withKey: accountKey, ofType: .master)
AzureData.configure(forAccountNamed: accountName, withKey: accountKey, ofType: .master)
}
}

Expand Down Expand Up @@ -172,7 +173,7 @@ class AzureDataTests: XCTestCase {
}
}

XCTAssert(AzureData.isSetup(), "AzureData setup failed")
XCTAssert(AzureData.isConfigured(), "AzureData configure failed")
}


Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Azure.iOS [![GitHub license](https://img.shields.io/badge/license-MIT-lightgrey.svg)](https://raw.githubusercontent.com/colbylwilliams/Azure.iOS/master/LICENSE.md) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
# Azure.iOS [![GitHub license](https://img.shields.io/badge/license-MIT-lightgrey.svg)](LICENSE) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)


| Target | OS | Status |
Expand All @@ -13,10 +13,10 @@
| AzureData Sample App | watchOS | _coming soon_ |


# Setup
# Configure

```swift
AzureData.setup("cosmosDb name", key: "read-write key", keyType: .master)
AzureData.configure (forAccountNamed: "cosmosDb name", withKey: "read-write key", ofType: .master)
```


Expand Down

0 comments on commit bb89f99

Please sign in to comment.