Skip to content

Commit fa65744

Browse files
committed
Add additional channel action and fix endpoint config.
1 parent b30f806 commit fa65744

File tree

4 files changed

+58
-14
lines changed

4 files changed

+58
-14
lines changed

src/teletube/cli.cr

+6-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ module Teletube
2727
puts @client.get_categories
2828
when "channels"
2929
case context.command
30-
when "show"
31-
puts @client.get_channel
3230
when "create"
3331
puts @client.create_channel
32+
when "show"
33+
puts @client.get_channel
34+
when "update"
35+
puts @client.update_channel
36+
when "destroy"
37+
puts @client.destroy_channel
3438
else
3539
puts @client.get_channels
3640
end

src/teletube/client.cr

+26-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require "json"
2+
13
module Teletube
24
class Client
35
def initialize(config : Teletube::Config, context : Teletube::Context)
@@ -10,10 +12,6 @@ module Teletube
1012
handle_response(@http.get(path: "/api/v1/categories"))
1113
end
1214

13-
def get_channel
14-
handle_response(@http.get(path: "/api/v1/channels/#{@context.params["id"]}"))
15-
end
16-
1715
def get_channels
1816
if @context.params.empty?
1917
handle_response(@http.get(path: "/api/v1/channels"))
@@ -27,6 +25,22 @@ module Teletube
2725
response.body
2826
end
2927

28+
def get_channel
29+
handle_response(@http.get(path: "/api/v1/channels/#{@context.params["id"]}"))
30+
end
31+
32+
def update_channel
33+
handle_response(
34+
@http.patch(path: "/api/v1/channels/#{@context.params["id"]}", params: @context.params)
35+
)
36+
end
37+
38+
def destroy_channel
39+
handle_response(
40+
@http.delete(path: "/api/v1/channels/#{@context.params["id"]}")
41+
)
42+
end
43+
3044
def get_languages
3145
handle_response(@http.get(path: "/api/v1/languages"))
3246
end
@@ -35,9 +49,16 @@ module Teletube
3549
handle_response(@http.get(path: "/api/v1/profiles/me"))
3650
end
3751

52+
def encoded_json(params)
53+
54+
end
55+
3856
def handle_response(response)
39-
if response.status_code == 200
57+
case response.status_code
58+
when 200
4059
response.body
60+
when 404
61+
"Not found"
4162
else
4263
"Failed"
4364
end

src/teletube/http.cr

+11-7
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,11 @@ require "http/client"
55
module Teletube
66
class Http
77
def initialize(config : Teletube::Config)
8-
uri = URI.parse(config.endpoint)
9-
@http = HTTP::Client.new(
10-
host: uri.host || "localhost",
11-
port: uri.port || 500,
12-
tls: uri.scheme == "https"
13-
)
8+
@http = HTTP::Client.new(uri: URI.parse(config.endpoint))
149
@headers = HTTP::Headers.new
1510
@headers["Authorization"] = "Token #{config.token}"
1611
@headers["Accept"] = "application/json,*/*"
12+
@headers["Content-Type"] = "application/json; charset=utf-8"
1713
end
1814

1915
def get(path : String)
@@ -25,7 +21,15 @@ module Teletube
2521
end
2622

2723
def post(path : String, params : Hash(String, String))
28-
@http.post(path: path, headers: @headers, body: HTTP::Params.encode(params))
24+
@http.post(path: path, headers: @headers, body: params.to_json)
25+
end
26+
27+
def patch(path : String, params : Hash(String, String))
28+
@http.patch(path: path, headers: @headers, body: params.to_json)
29+
end
30+
31+
def delete(path : String)
32+
@http.delete(path: path, headers: @headers)
2933
end
3034
end
3135
end

src/teletube/option_parser.cr

+15
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ module Teletube
5858
context.params["id"] = id
5959
end
6060
end
61+
parser.on("update", "Update details for a channel.") do
62+
context.command = "update"
63+
parser.on("--id ID", "The identifier of the channel to update.") do |id|
64+
context.params["id"] = id
65+
end
66+
parser.on("--name NAME", "The name of the channel.") do |name|
67+
context.params["name"] = name
68+
end
69+
end
70+
parser.on("destroy", "Destroy a channel.") do
71+
context.command = "destroy"
72+
parser.on("--id ID", "The identifier of the channel to destroy.") do |id|
73+
context.params["id"] = id
74+
end
75+
end
6176
end
6277

6378
parser.on("languages", "Interact with languages.") do

0 commit comments

Comments
 (0)