Created
April 6, 2021 14:39
-
-
Save amir-dropit/181fb42680fa7fb064ef206a8d4cf197 to your computer and use it in GitHub Desktop.
Airtable wrapper https://github.com/roisagiv/WeightTracker-Android/tree/master/app/src/main/java/io/roisagiv/github/weighttracker/api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.roisagiv.github.weighttracker.api | |
import okhttp3.OkHttpClient | |
import okhttp3.logging.HttpLoggingInterceptor | |
import retrofit2.Response | |
import retrofit2.Retrofit | |
import retrofit2.converter.gson.GsonConverterFactory | |
import retrofit2.create | |
import retrofit2.http.Body | |
import retrofit2.http.GET | |
import retrofit2.http.POST | |
interface AirtableAPI { | |
@GET("./") | |
suspend fun records(): Response<RecordsApiResponse> | |
@POST("./") | |
suspend fun create(@Body fields: CreateRecordBody): Response<AirtableRecord> | |
companion object { | |
fun build(baseUrl: String, apiKey: String): AirtableAPI { | |
val loggingInterceptor = HttpLoggingInterceptor() | |
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY | |
val okHttpClient = OkHttpClient.Builder() | |
.addInterceptor(ApiKeyInterceptor(apiKey)) | |
.addInterceptor(loggingInterceptor) | |
.build() | |
return Retrofit.Builder() | |
.baseUrl(baseUrl) | |
.client(okHttpClient) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build() | |
.create() | |
} | |
fun build( | |
baseUrl: String, | |
okHttpClient: OkHttpClient | |
): AirtableAPI { | |
return Retrofit.Builder() | |
.baseUrl(baseUrl) | |
.client(okHttpClient) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build() | |
.create() | |
} | |
} | |
} | |
data class RecordsApiResponse(val records: List<AirtableRecord>) | |
data class AirtableRecord(val id: String, val fields: Map<String, String>) | |
data class CreateRecordBody(val fields: Map<String, Any>) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.roisagiv.github.weighttracker.api | |
import okhttp3.Interceptor | |
import okhttp3.Response | |
class ApiKeyInterceptor(private val apiKey: String) : Interceptor { | |
override fun intercept(chain: Interceptor.Chain): Response { | |
val original = chain.request() | |
val request = original.newBuilder() | |
.addHeader("Authorization", "Bearer $apiKey") | |
.build() | |
return chain.proceed(request) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment