Skip to content

Instantly share code, notes, and snippets.

View daviddenton's full-sized avatar

David Denton daviddenton

View GitHub Profile
@daviddenton
daviddenton / AdjustableTickingClock.kt
Created June 21, 2023 09:22
AdjustableTickingClock
class AdjustableTickingClock(
private val startTime: Instant = EPOCH,
private val tick: Duration = Duration.ofSeconds(5)
) : Clock() {
private var currentTime = startTime
override fun getZone() = ZoneId.of("UTC")
override fun withZone(zone: ZoneId?) = this
override fun instant() = currentTime.also { currentTime += tick }
@daviddenton
daviddenton / SQSToLambdaBridge.kt
Last active August 30, 2021 15:01
Bridging an SQS queue to an AWS Request SQS Event Lambda
package org.http4k.example
import com.amazonaws.services.lambda.runtime.Context
import com.amazonaws.services.lambda.runtime.events.SQSEvent
import org.http4k.aws.AwsSdkClient
import org.http4k.client.JavaHttpClient
import org.http4k.core.HttpHandler
import org.http4k.core.Uri
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider.create
@daviddenton
daviddenton / AzureSdkHttpClient.kt
Last active May 14, 2021 13:21
http4k AzureSdkHttpClient adapter
import com.azure.core.http.HttpClient
import com.azure.core.http.HttpHeader
import com.azure.core.http.HttpHeaders
import com.azure.core.http.HttpRequest
import com.azure.core.http.HttpResponse
import org.http4k.core.Body
import org.http4k.core.Body.Companion.EMPTY
import org.http4k.core.HttpHandler
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.contract.PreFlightExtraction
import org.http4k.contract.contract
import org.http4k.contract.meta
import org.http4k.core.Body
import org.http4k.core.Filter
import org.http4k.core.Method.POST
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status.Companion.BAD_REQUEST
import org.http4k.core.Status.Companion.OK
@daviddenton
daviddenton / main.kt
Created October 16, 2020 17:09
Composing multiple http4k apps
val app1 = routes(
"/bob" bind GET to { Response(OK).body("you GET bob") },
"/rita" bind POST to { Response(CREATED).body("you POST rita") }
)
val app2 = routes(
"/sue" bind DELETE to { Response(ACCEPTED).body("you DELETE sue") }
)
// we can compose the apps together into one large app here at the root,
### Keybase proof
I hereby claim:
* I am daviddenton on github.
* I am daviddenton (https://keybase.io/daviddenton) on keybase.
* I have a public key ASBCXxFMUhDpkU0nuStiO7UXK879HTLE5CitEDuRXB9oyQo
To claim this, I am signing this object:
@daviddenton
daviddenton / Main.kt
Last active June 23, 2020 20:32
Bunting - A nano command-line flags library extracted from http4k
class MyGreatFlags(args: Array<String>) : Bunting(args) {
val user by requiredFlag()
val password by requiredFlag()
val version by defaultedFlag("0.0.0")
}
// run the main with: java (...) Mainkt --user foo --password bar
fun main(args: Array<String>) = MyGreatFlags(args).use {
println(user) // foo
println(password) // bar
@daviddenton
daviddenton / main.kt
Created May 18, 2020 08:15
Customising CatchLensFailure
import org.http4k.core.Body
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status
import org.http4k.core.then
import org.http4k.filter.DebuggingFilters
import org.http4k.filter.ServerFilters
import org.http4k.format.Jackson.auto
import org.http4k.core.HttpHandler
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status
import org.http4k.core.Uri
import org.http4k.core.then
import org.http4k.events.AutoJsonEvents
import org.http4k.events.Event
import org.http4k.events.EventFilters
@daviddenton
daviddenton / main.kt
Last active January 20, 2019 15:21
validating primitive values with lenses
fun main(args: Array<String>) {
data class ValidatedNumber(val value: Long) {
init {
if (value < 0 || value > 100) throw IllegalArgumentException("Out of range")
}
}
val lens = Query.long().map(::ValidatedNumber).required("myLong")