Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit f1d0529

Browse files
committed
Updated requirement library changes to new framework version.
1 parent fd63da1 commit f1d0529

File tree

6 files changed

+146
-127
lines changed

6 files changed

+146
-127
lines changed

src/main/scala/org/codeoverflow/chatoverflow/framework/StringMappingActor.scala

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/main/scala/org/codeoverflow/chatoverflow/framework/HttpClientActor.scala renamed to src/main/scala/org/codeoverflow/chatoverflow/framework/actors/HttpClientActor.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
package org.codeoverflow.chatoverflow.framework
1+
package org.codeoverflow.chatoverflow.framework.actors
22

3+
import akka.actor.Actor
34
import org.apache.http.client.methods.HttpGet
45
import org.apache.http.impl.client.HttpClientBuilder
56

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.codeoverflow.chatoverflow.framework.actors
2+
3+
import akka.actor.Actor
4+
5+
case class Mapping(mapFunc: String => Any, content: String)
6+
7+
class StringMappingActor extends Actor {
8+
9+
override def receive: Receive = {
10+
case m: Mapping => sender ! m.mapFunc(m.content)
11+
}
12+
}
Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,111 @@
11
package org.codeoverflow.chatoverflow.requirement.service.twitch.api
22

3-
class TwitchAPIConnector {
3+
import akka.actor.{ActorRef, ActorSystem, Props}
4+
import akka.pattern.ask
5+
import akka.util.Timeout
6+
import org.apache.http.HttpEntity
7+
import org.apache.http.client.methods.HttpGet
8+
import org.apache.http.client.utils.URIBuilder
9+
import org.apache.http.util.EntityUtils
10+
import org.codeoverflow.chatoverflow.WithLogger
11+
import org.codeoverflow.chatoverflow.connector.Connector
12+
import org.codeoverflow.chatoverflow.framework.actors.HttpClientActor
413

14+
import scala.concurrent.Await
15+
import scala.concurrent.duration._
16+
17+
// FIXME: Chery picked from Class Library Rework, should be reworked, lol
18+
19+
/**
20+
* The twitch api connector
21+
*
22+
* @param sourceIdentifier the name to the twitch account
23+
*/
24+
class TwitchAPIConnector(override val sourceIdentifier: String) extends Connector(sourceIdentifier) with WithLogger {
25+
private val API_FORMAT: String = "application/vnd.twitchtv.v5+json"
26+
private val BASE_URL: String = "https://api.twitch.tv/helix/"
27+
private val BASE_URL_v5: String = "https://api.twitch.tv/kraken/"
28+
private val actorSystem = ActorSystem("TwitchAPIActorSystem")
29+
private val actor: ActorRef = actorSystem.actorOf(Props[HttpClientActor])
30+
private var clientID = ""
31+
private var oauth = ""
32+
requiredCredentialKeys = List(TwitchAPIConnector.credentialsClientID, TwitchAPIConnector.credentialsOauthKey)
33+
34+
override def getUniqueTypeString: String = this.getClass.getName
35+
36+
/**
37+
* Returns true, if the connector has been already instantiated and is running.
38+
*/
39+
override def isRunning: Boolean = true
40+
41+
/**
42+
* Initializes the connector, e.g. creates a connection with its platform.
43+
*/
44+
override def init(): Boolean = {
45+
val oauth = credentials.get.getValue(TwitchAPIConnector.credentialsOauthKey)
46+
val clientID = credentials.get.getValue(TwitchAPIConnector.credentialsClientID)
47+
48+
if (clientID.isEmpty) {
49+
logger warn s"key '${TwitchAPIConnector.credentialsClientID}' not found in credentials for '$sourceIdentifier'."
50+
false
51+
} else {
52+
this.clientID = clientID.get
53+
if (oauth.isEmpty) {
54+
logger warn s"key '${TwitchAPIConnector.credentialsOauthKey}' not found in credentials for '$sourceIdentifier'."
55+
false
56+
} else {
57+
this.oauth = oauth.get
58+
true
59+
}
60+
}
61+
}
62+
63+
def getSubscriptions(channelID: String, offset: Int = 0, newestFirst: Boolean = true): String = {
64+
get("channels/" + channelID + "/subscriptions", auth = true, oldAPI = true, Seq(("limit", "100"), ("offset", String.valueOf(offset)), ("direction", if (newestFirst) "desc" else "asc")))
65+
}
66+
67+
def getUser(userLogin: String): String = {
68+
get("users", auth = false, oldAPI = false, Seq(("login", userLogin)))
69+
}
70+
71+
def get(uri: String, auth: Boolean, oldAPI: Boolean, queryParams: Seq[(String, String)]): String = {
72+
val httpGet = if (auth) getUrlAuth(uri, oldAPI) else getURL(uri, oldAPI)
73+
val urlBuilder = new URIBuilder(httpGet.getURI)
74+
queryParams.foreach(param => urlBuilder.addParameter(param._1, param._2))
75+
httpGet.setURI(urlBuilder.build())
76+
implicit val timeout: Timeout = Timeout(5 seconds)
77+
val entity = Await.result(actor ? httpGet, timeout.duration).asInstanceOf[HttpEntity]
78+
if (entity != null) {
79+
EntityUtils.toString(entity, "UTF-8")
80+
}
81+
else ""
82+
}
83+
84+
def getUrlAuth(uri: String, oldAPI: Boolean): HttpGet = {
85+
val get = getURL(uri, oldAPI)
86+
get.setHeader("Authorization", oauth)
87+
get
88+
}
89+
90+
def getURL(uri: String, oldAPI: Boolean): HttpGet = {
91+
val baseUrl = if (oldAPI) BASE_URL_v5 else BASE_URL
92+
new HttpGet(baseUrl + uri) {
93+
setHeader("Accept", API_FORMAT)
94+
setHeader("Client-ID", clientID)
95+
}
96+
}
97+
98+
def getFollowers(userID: String): String = {
99+
get("users/follows", auth = false, oldAPI = false, Seq(("to_id", userID)))
100+
}
101+
102+
/**
103+
* Shuts down the connector, closes its platform connection.
104+
*/
105+
override def shutdown(): Unit = ???
5106
}
107+
108+
object TwitchAPIConnector {
109+
val credentialsOauthKey = "oauth"
110+
val credentialsClientID = "clientid"
111+
}
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,43 @@
1-
package org.codeoverflow.chatoverflow.service.twitch.stat.impl
1+
package org.codeoverflow.chatoverflow.requirement.service.twitch.api.impl
22

3-
import com.fasterxml.jackson.databind.ObjectMapper
4-
import org.codeoverflow.chatoverflow.framework.{Mapping, StringMappingActor}
5-
import org.codeoverflow.chatoverflow.service.twitch.TwitchAPIConnector
3+
import akka.actor.{ActorRef, ActorSystem, Props}
4+
import akka.pattern.ask
5+
import akka.util.Timeout
6+
import org.codeoverflow.chatoverflow.api.io.input.chat.User
7+
import org.codeoverflow.chatoverflow.api.io.input.stat.TwitchStatInput
8+
import org.codeoverflow.chatoverflow.framework.actors.{Mapping, StringMappingActor}
9+
import org.codeoverflow.chatoverflow.registry.Impl
10+
import org.codeoverflow.chatoverflow.requirement.Connection
11+
import org.codeoverflow.chatoverflow.requirement.service.twitch.api.TwitchAPIConnector
612

713
import scala.concurrent.Await
814
import scala.concurrent.duration._
915

16+
// FIXME: Chery picked from Class Library Rework, should be reworked, lol
17+
1018
case class UserResult(data: Seq[UserEntity])
1119

1220
case class UserEntity(id: String, login: String, display_name: String, `type`: String, broadcaster_type: String, description: String, profile_image_url: String, offline_image_url: String, view_count: Int)
1321

22+
@Impl(impl = classOf[TwitchStatInput], connector = classOf[TwitchAPIConnector])
1423
class TwitchStatInputImpl extends Connection[TwitchAPIConnector] with TwitchStatInput {
1524
private val actorSystem = ActorSystem("TwitchAPIActorSystem")
1625
private val actor: ActorRef = actorSystem.actorOf(Props[StringMappingActor])
1726
implicit val timeout: Timeout = Timeout(5 seconds)
1827

1928
override def init(): Unit = {
20-
sourceConnector.init()
29+
sourceConnector.get.init()
2130
}
2231

2332
override def getFollowers(userName: String): java.util.List[User] = {
2433
val userID = getUser(userName).getId
25-
val response = sourceConnector.getFollowers(userID)
34+
val response = sourceConnector.get.getFollowers(userID)
2635
println(response)
2736
null
2837
}
2938

3039
override def getUser(userName: String): User = {
31-
val response = sourceConnector.getUser(userName)
40+
val response = sourceConnector.get.getUser(userName)
3241
println(response)
3342
val result = Await.result(actor ? Mapping(map[UserResult], response), timeout.duration).asInstanceOf[UserResult]
3443
if (result.data.nonEmpty) {
@@ -38,14 +47,19 @@ class TwitchStatInputImpl extends Connection[TwitchAPIConnector] with TwitchStat
3847
else null
3948
}
4049

50+
// FIXME: Kicked jackson mapping (deprecated?!), different way needed
4151
def map[T: Manifest](content: String): Any = {
42-
val mapper = new ObjectMapper() with ScalaObjectMapper
43-
mapper.registerModule(DefaultScalaModule)
44-
mapper.readValue[T](content)
52+
//val mapper = new ObjectMapper() with ScalaObjectMapper
53+
// mapper.registerModule(DefaultScalaModule)
54+
//mapper.readValue[T](content)
4555
}
4656

4757
override def getSubscribers(userName: String): String = {
4858
val userID = getUser(userName).getId
49-
sourceConnector.getSubscriptions(userID)
59+
sourceConnector.get.getSubscriptions(userID)
5060
}
61+
62+
override def serialize(): String = ???
63+
64+
override def deserialize(value: String): Unit = ???
5165
}

src/main/scala/org/codeoverflow/chatoverflow/service/twitch/TwitchAPIConnector.scala

Lines changed: 0 additions & 104 deletions
This file was deleted.

0 commit comments

Comments
 (0)