Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mownier/pyrobase
An iOS lightweight wrapper for Firebase REST API
https://github.com/mownier/pyrobase
firebase framework ios swift
Last synced: 2 months ago
JSON representation
An iOS lightweight wrapper for Firebase REST API
- Host: GitHub
- URL: https://github.com/mownier/pyrobase
- Owner: mownier
- License: mit
- Created: 2017-06-23T08:34:19.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-04-03T14:01:18.000Z (over 5 years ago)
- Last Synced: 2024-09-28T17:01:40.635Z (3 months ago)
- Topics: firebase, framework, ios, swift
- Language: Swift
- Size: 103 KB
- Stars: 90
- Watchers: 9
- Forks: 13
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Pyrobase
An iOS lightweight wrapper for Firebase REST API. For more Firebase's details, see it [here.](https://firebase.google.com/docs/reference/rest/database/) It is written in Swift and has 100% code coverage.## Installation
### Cocoapods
```shell
$ pod repo update
$ pod init
$ vim Podfile
// Add `pod Pyrobase` in your podfile
$ pod install
```### Carthage
```shell
$ 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
```swift
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
```swift
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
```swift
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
```swift
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
```swift
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
```swift
let baseURL = "https://foo.firebaseio.com"
let accessToken = "accessToken"
let pyrobase = Pyrobase.create(baseURL: baseURL, accessToken: accessToken)
```#### GET Request
```swift
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
```swift
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
```swift
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
```swift
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
```swift
let baseURL = "https://foo.firebaseio.com"
let accessToken = "accessToken"
let transaction = PyroTransaction.create(baseURL: baseURL, accessToken: accessToken)
```
#### Run
```swift
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
```swift
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
```swift
let callback = StreamCallback()
let baseURL = "https://foo.firebaseio.com"
let accessToken = "accessToken"
let eventSource = PyroEventSource.create(baseURL: baseURL, accessToken: accessToken)
eventSource.callback = callback
```#### Stream
```swift
eventSource.stream("chat/rooms/hdjye53910kwdop")
```#### Close
```swift
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.```swift
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