Skip to content

Commit c1c0486

Browse files
committed
dto response
1 parent cb0f358 commit c1c0486

File tree

8 files changed

+341
-0
lines changed

8 files changed

+341
-0
lines changed

src/main/java/com/temnenkov/mz/ports/tg/TgInbound.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.temnenkov.mz.Main;
44
import com.temnenkov.mz.ports.tg.dto.GetUpdatesRequest;
5+
import com.temnenkov.mz.ports.tg.dto.GetUpdatesResponse;
56
import com.temnenkov.mz.utils.JsonUtils;
67
import org.jetbrains.annotations.Nullable;
78
import org.slf4j.Logger;
@@ -51,6 +52,10 @@ public void loop() throws IOException, InterruptedException {
5152
final HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
5253
final String body = httpResponse.body();
5354
logger.info("Received {} {}", httpResponse.statusCode(), body);
55+
if (httpResponse.statusCode() == 200) {
56+
final GetUpdatesResponse getUpdatesResponse = JsonUtils.jsonToObject(body, GetUpdatesResponse.class);
57+
logger.info("Parsed to {}", getUpdatesResponse);
58+
}
5459
}
5560

5661
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.temnenkov.mz.ports.tg.dto;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
public class Chat {
6+
@SerializedName("id")
7+
private long id;
8+
@SerializedName("first_name")
9+
private String firstName;
10+
@SerializedName("last_name")
11+
private String lastName;
12+
@SerializedName("username")
13+
private String username;
14+
@SerializedName("type")
15+
private String type;
16+
17+
public long getId() {
18+
return id;
19+
}
20+
21+
public void setId(long id) {
22+
this.id = id;
23+
}
24+
25+
public String getFirstName() {
26+
return firstName;
27+
}
28+
29+
public void setFirstName(String firstName) {
30+
this.firstName = firstName;
31+
}
32+
33+
public String getLastName() {
34+
return lastName;
35+
}
36+
37+
public void setLastName(String lastName) {
38+
this.lastName = lastName;
39+
}
40+
41+
public String getUsername() {
42+
return username;
43+
}
44+
45+
public void setUsername(String username) {
46+
this.username = username;
47+
}
48+
49+
public String getType() {
50+
return type;
51+
}
52+
53+
public void setType(String type) {
54+
this.type = type;
55+
}
56+
57+
@Override
58+
public String toString() {
59+
return "Chat{" + "id=" + id + ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", " +
60+
"username='" + username + '\'' + ", type='" + type + '\'' + '}';
61+
}
62+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.temnenkov.mz.ports.tg.dto;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
public class Entity {
6+
@SerializedName("offset")
7+
private int offset;
8+
9+
@SerializedName("length")
10+
private int length;
11+
@SerializedName("type")
12+
private String type;
13+
14+
public String getType() {
15+
return type;
16+
}
17+
18+
public void setType(String type) {
19+
this.type = type;
20+
}
21+
22+
public int getLength() {
23+
return length;
24+
}
25+
26+
public void setLength(int length) {
27+
this.length = length;
28+
}
29+
30+
public int getOffset() {
31+
return offset;
32+
}
33+
34+
public void setOffset(int offset) {
35+
this.offset = offset;
36+
}
37+
38+
@Override
39+
public String toString() {
40+
return "Entity{" + "offset=" + offset + ", length=" + length + ", type='" + type + '\'' + '}';
41+
}
42+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.temnenkov.mz.ports.tg.dto;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
import java.util.List;
6+
7+
public class GetUpdatesResponse {
8+
9+
@SerializedName("ok")
10+
private boolean ok;
11+
12+
@SerializedName("result")
13+
private List<MessageUpdate> result;
14+
15+
public boolean isOk() {
16+
return ok;
17+
}
18+
19+
public void setOk(boolean ok) {
20+
this.ok = ok;
21+
}
22+
23+
public List<MessageUpdate> getResult() {
24+
return result;
25+
}
26+
27+
public void setResult(List<MessageUpdate> result) {
28+
this.result = result;
29+
}
30+
31+
@Override
32+
public String toString() {
33+
return "GetUpdatesResponse{" + "ok=" + ok + ", result=" + result + '}';
34+
}
35+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.temnenkov.mz.ports.tg.dto;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
import java.util.List;
6+
7+
public class Message {
8+
@SerializedName("message_id")
9+
private int messageId;
10+
11+
@SerializedName("from")
12+
private User from;
13+
14+
@SerializedName("chat")
15+
private Chat chat;
16+
17+
@SerializedName("date")
18+
private int date;
19+
20+
@SerializedName("text")
21+
private String text;
22+
23+
@SerializedName("entities")
24+
private List<Entity> entities;
25+
26+
public int getMessageId() {
27+
return messageId;
28+
}
29+
30+
public void setMessageId(int messageId) {
31+
this.messageId = messageId;
32+
}
33+
34+
public User getFrom() {
35+
return from;
36+
}
37+
38+
public void setFrom(User from) {
39+
this.from = from;
40+
}
41+
42+
public Chat getChat() {
43+
return chat;
44+
}
45+
46+
public void setChat(Chat chat) {
47+
this.chat = chat;
48+
}
49+
50+
public int getDate() {
51+
return date;
52+
}
53+
54+
public void setDate(int date) {
55+
this.date = date;
56+
}
57+
58+
public String getText() {
59+
return text;
60+
}
61+
62+
public void setText(String text) {
63+
this.text = text;
64+
}
65+
66+
public List<Entity> getEntities() {
67+
return entities;
68+
}
69+
70+
public void setEntities(List<Entity> entities) {
71+
this.entities = entities;
72+
}
73+
74+
@Override
75+
public String toString() {
76+
return "Message{" + "messageId=" + messageId + ", from=" + from + ", chat=" + chat + ", date=" + date + ", " +
77+
"text='" + text + '\'' + ", entities=" + entities + '}';
78+
}
79+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.temnenkov.mz.ports.tg.dto;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
public class MessageUpdate {
6+
@SerializedName("update_id")
7+
private long updateId;
8+
9+
@SerializedName("message")
10+
private Message message;
11+
12+
public long getUpdateId() {
13+
return updateId;
14+
}
15+
16+
public void setUpdateId(long updateId) {
17+
this.updateId = updateId;
18+
}
19+
20+
public Message getMessage() {
21+
return message;
22+
}
23+
24+
public void setMessage(Message message) {
25+
this.message = message;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return "MessageUpdate{" + "updateId=" + updateId + ", message=" + message + '}';
31+
}
32+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.temnenkov.mz.ports.tg.dto;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
public class User {
6+
@SerializedName("id")
7+
private long id;
8+
@SerializedName("is_bot")
9+
private boolean isBot;
10+
@SerializedName("first_name")
11+
private String firstName;
12+
@SerializedName("last_name")
13+
private String lastName;
14+
@SerializedName("username")
15+
private String username;
16+
@SerializedName("language_code")
17+
private String languageCode;
18+
@SerializedName("is_premium")
19+
private boolean isPremium;
20+
21+
public long getId() {
22+
return id;
23+
}
24+
25+
public void setId(long id) {
26+
this.id = id;
27+
}
28+
29+
public boolean isBot() {
30+
return isBot;
31+
}
32+
33+
public void setBot(boolean bot) {
34+
isBot = bot;
35+
}
36+
37+
public String getFirstName() {
38+
return firstName;
39+
}
40+
41+
public void setFirstName(String firstName) {
42+
this.firstName = firstName;
43+
}
44+
45+
public String getLastName() {
46+
return lastName;
47+
}
48+
49+
public void setLastName(String lastName) {
50+
this.lastName = lastName;
51+
}
52+
53+
public String getUsername() {
54+
return username;
55+
}
56+
57+
public void setUsername(String username) {
58+
this.username = username;
59+
}
60+
61+
public String getLanguageCode() {
62+
return languageCode;
63+
}
64+
65+
public void setLanguageCode(String languageCode) {
66+
this.languageCode = languageCode;
67+
}
68+
69+
public boolean isPremium() {
70+
return isPremium;
71+
}
72+
73+
public void setPremium(boolean premium) {
74+
isPremium = premium;
75+
}
76+
77+
@Override
78+
public String toString() {
79+
return "User{" + "id=" + id + ", isBot=" + isBot + ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", username='" + username + '\'' + ", languageCode='" + languageCode + '\'' + ", isPremium=" + isPremium + '}';
80+
}
81+
}

src/main/java/com/temnenkov/mz/utils/JsonUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ private JsonUtils() {
1414
public static String objectToJson(@Nullable Object object) {
1515
return gson.toJson(object);
1616
}
17+
18+
public static <T> T jsonToObject(String json, Class<T> clazz) {
19+
return gson.fromJson(json, clazz);
20+
}
21+
1722
}

0 commit comments

Comments
 (0)