An open API service indexing awesome lists of open source software.

https://github.com/theapache64/retrosheet

πŸ“ƒ Turn Google Spreadsheet to JSON endpoint. Supported Platforms: Android, iOS, JVM and JS. Thanks to Kotlin Multiplatform πŸ™πŸΌ
https://github.com/theapache64/retrosheet

google-sheets interceptors okhttp rest-api retrofit

Last synced: 7 days ago
JSON representation

πŸ“ƒ Turn Google Spreadsheet to JSON endpoint. Supported Platforms: Android, iOS, JVM and JS. Thanks to Kotlin Multiplatform πŸ™πŸΌ

Awesome Lists containing this project

README

        

# πŸ“ retrosheet
Turn Google Spreadsheet to JSON endpoint.

![https://github.com/theapache64/notes](demo.png)

## 🀝 Benefits

- πŸ”₯ Free analytics via Google forms
- πŸ”„ Migrate to your REST API with minimal code changes.
- πŸ“Š You get a easy to use and real time interface for your data (GoogleSheet) ;)
- πŸƒβ€β™‚οΈ Speed up development of your POC or MVP with this library.

## πŸš€ Platform Supported

![Android](https://img.shields.io/badge/Android-3DDC84?style=for-the-badge&logo=android&logoColor=white) ![iOS](https://img.shields.io/badge/iOS-000000?style=for-the-badge&logo=ios&logoColor=white) ![Java](https://img.shields.io/badge/java-%23ED8B00.svg?style=for-the-badge&logo=openjdk&logoColor=white) ![JavaScript](https://img.shields.io/badge/javascript-%23323330.svg?style=for-the-badge&logo=javascript&logoColor=%23F7DF1E)

## 🀝 Install

![latestVersion](https://img.shields.io/github/v/release/theapache64/retrosheet)

```kotlin
repositories {
mavenCentral()
}

dependencies {
implementation("io.github.theapache64:retrosheet:")
}
```

## ⌘️ Usage

### ✍️ Writing Data

#### πŸ“ Step 1: Create a Google Form
Create a form with required fields.
![Google Form](https://i.imgur.com/9PeK2EQ.png)

#### 🎯 Step 2: Set Response Destination
Choose a Google Sheet to save responses.
![Response Destination](https://i.imgur.com/fIzWiN5.png)
![Sheet Selection](https://i.imgur.com/7ASAB55.png)

#### πŸ“Š Step 3: Customize Sheet
Rename sheet and columns (optional).
![Before](https://i.imgur.com/keT8P1o.png)
![After](https://i.imgur.com/N6xfuZK.png)

#### πŸ”— Step 4: Get Form Link
Press `Send` and copy the link.
![Form Link](https://i.imgur.com/veATAn5.png)

#### πŸ”§ Step 5: Create `RetrosheetConfig` and attach it to the client
```kotlin
val config = RetrosheetConfig.Builder()
.setLogging(true)
// For reading from sheet
.addSheet(
"notes", // sheet name
"created_at", "title", "description" // columns in same order
)
// For writing to sheet
.addForm(
"add_note",
"https://docs.google.com/forms/d/e/1FAIpQLSdmavg6P4eZTmIu-0M7xF_z-qDCHdpGebX8MGL43HSGAXcd3w/viewform?usp=sf_link" // form link
)
.build()

val ktorClient = HttpClient {
install(createRetrosheetPlugin(config)) {}
...
}
```

#### 🌐 Step 6: Create API Interface
```kotlin
interface NotesApi {
@Read("SELECT *")
@GET("notes")
suspend fun getNotes(): List

@Write
@POST("add_note")
suspend fun addNote(@Body note: Note): Note
}
```

> **@Write** is used for writing data and **@Read** for reading data.

[Query Language Guide](https://developers.google.com/chart/interactive/docs/querylanguage)

### πŸ“š Reading Data

#### πŸ”„ Step 7: Share Sheet
Open a sheet and copy its shareable link.
![Copy Link](https://i.imgur.com/MNYD7mg.png)

#### βœ‚οΈ Step 8: Edit Link
Trim the link after the last '/'.

`https://docs.google.com/spreadsheets/d/1IcZTH6-g7cZeht_xr82SHJOuJXD_p55QueMrZcnsAvQ`~~/edit?usp=sharing~~

#### πŸ”— Step 9: Set Base URL
Use the trimmed link as `baseUrl` in `Ktorfit`.

```kotlin
val retrofit = Ktorfit.Builder()
// Like this πŸ‘‡πŸΌ
.baseUrl("https://docs.google.com/spreadsheets/d/1YTWKe7_mzuwl7AO1Es1aCtj5S9buh3vKauKCMjx1j_M/")
.httpClient(ktorClient)
.converterFactories(RetrosheetConverter(config))
.build()
```

**Done πŸ‘**

## 🌠 Full Example

```kotlin
suspend fun main() {
val notesApi = buildNotesApi()
println(notesApi.getNotes())

// Adding sample order
val newNote = notesApi.addNote(
Note(
createdAt = null,
title = "Dynamic Note 1",
description = "DynΓ‘mic Desc 1: ${Date()}"
)
)

println(newNote)
}

fun createNotesApi(
configBuilder: RetrosheetConfig.Builder.() -> Unit = {}
): NotesApi {
val config = RetrosheetConfig.Builder()
.apply { this.configBuilder() }
.setLogging(true)
// To Read
.addSheet(
"notes", // sheet name
"created_at", "title", "description" // columns in same order
)
// To write
.addForm(
"add_note",
// Google form name
"https://docs.google.com/forms/d/e/1FAIpQLSdmavg6P4eZTmIu-0M7xF_z-qDCHdpGebX8MGL43HSGAXcd3w/viewform?usp=sf_link"
)
.build()

val ktorClient = HttpClient {
install(createRetrosheetPlugin(config)) {}
install(ContentNegotiation) {
json()
}
}

val retrofit = Ktorfit.Builder()
// GoogleSheet Public URL
.baseUrl("https://docs.google.com/spreadsheets/d/1YTWKe7_mzuwl7AO1Es1aCtj5S9buh3vKauKCMjx1j_M/")
.httpClient(ktorClient)
.converterFactories(RetrosheetConverter(config))
.build()

return retrofit.createNotesApi()
}
```
- Source: https://github.com/theapache64/retrosheet-jvm-sample. Check `sample` directory for more samples

## πŸ”„ Migration
- Want to migrate from v1 or v2?Here's the [guide](https://github.com/theapache64/retrosheet/blob/master/MIGRATION.md)

## Must Read βœ‹πŸΌ
Retrosheet is great for prototyping and not recommended to be used in production for real apps. This is because the library makes direct calls to Google APIsβ€”so if those APIs go down, your app goes down with them.

That said, I do use it in production for a few of my [side projects](https://github.com/theapache64/stackzy) :P, and it has been working fine for over 5 years now. (So if things break, I’ll be right there, drowning in tears with you.)

## ✍️ Author
- theapache64