Git Product home page Git Product logo

swiftairtable's Introduction

Update

For a more modern implementation, see AirtableKit

SwiftAirtable

Alt Text

Description

An unofficial Swift interface to Airtable's REST API

Usage

In order to make use of the Framework, simply create a strucuture

struct AirtablePerson {
    // The airtable object id
    var id: String = ""
    var name: String = ""
}

and make it adopt the AirtableObject protocol

extension AirtablePerson: AirtableObject {
    static var fieldKeys: [(fieldName: String, fieldType: AirtableTableSchemaFieldKey.KeyType)] {
        var fields = [(fieldName: String, fieldType: AirtableTableSchemaFieldKey.KeyType)]()
        fields.append((fieldName: AirtableField.name.rawValue, fieldType: .singleLineText))
        return fields
    }
    
    func value(forKey key: AirtableTableSchemaFieldKey) -> AirtableValue? {
        switch key {
        case AirtableTableSchemaFieldKey(fieldName: AirtableField.name.rawValue, fieldType: .singleLineText): return self.name
        default: return nil
        }
    }
    
    init(withId id: String, populatedTableSchemaKeys tableSchemaKeys: [AirtableTableSchemaFieldKey : AirtableValue]) {
        self.id = id
        tableSchemaKeys.forEach { element in
            switch element.key {
            case AirtableTableSchemaFieldKey(fieldName: AirtableField.name.rawValue, fieldType: .singleLineText): self.name = element.value.stringValue
            default: break
            }
        }
    }
}

Finally create an Airtable to perform the operations

let airtable = Airtable(apiKey: apiKey, apiBaseUrl: apiBaseUrl, schema: AirtablePerson.schema)

You can perform all standard CRUD operations

Create

airtable.createObject(with: object, inTable: table) { object: AirtablePerson, error: Error? in
    if let error = error {
        // Error Code
    } else {
        // Success Code
    }
}

Retrieve

airtable.fetchAll(table: table) { (objects: [AirtablePerson], error: Error?) in
    if let error = error {
        // Error Code
    } else {
        // Success Code
    }
}

Update

airtable.updateObject(with: object, inTable: table) { object: AirtablePerson?, error: Error? in
    if let error = error {
        // Error Code
    } else {
        // Success Code
    }
}

Delete

airtable.deleteObject(with: object, inTable: table) { sucess, error in
    if let error = error {
        // Error Code
    } else {
        // Success Code
    }
}

Example

To run the example project, clone the repo, and run pod install from the Example directory first. You'll need to provide your API Key and Base URL.

Installation

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

pod 'SwiftAirtable'

Author

Nicolas Nascimento, [email protected]

Sourcery

An stencil file is provided in the repo that allows you generate code for the AirtableObject protocol using Sourcery (https://github.com/krzysztofzablocki/Sourcery)

License

SwiftAirtable is available under the MIT license. See the LICENSE file for more info.

swiftairtable's People

Contributors

nicolasnascimento avatar tim289 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

swiftairtable's Issues

Expanding example code

In the example project, there is only one record used locally.

/// Updates local state using the provided Record
/// - Parameter record: The record from which to get the data
   private func update(with record: AirtableKit.Record) {
       self.record = record
       self.state.name = record.name ?? ""
       self.state.age = record.age ?? 0
       self.state.isCool = record.isCool ?? false
       self.state.createdTime = record.createdTime ?? Date()
       self.state.updatedTime = record.updatedTime ?? Date()
       self.state.imageUrl = record.attachments["image"]?.first?.url ?? URL(string: AppState.placeholderStringUrl)!

I have struggled to figure out how to access a table that may contain multiple records to use locally as an array of records? So the entire table data could be displayed as a list? If given some guidance, I'd happily contribute expanded example code to show how this could be done. Thanks!

Authentication required

} else if let result = jsonValue {

I used an wrong API Key but no error was indicated, and the server refused the request.

Printing description of result:
▿ 1 element
▿ 0 : 2 elements
- key : "error"
▿ value : 2 elements
▿ 0 : 2 elements
- key : type
- value : AUTHENTICATION_REQUIRED
▿ 1 : 2 elements
- key : message
- value : Authentication required

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.