Skip to content

Commit

Permalink
Move torrent client to the separate service
Browse files Browse the repository at this point in the history
  • Loading branch information
galushko committed Oct 18, 2023
1 parent bf1e421 commit fb5749a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 40 deletions.
45 changes: 45 additions & 0 deletions core/src/main/java/com/halushko/kinocat/core/web/ApiResponce.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.halushko.kinocat.core.web;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

@Slf4j
public class ApiResponce {
private final HttpResponse httpResponse;
public ApiResponce(HttpResponse httpResponse) {
if (httpResponse == null) {
log.debug("[ApiResponce] Responce is null");
}
this.httpResponse = httpResponse;
}

public String getHeader(String headerKey) {
if (httpResponse == null) {
log.debug("[getHeader] Responce is null. Can't get header [{}]", headerKey);
return "";
}
Header header = httpResponse.getFirstHeader(headerKey);
String value = header.getValue();
log.debug("[getHeader] Responce header {}=[{}]", headerKey, value);
return value;
}

public String getBody() {
if (httpResponse == null) {
log.debug("[getBody] Responce is null. Can't get body");
return "";
}
try {
String body = EntityUtils.toString(httpResponse.getEntity());
log.debug("[getBody] Responce body=[{}]", body);
return body;
} catch (IOException e) {
log.error("[getBody] Can't get body from responce", e);
}
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import com.halushko.kinocat.core.handlers.input.InputMessageHandler;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.LinkedHashMap;
Expand All @@ -19,14 +17,17 @@

@SuppressWarnings("unused")
@Slf4j
public abstract class WebSender extends InputMessageHandler {
public abstract class InputMessageHandlerApiRequest extends InputMessageHandler {
private final String serverUrl;

public WebSender(String protocol, String ip, int port, String suffix) {
public InputMessageHandlerApiRequest(String protocol, String ip, int port, String suffix) {
this.serverUrl = String.format("%s://%s:%s/%s", protocol, ip, port, suffix);
}

protected HttpResponse request(String body, Map<String, String> headers) {
protected ApiResponce send(String body, Map<String, String> headers) {
return new ApiResponce(requestPrivate(body, headers));
}
private HttpResponse requestPrivate(String body, Map<String, String> headers) {
log.debug("[request] headers=[{}], body=[{}]", headers, body);
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost postRequest = new HttpPost(serverUrl);
Expand All @@ -45,38 +46,11 @@ protected HttpResponse request(String body, Map<String, String> headers) {
return null;
}

protected HttpResponse request(String body, String... headers) {
protected ApiResponce send(String body, String... headers) {
val headerMap = IntStream.iterate(0, i -> i < headers.length, i -> i + 2).
filter(i -> i + 1 < headers.length).
boxed().
collect(Collectors.toMap(i -> headers[i], i -> headers[i + 1], (a, b) -> b, LinkedHashMap::new));
return request(body, headerMap);
}


protected String getResponceHeader(HttpResponse response, String headerKey) {
if (response == null) {
log.debug("[getResponceHeader] Responce is null. Can't get header [{}]", headerKey);
return "";
}
Header header = response.getFirstHeader(headerKey);
String value = header.getValue();
log.debug("[getResponceHeader] Responce header {}=[{}]", headerKey, value);
return value;
}

protected String getResponceBody(HttpResponse response) {
if (response == null) {
log.debug("[getResponceHeader] Responce is null. Can't get body");
return "";
}
try {
String body = EntityUtils.toString(response.getEntity());
log.debug("[getResponceBody] Responce body=[{}]", body);
return body;
} catch (IOException e) {
log.error("[getResponceBody] Can't get body from responce", e);
}
return "";
return send(body, headerMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.halushko.kinocat.core.files.ResourceReader;
import com.halushko.kinocat.core.rabbit.SmartJson;
import com.halushko.kinocat.core.rabbit.RabbitUtils;
import com.halushko.kinocat.core.web.WebSender;
import com.halushko.kinocat.core.web.InputMessageHandlerApiRequest;
import lombok.extern.slf4j.Slf4j;
import lombok.val;

Expand All @@ -13,7 +13,7 @@

@Slf4j
// https://github.com/transmission/transmission/blob/main/docs/rpc-spec.md
public class TorrentsList extends WebSender {
public class TorrentsList extends InputMessageHandlerApiRequest {
public TorrentsList() {
super("http", "10.10.255.253", 9091, "transmission/rpc");
}
Expand All @@ -23,12 +23,12 @@ protected void getDeliverCallbackPrivate(SmartJson message) {
long chatId = message.getUserId();

String requestBody = ResourceReader.readResourceContent("get_torrents_list.json");
val responce = request("", "Content-Type", "application/json");
val responce = send("", "Content-Type", "application/json");
String sessionIdKey = "X-Transmission-Session-Id";
String sessionIdValue = getResponceHeader(responce, sessionIdKey);
String sessionIdValue = responce.getHeader(sessionIdKey);

val responce2 = request(requestBody, "Content-Type", "application/json", sessionIdKey, sessionIdValue);
String bodyJson = getResponceBody(responce2);
val responce2 = send(requestBody, "Content-Type", "application/json", sessionIdKey, sessionIdValue);
String bodyJson = responce2.getBody();

val json = new SmartJson(bodyJson);
StringBuilder sb = new StringBuilder();
Expand Down

0 comments on commit fb5749a

Please sign in to comment.