Giter Site home page Giter Site logo

wilderminds / retrosheet Goto Github PK

View Code? Open in Web Editor NEW

This project forked from theapache64/retrosheet

0.0 0.0 0.0 18.03 MB

๐Ÿ“ƒ Turn Google Spreadsheet to JSON endpoint (for Android and JVM) for FREE (100%)

License: Apache License 2.0

Kotlin 100.00%

retrosheet's Introduction

retrosheet ๐Ÿ“„

Turn Google Spreadsheet to JSON endpoint. [For Android and JVM].

https://github.com/theapache64/notes

Benefits ๐Ÿค—

  • No worries about server health (because you're using Google's server ๐Ÿ˜‹)
  • Rapid response and unlimited bandwidth
  • You can easily migrate to your REST API without any significant code change.
  • You don't have to create an admin panel/dashboard to control the data. You can simply use Google Spreadsheet app (web/mobile).
  • You can use this library to create POC/MVP instantly

Install ๐Ÿค

latestVersion

repositories {
  maven { url 'https://jitpack.io' } // Add jitpack
}

dependencies {
  implementation 'com.github.theapache64:retrosheet:latest.version'
}

Usage โŒจ๏ธ

How to write data ? โœ๏ธ

Step 1 : Writing Data To Sheet

Step 2

  • Select response destination and select/create a Google sheet to store the responses.

Step 3

  • Now you can open the sheet and change sheet name and column names if you want. This is just to make the Google sheet table look like a real database table (optional)

I've changed to

Step 4

  • Next, Press the Send button and copy the form link

Step 5

  • Now let's go to our code and create our RetrosheetInterceptor
val retrosheetInterceptor = RetrosheetInterceptor.Builder()
    .setLogging(false)
    // To Read
    .addSheet(
        "notes", // sheet name
        "created_at", "title", "description" // columns in same order
    )
    // To write
    .addForm(
        ADD_NOTE_ENDPOINT,
        "https://docs.google.com/forms/d/e/1FAIpQLSdmavg6P4eZTmIu-0M7xF_z-qDCHdpGebX8MGL43HSGAXcd3w/viewform?usp=sf_link" // form link
    )
    .build()

Step 6

  • Next, let's create a normal Retrofit API interface
interface NotesApi {

    @Read("SELECT *") 
    @GET("notes") // sheet name
    suspend fun getNotes(): List<Note>

    @Write
    @POST(ADD_NOTE_ENDPOINT) // form name
    suspend fun addNote(@Body addNoteRequest: AddNoteRequest): AddNoteRequest
}
  • @Write : To write data to a sheet

  • @Read : To read data from a sheet.

You can lean more about query language from here : https://developers.google.com/chart/interactive/docs/querylanguage.

NOTE: You can use your column name in the query rather than using column letter such as A,B,C etc.

How to read data ? ๐Ÿ“–

Step 7 : Reading data from Sheet

  • We're done configuring the writing part. Now let's finish the reading part. Create/open a google sheet, (it can be either form connected, or a simple Google sheet).

  • Press Share and copy the link

copy-link

Step 8

  • Remove contents after the last forward slash from the copied link.

For example, this

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

would become this

https://docs.google.com/spreadsheets/d/1IcZTH6-g7cZeht_xr82SHJOuJXD_p55QueMrZcnsAvQ/

Step 9

  • Finally, Set the Retrofit or OkHttp's baseUrl with the above link.

baseUrl

Done ๐Ÿ‘

Full Example ๐ŸŒŸ

import com.squareup.moshi.Moshi
import com.github.theapache64.retrosheet.RetrosheetInterceptor
import kotlinx.coroutines.runBlocking
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory

/**
 * Created by theapache64 : Jul 21 Tue,2020 @ 02:11
 */
fun main() = runBlocking {
  
    // Building Retrosheet Interceptor
    val retrosheetInterceptor = RetrosheetInterceptor.Builder()
        .setLogging(false)
        // To Read
        .addSheet(
            "notes", // sheet name
            "created_at", "title", "description" // columns in same order
        )
        // To write
        .addForm(
            "add_note",
            "https://docs.google.com/forms/d/e/1FAIpQLSdmavg6P4eZTmIu-0M7xF_z-qDCHdpGebX8MGL43HSGAXcd3w/viewform?usp=sf_link" // form link
        )
        .build()

    // Building OkHttpClient 
    val okHttpClient = OkHttpClient.Builder()
        .addInterceptor(retrosheetInterceptor) // and attaching interceptor
        .build()


    val moshi = Moshi.Builder().build()

    // Building retrofit client
    val retrofit = Retrofit.Builder()
        // with baseUrl as sheet's public URL    
        .baseUrl("https://docs.google.com/spreadsheets/d/1YTWKe7_mzuwl7AO1Es1aCtj5S9buh3vKauKCMjx1j_M/") // Sheet's public URL
        // and attach previously created OkHttpClient
        .client(okHttpClient)
        .addConverterFactory(MoshiConverterFactory.create(moshi))
        .build()

    // Now create the API interface
    val notesApi = retrofit.create(NotesApi::class.java)
  
    // Reading notes
    println(notesApi.getNotes())

    // Adding note
    val addNote = notesApi.addNote(
        AddNoteRequest("Dynamic Note 1", "Dynamic Desc 1")
    )
    println(addNote)
    Unit
}

Samples ๐ŸŒ 

TODO โ˜‘๏ธ

  • Add git hook instruction (ktlint)

Contributing

This project is applying ktlint (without import ordering since it's conflicted with IDE's format). Before creating a PR, please make sure your code is aligned with ktlint (./gradlew ktlint). We can run auto-format with:

./gradlew ktlintFormat

Retrosheet JS

  • Coming Soon

Author โœ๏ธ

  • theapache64

retrosheet's People

Contributors

theapache64 avatar tuanchauict avatar imgbotapp 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.