This file contains 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
data class SseModel(val event: String? = null, val data: String = "", val id: String? = null, val retry: Number? = null) { | |
override fun toString(): String { | |
val sseStrings = arrayListOf<String>() | |
if(event != null) sseStrings.add("event: $event") | |
sseStrings.add("data: $data") | |
if(id != null) sseStrings.add("id: $id") | |
if(retry != null) sseStrings.add("retry: $retry") | |
sseStrings.add("\n") |
This file contains 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
class TimeProducer { | |
// create singleton | |
companion object { | |
val instance = TimeProducer() | |
} | |
// create public observable | |
val obs = Observable.interval(1, TimeUnit.SECONDS) | |
.map { LocalDateTime.now() }.share() |
This file contains 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
class TimeHandler : Handler<RoutingContext> { | |
override fun handle(rtx: RoutingContext) { | |
val response = rtx.response() | |
response.setChunked(true) | |
// set headers | |
response.headers().add("Content-Type", "text/event-stream;charset=UTF-8") | |
response.headers().add("Connection", "keep-alive") | |
response.headers().add("Cache-Control", "no-cache") |
This file contains 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
router.route("/time") | |
.handler(TimeHandler()) | |
.failureHandler { | |
it.response().end("time error asynchron response\n") | |
} |
This file contains 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
event: time | |
data: the current time is 2019-07-19T11:15:34.307425 | |
event: time | |
data: the current time is 2019-07-19T11:15:35.307430 | |
event: time | |
data: the current time is 2019-07-19T11:15:36.307562 | |
event: time |
This file contains 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
{ | |
"statusCode": 200, | |
"body": "{\"title\":\"Raspberry Pi 4 Modell B 4GB ARM-Cortex-A72 4X 1,50GHz, 4GB RAM, WLAN-ac, Bluetooth 5, LAN, 4X USB, 2X Micro-HDMI\",\"price\":\"65.90\"}" | |
} |
This file contains 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
serverless invoke local --function price --data '{"queryStringParameters":{"url":"https://www.amazon.de/Raspberry-Pi-ARM-Cortex-A72-Bluetooth-Micro-HDMI/dp/B07TC2BK1X/"}}' |
This file contains 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
const axios = require('axios').default; | |
const JSDOM = require('jsdom').JSDOM; | |
module.exports.price = async event => { | |
if(event.queryStringParameters == undefined || event.queryStringParameters.url == undefined) { | |
return { | |
statusCode: 404, | |
body: JSON.stringify({ | |
message: "You forgot the URL parameter!" | |
}) |
This file contains 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
const axios = require('axios').default; | |
module.exports.price = async event => { | |
var encoded_url = encodeURI("https://www.amazon.de/Raspberry-Pi-ARM-Cortex-A72-Bluetooth-Micro-HDMI/dp/B07TC2BK1X"); | |
const res = await axios.get(encoded_url); | |
return { | |
statusCode: res.status, | |
body: JSON.stringify({ | |
title: "PLACEHOLDER", | |
price: 0.0 |
This file contains 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
const axios = require('axios').default; | |
const JSDOM = require('jsdom').JSDOM; | |
module.exports.price = async event => { | |
var encoded_url = encodeURI("https://www.amazon.de/Raspberry-Pi-ARM-Cortex-A72-Bluetooth-Micro-HDMI/dp/B07TC2BK1X"); | |
const res = await axios.get(encoded_url); | |
const document = new JSDOM(res.data).window.document; | |
const title_with_whitespace = document.querySelector("#productTitle").textContent; |