Skip to content

Instantly share code, notes, and snippets.

View lordcodes's full-sized avatar
⌨️
Coding

Andrew Lord lordcodes

⌨️
Coding
View GitHub Profile
@lordcodes
lordcodes / current-git-branch.gradle
Last active January 11, 2024 03:54
Gradle function to get the current Git branch
def getCurrentGitBranch() {
def gitBranch = "Unknown branch"
try {
def workingDir = new File("${project.projectDir}")
def result = 'git rev-parse --abbrev-ref HEAD'.execute(null, workingDir)
result.waitFor()
if (result.exitValue() == 0) {
gitBranch = result.text.trim()
}
} catch (e) {
@lordcodes
lordcodes / sourcery-generate-app-secrets.sh
Created November 19, 2018 19:59
Read secrets into your iOS project from xcconfig files and then use Sourcery to generate a source file to use them within your code.
#!/bin/bash
# Generate list of arguments to pass to Sourcery
function sourceryArguments {
# Environment variables from BuildConfig to map into AppSecrets
local arguments=(
"CHAT_API_CLIENT_SECRET" "ANALYTICS_WRITE_KEY"
)
local combinedArgs
@lordcodes
lordcodes / explainme.gradle
Created November 2, 2015 21:21
Gradle function to print out the contents of an object
def void explainMe(it) {
println "Examining $it.name:"
println "Meta:"
println it.metaClass.metaMethods*.name.sort().unique()
println "Methods:"
println it.metaClass.methods*.name.sort().unique()
println "Properties:"
println it.properties.entrySet()*.toString()
.sort().toString().replaceAll(", ","\n")
}
@lordcodes
lordcodes / TokenRefreshAuthenticator.kt
Created March 17, 2020 09:31
Code for the article: "Authorization and retrying of web requests for OkHttp and Retrofit"
class TokenRefreshAuthenticator(
private val authorizationRepository: AuthorizationRepository
) : Authenticator {
override fun authenticate(route: Route?, response: Response): Request? = when {
response.retryCount > 2 -> null
else -> response.createSignedRequest()
}
private fun Response.createSignedRequest(): Request? = try {
val accessToken = authenticationRepository.fetchFreshAccessToken()
@lordcodes
lordcodes / AttachmentUploader.kt
Created February 25, 2020 20:21
Code for the article: "Uploading a file with progress in Kotlin"
fun uploadAttachment(
filename: String, file: File, mimeType: String
): Observable<AttachmentUploadRemoteResult> {
val progressEmitter = PublishSubject.create<Double>()
val uploadRequest = createUploadRequest(
filename, file, mimeType, progressEmitter
)
val uploadResult = uploadRequest
.map<AttachmentUploadRemoteResult> {
@lordcodes
lordcodes / app_build.gradle.kts
Created November 28, 2020 01:14
Brief demo of using a platform module to constrain dependency versions
dependencies {
implementation(enforcedPlatform(project(":dependency-constraints")))
implementation("org.jetbrains.kotlin:kotlin-stdlib")
implementation("androidx.lifecycle:lifecycle-livedata-ktx")
implementation("io.coil-kt:coil")
}
@lordcodes
lordcodes / ci-setup.sh
Created January 22, 2019 18:53
Android Circle CI config involving workflows, caching and sharing the workspace between steps in workflow.
#!/usr/bin/env bash
# Accept licenses
${ANDROID_HOME}/tools/bin/sdkmanager --licenses
# Install dependencies
./gradlew androidDependencies || true
@lordcodes
lordcodes / AuthorizationInterceptor.kt
Created March 17, 2020 09:33
Code for the article: "Authorization and retrying of web requests for OkHttp and Retrofit"
class AuthorizationInterceptor(
private val authorizationRepository: AuthorizationRepository
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val newRequest = chain.request().signedRequest()
return chain.proceed(newRequest)
}
private fun Request.signedRequest() = when (AuthorizationType.fromRequest(this)) {
AuthorizationType.ACCESS_TOKEN -> signWithFreshAccessToken()
@lordcodes
lordcodes / CreateAccountRemoteApi.kt
Created March 17, 2020 09:32
Code for the article: "Authorization and retrying of web requests for OkHttp and Retrofit"
interface CreateAccountRemoteApi {
@POST("user/identity")
fun createAccount(
@Body request: CreateAccountRemoteDto,
@Tag authorization: AuthorizationType = AuthorizationType.CLIENT_CREDENTIALS
): Single<AccountCreatedRemoteDto>
}
@lordcodes
lordcodes / AuthorizationType.kt
Created March 17, 2020 09:32
Code for the article: "Authorization and retrying of web requests for OkHttp and Retrofit"
enum class AuthorizationType {
ACCESS_TOKEN,
CLIENT_CREDENTIALS,
NONE;
companion object {
fun fromRequest(request: Request): AuthorizationType =
request.tag(AuthorizationType::class.java) ?: ACCESS_TOKEN
}
}