Skip to content

Instantly share code, notes, and snippets.

@rahul619anand
Last active June 17, 2017 16:25
Show Gist options
  • Save rahul619anand/4df603de9f666f4cf9ce3821dbf3ca00 to your computer and use it in GitHub Desktop.
Save rahul619anand/4df603de9f666f4cf9ce3821dbf3ca00 to your computer and use it in GitHub Desktop.
package com.test.quartz
import java.util
import org.asynchttpclient.{DefaultAsyncHttpClient, DefaultAsyncHttpClientConfig, Param, RequestBuilder}
import org.asynchttpclient.proxy.ProxyServer
import org.asynchttpclient.ws.{WebSocket, WebSocketTextListener, WebSocketUpgradeHandler}
import org.json4s._
import org.json4s.native.JsonMethods._
import org.json4s.native.Serialization
/**
* Created by ranand on 5/24/2017 AD.
*/
class TestAsyncHttpCLient {
def test () = {
implicit val formats = Serialization.formats(NoTypeHints)
val cf = new DefaultAsyncHttpClientConfig.Builder()
.setProxyServer(new ProxyServer.Builder("custom.proxy.local", 3128))
.build
val c = new DefaultAsyncHttpClient(cf)
val rb =
new RequestBuilder("GET").setUrl("https://bornconfused.slack.com/api/api.test")
val response = c.prepareRequest(rb).execute.get
println(" Test Response " + response.getResponseBody)
val list = new util.ArrayList[Param]()
list.add(new Param("token", "your-bot-token"))
val rb2 = new RequestBuilder("POST")
.setUrl("https://your-domain.slack.com/api/rtm.connect")
.setQueryParams(list)
val response2 = c.prepareRequest(rb2).execute.get
println("RTM Connect Response " + response2.getResponseBody)
val jsonObject = parse(response2.getResponseBody).extract[R00tJsonObject]
val url = jsonObject.url
println("Url = " + url)
val websocket = new DefaultAsyncHttpClient(cf)
.prepareGet(url)
.execute(
new WebSocketUpgradeHandler.Builder()
.addWebSocketListener(new WebSocketTextListener() {
var socket : WebSocket =null;
override def onMessage(message: String): Unit = {
println(message)
}
override def onOpen(websocket: WebSocket): Unit = {
socket = websocket
println("opening websocket connection...")
}
override def onClose(websocket: WebSocket): Unit = {
println("closing websocket connection...")
websocket.close()
}
override def onError(t: Throwable): Unit = {
println("error")
t.printStackTrace()
}
})
.build)
.get
// bot channel id - C5K51J8EQ
websocket.sendMessage(
"{" +
" \"id\": 1," +
" \"type\": \"message\", " +
" \"channel\": \"C5K51J8EQ\", " +
" \"text\": \"Testing new client to be used... \" " +
"}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment