summaryrefslogtreecommitdiff
path: root/src/twitch/rest.cr
diff options
context:
space:
mode:
authorJonathan B <greenbigfrog@gmail.com>2019-08-14 21:25:02 +0200
committerJonathan B <greenbigfrog@gmail.com>2019-08-14 21:25:02 +0200
commit99b3d7287bd0ac36ee82129ad38bd88c5b5af625 (patch)
tree461ba1e96425d62a60d4fdb1fd9d892264b146f2 /src/twitch/rest.cr
downloadtwitcr-99b3d7287bd0ac36ee82129ad38bd88c5b5af625.tar.gz
twitcr-99b3d7287bd0ac36ee82129ad38bd88c5b5af625.zip
Initial release
Diffstat (limited to 'src/twitch/rest.cr')
-rw-r--r--src/twitch/rest.cr50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/twitch/rest.cr b/src/twitch/rest.cr
new file mode 100644
index 0000000..1de8d6f
--- /dev/null
+++ b/src/twitch/rest.cr
@@ -0,0 +1,50 @@
+require "http"
+require "json"
+require "./mappings/*"
+
+module Twitcr::REST
+ # Mixin for interacting with Twitch's REST API
+ SSL_CONTEXT = OpenSSL::SSL::Context::Client.new
+ API_BASE = "https://api.twitch.tv/helix"
+
+ EMPTY_RESULT = Exception.new("Empty Result")
+
+ # Executes an HTTP request against the API_BASE url
+ def request(method : String, route : String, version = "5", headers = HTTP::Headers.new, body : String? = nil)
+ headers["Authorization"] = @token
+ headers["Client-ID"] = @client_id
+
+ response = HTTP::Client.exec(
+ method,
+ API_BASE + route,
+ headers,
+ tls: SSL_CONTEXT
+ )
+
+ response.body
+ end
+
+ def get_user_by_login(login : String)
+ response = request(
+ "GET",
+ "/users?login=" + login
+ )
+
+ list = UserList.from_json(response)
+ raise EMPTY_RESULT if list.data.empty?
+
+ list.data.first
+ end
+
+ def get_user_by_id(id : Int64)
+ response = request(
+ "GET",
+ "/users?id=" + id.to_s
+ )
+
+ list = UserList.from_json(response)
+ raise EMPTY_RESULT if list.data.empty?
+
+ list.data.first
+ end
+end