Giter Site home page Giter Site logo

pyrobase's Introduction

Pyrobase

An iOS lightweight wrapper for Firebase REST API. For more Firebase's details, see it here. It is written in Swift and has 100% code coverage.

Installation

Cocoapods

$ pod repo update
$ pod init
$ vim Podfile
// Add `pod Pyrobase` in your podfile
$ pod install

Carthage

$ brew install carthage
$ vim Cartfile
// Add `github "mownier/Pyrobase" ~> 1.0` in your cartfile
$ carthage update

Usage

Authentication

Make sure to copy PyroAuthInfo.plist. And keep in mind the bundle identifier where the said file is added.

Initialization

let apiKey = "yourFirebaseAPIKey"
let bundleIdentifier = "com.my.app"
let auth = PyroAuth.create(key: apiKey, bundleIdentifier: bundleIdentifier)
// The variable 'auth' is nil if you provide an invalid bundle identifier.
// Otherwise, you are good to go.
// NOTE: If you build this project as framework, you can opt out
//       providing the bundle identifier. The default value
//       is the project's bundle identifier

Sign In

auth.signIn(email: email, password: password) { result in
    switch result {
    case .failed(let error):
        print(error)
        // Do some stuff
            
    case .succeeded(let data):
        print(data)
        // Do some stuff
        // 'data' is PyroAuthContent
    }
}

Register

auth.register(email: email, password: password) { result in
    switch result {
    case .failed(let error):
        print(error)
        // Do some stuff
            
    case .succeeded(let data):
        print(data)
        // Do some stuff
        // 'data' is PyroAuthContent
    }
}

Refresh Token

auth.refresh(token: "refreshToken") { result in
    switch result {
    case .failed(let error):
        print(error)
        // Do some stuff
            
    case .succeeded(let data):
        print(data)
        // Do some stuff
        // 'data' is PyroAuthTokenContent
    }
}

Send Password Reset

auth.sendPasswordReset(email: "[email protected]") { result in
    switch result {
    case .failed(let error):
        print(error)
        // Do some stuff
            
    case .succeeded(let data):
        print(data)
        // Do some stuff
        // 'data' is Bool
    }
}

REST

Initialization

let baseURL = "https://foo.firebaseio.com"
let accessToken = "accessToken"
let pyrobase = Pyrobase.create(baseURL: baseURL, accessToken: accessToken)

GET Request

pyrobase.get(path: "users/abcde12345wert", query: [:]) { result in
    switch result {
    case .failed(let error):
        print(error)
        // Do some stuff
            
    case .succeeded(let data):
        print(data)
        // Do some stuff
    }
}

POST Request

pyrobase.post(path: "messages", value: ["message": "hello world", "user_id": "abcde12345qwert"]) { result in
    switch result {
    case .failed(let error):
        print(error)
        // Do some stuff
            
    case .succeeded(let data):
        print(data)
        // Do some stuff
    }
}

PUT Request

pyrobase.put(path: "users/abcde12345wert", value: ["first_name": "Juan", "last_name": "Dela Cruz"]) { result in
    switch result {
    case .failed(let error):
        print(error)
        // Do some stuff
            
    case .succeeded(let data):
        print(data)
        // Do some stuff
    }
}

PATCH Request

pyrobase.patch(path: "users/abcde12345wert", value: ["first_name": "Jose"]) { result in
    switch result {
    case .failed(let error):
        print(error)
        // Do some stuff
            
    case .succeeded(let data):
        print(data)
        // Do some stuff
    }
}

Transaction

Initialization

let baseURL = "https://foo.firebaseio.com"
let accessToken = "accessToken"
let transaction = PyroTransaction.create(baseURL: baseURL, accessToken: accessToken)

Run

transaction.run(
    parentPath: "posts/yuiop98765nbcwe",
    childKey: "likes_count", 
    mutator: { data in
    	let likesCount = data as! Int
    	return likesCount + 1
}) { result in
    switch result {
    case .failed(let error):
        print(error)
        // Do some stuff
            
    case .succeeded(let data):
        print(data)
        // Do some stuff
    }
}

Event Source

Callback

class StreamCallback: PyroEventSourceCallback {

    func pyroEventSource(_ eventSource: PyroEventSource, didReceiveError error: Error) {
        // Do some stuff
    }
    
     func pyroEventSource(_ eventSource: PyroEventSource, didReceiveMessage message: PyroEventSourceMessage) {
        // Do some stuff
    }

    func pyroEventSourceOnOpen(_ eventSource: PyroEventSource) {
        // Do some stuff
    }
    
    func pyroEventSourceOnClosed(_ eventSource: PyroEventSource) {
        // Do some stuff
    }
    
    func pyroEventSourceOnConnecting(_ eventSource: PyroEventSource) {
        // Do some stuff
    }
}

Initialization

let callback = StreamCallback()
let baseURL = "https://foo.firebaseio.com"
let accessToken = "accessToken"
let eventSource = PyroEventSource.create(baseURL: baseURL, accessToken: accessToken)
eventSource.callback = callback

Stream

eventSource.stream("chat/rooms/hdjye53910kwdop")

Close

eventSource.close()

Query Parameters

Always keep in mind of adding .indexOn in your rules for the path you want to query. You may receive a badRequest error if you don't set it.

let query = ["orderBy": "\"$key\"", "limitToFirst": 1]
pyrobase.get("posts", query: query) { result in
   switch result {
    case .failed(let error):
        print(error)
        // Do some stuff
            
    case .succeeded(let data):
        print(data)
        // Do some stuff
    }
}

License

MIT License

pyrobase's People

Contributors

mownier avatar

Watchers

James Cloos avatar

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.