|
| 1 | +package org.bot4j.telegram.message; |
| 2 | + |
| 3 | +import org.alpha4j.common.Map4j; |
| 4 | +import org.bot4j.telegram.model.enums.TelegramIconMode; |
| 5 | +import org.unify4j.common.*; |
| 6 | +import org.unify4j.model.builder.HttpStatusBuilder; |
| 7 | +import org.unify4j.model.c.Method; |
| 8 | +import org.unify4j.model.enums.TimezoneType; |
| 9 | +import org.unify4j.model.response.HttpResponse; |
| 10 | + |
| 11 | +import java.io.UnsupportedEncodingException; |
| 12 | +import java.util.Date; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.TimeZone; |
| 15 | + |
| 16 | +public class HttpBuilder extends MarkdownBuilder { |
| 17 | + protected final Map4j<String, Object> placeholders; |
| 18 | + protected Date start; |
| 19 | + protected Date end; |
| 20 | + protected String timezone; |
| 21 | + |
| 22 | + public HttpBuilder() { |
| 23 | + super(); |
| 24 | + this.placeholders = new Map4j<>(); |
| 25 | + } |
| 26 | + |
| 27 | + protected static class Index { |
| 28 | + public static final String METHOD_INDEX = "method_index"; |
| 29 | + public static final String BASE_URL_INDEX = "base_url_index"; |
| 30 | + public static final String PATH_INDEX = "path_index"; |
| 31 | + public static final String QUERY_INDEX = "query_index"; |
| 32 | + public static final String REQUEST_INDEX = "request_index"; |
| 33 | + public static final String RESPONSE_INDEX = "response_index"; |
| 34 | + public static final String HEADER_INDEX = "header_index"; |
| 35 | + public static final String STATUS_INDEX = "code_index"; |
| 36 | + public static final String API_DESC_INDEX = "api_desc_index"; |
| 37 | + } |
| 38 | + |
| 39 | + public HttpBuilder apiDesc(String desc) { |
| 40 | + this.placeholders.put(Index.API_DESC_INDEX, desc); |
| 41 | + return this; |
| 42 | + } |
| 43 | + |
| 44 | + public HttpBuilder method(String method) { |
| 45 | + this.placeholders.put(Index.METHOD_INDEX, method); |
| 46 | + return this; |
| 47 | + } |
| 48 | + |
| 49 | + public HttpBuilder method(Method method) { |
| 50 | + if (method == null) { |
| 51 | + return this; |
| 52 | + } |
| 53 | + return this.method(method.getName()); |
| 54 | + } |
| 55 | + |
| 56 | + public HttpBuilder baseUrl(String url) { |
| 57 | + this.placeholders.put(Index.BASE_URL_INDEX, url); |
| 58 | + return this; |
| 59 | + } |
| 60 | + |
| 61 | + public HttpBuilder path(String url) { |
| 62 | + this.placeholders.put(Index.PATH_INDEX, url); |
| 63 | + return this; |
| 64 | + } |
| 65 | + |
| 66 | + public HttpBuilder query(Map<String, ?> queries) { |
| 67 | + if (Collection4j.isEmptyMap(queries)) { |
| 68 | + return this; |
| 69 | + } |
| 70 | + this.placeholders.put(Index.QUERY_INDEX, queries); |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + public HttpBuilder request(Map<String, ?> request) { |
| 75 | + if (Collection4j.isEmptyMap(request)) { |
| 76 | + return this; |
| 77 | + } |
| 78 | + this.placeholders.put(Index.REQUEST_INDEX, request); |
| 79 | + return this; |
| 80 | + } |
| 81 | + |
| 82 | + public HttpBuilder response(Map<String, ?> response) { |
| 83 | + if (Collection4j.isEmptyMap(response)) { |
| 84 | + return this; |
| 85 | + } |
| 86 | + this.placeholders.put(Index.RESPONSE_INDEX, response); |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + public HttpBuilder header(Map<String, ?> headers) { |
| 91 | + if (Collection4j.isEmptyMap(headers)) { |
| 92 | + return this; |
| 93 | + } |
| 94 | + this.placeholders.put(Index.HEADER_INDEX, headers); |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + public HttpBuilder status(int httpStatus) { |
| 99 | + this.placeholders.put(Index.STATUS_INDEX, httpStatus); |
| 100 | + return this; |
| 101 | + } |
| 102 | + |
| 103 | + public HttpBuilder status(HttpResponse response) { |
| 104 | + if (response == null) { |
| 105 | + return this; |
| 106 | + } |
| 107 | + return this.status(response.getCode()); |
| 108 | + } |
| 109 | + |
| 110 | + public HttpBuilder startTime() { |
| 111 | + this.start = new Date(); |
| 112 | + return this; |
| 113 | + } |
| 114 | + |
| 115 | + public HttpBuilder startTime(Date date) { |
| 116 | + this.start = date; |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + public HttpBuilder endTime() { |
| 121 | + this.end = new Date(); |
| 122 | + return this; |
| 123 | + } |
| 124 | + |
| 125 | + public HttpBuilder endTime(Date date) { |
| 126 | + this.end = date; |
| 127 | + return this; |
| 128 | + } |
| 129 | + |
| 130 | + public HttpBuilder timezone(String timezone) { |
| 131 | + this.timezone = timezone; |
| 132 | + return this; |
| 133 | + } |
| 134 | + |
| 135 | + public HttpBuilder timezone(TimezoneType timezone) { |
| 136 | + return this.timezone(timezone.getTimeZoneId()); |
| 137 | + } |
| 138 | + |
| 139 | + @SuppressWarnings({"unchecked"}) |
| 140 | + @Override |
| 141 | + public String toString() { |
| 142 | + if (this.placeholders.containsKey(Index.STATUS_INDEX)) { |
| 143 | + int status = (int) this.placeholders.get(Index.STATUS_INDEX); |
| 144 | + if (HttpStatusBuilder.isSuccess(status)) { |
| 145 | + this.icon(TelegramIconMode.SUCCESS) |
| 146 | + .bold(status) |
| 147 | + .timestampParenthesis() |
| 148 | + .line(); |
| 149 | + } else { |
| 150 | + this.icon(TelegramIconMode.ERROR).bold(status) |
| 151 | + .timestampParenthesis() |
| 152 | + .line(); |
| 153 | + } |
| 154 | + } else { |
| 155 | + this.icon(TelegramIconMode.TS_3).timestamp().line(); |
| 156 | + } |
| 157 | + if (this.placeholders.containsKey(Index.API_DESC_INDEX)) { |
| 158 | + this.bold("API").italic((String) this.placeholders.get(Index.API_DESC_INDEX)).line(); |
| 159 | + } |
| 160 | + if (this.placeholders.containsKey(Index.METHOD_INDEX)) { |
| 161 | + this.bold((String) this.placeholders.get(Index.METHOD_INDEX)); |
| 162 | + } |
| 163 | + if (this.placeholders.containsKey(Index.BASE_URL_INDEX)) { |
| 164 | + String url; |
| 165 | + if (this.placeholders.containsKey(Index.PATH_INDEX)) { |
| 166 | + url = String.format("%s%s", |
| 167 | + this.placeholders.get(Index.BASE_URL_INDEX), |
| 168 | + this.placeholders.get(Index.PATH_INDEX)); |
| 169 | + } else { |
| 170 | + url = (String) this.placeholders.get(Index.BASE_URL_INDEX); |
| 171 | + } |
| 172 | + if (this.placeholders.containsKey(Index.QUERY_INDEX)) { |
| 173 | + Map<String, Object> queries = (Map<String, Object>) Json4j.toMapFrom(this.placeholders.get(Index.QUERY_INDEX)); |
| 174 | + try { |
| 175 | + url = Request4j.appendQueryParams(url, queries); |
| 176 | + } catch (UnsupportedEncodingException ignored) { |
| 177 | + |
| 178 | + } |
| 179 | + } |
| 180 | + this.code(String4j.trimWhitespace(url)); |
| 181 | + } |
| 182 | + this.line(2); |
| 183 | + if (Object4j.allNotNull(this.start)) { |
| 184 | + if (String4j.isEmpty(this.timezone)) { |
| 185 | + this.bold("RFT:").timestamp(this.start).line(); |
| 186 | + } else { |
| 187 | + this.bold("RFT:").timestamp(this.start, TimeZone.getTimeZone(this.timezone)).line(); |
| 188 | + } |
| 189 | + } |
| 190 | + if (Object4j.allNotNull(this.end)) { |
| 191 | + if (String4j.isEmpty(this.timezone)) { |
| 192 | + this.bold("RT:").timestamp(this.end).line(); |
| 193 | + } else { |
| 194 | + this.bold("RT:").timestamp(this.end, TimeZone.getTimeZone(this.timezone)).line(); |
| 195 | + } |
| 196 | + } |
| 197 | + if (Object4j.allNotNull(this.start, this.end)) { |
| 198 | + this.icon(TelegramIconMode.RIGHT_ARROW_1) |
| 199 | + .bold("Time:") |
| 200 | + .code(Time4j.sinceSmallRecently(this.end, this.start)) |
| 201 | + .line(); |
| 202 | + } |
| 203 | + if (this.placeholders.containsKey(Index.HEADER_INDEX)) { |
| 204 | + this.bold("H:").line(); |
| 205 | + Map<String, Object> headers = (Map<String, Object>) Json4j.toMapFrom(this.placeholders.get(Index.HEADER_INDEX)); |
| 206 | + if (Collection4j.isNotEmptyMap(headers)) { |
| 207 | + headers.forEach((key, value) -> { |
| 208 | + this.code(key).text(":").code(value.toString()).line(); |
| 209 | + }); |
| 210 | + } |
| 211 | + } |
| 212 | + if (this.placeholders.containsKey(Index.REQUEST_INDEX)) { |
| 213 | + Map<String, Object> request = (Map<String, Object>) Json4j.toMapFrom(this.placeholders.get(Index.REQUEST_INDEX)); |
| 214 | + this.bold("Req:").preformatted("json", request); |
| 215 | + } |
| 216 | + if (this.placeholders.containsKey(Index.RESPONSE_INDEX)) { |
| 217 | + Map<String, Object> request = (Map<String, Object>) Json4j.toMapFrom(this.placeholders.get(Index.RESPONSE_INDEX)); |
| 218 | + this.bold("Resp:").preformatted("json", request); |
| 219 | + } |
| 220 | + return super.toString(); |
| 221 | + } |
| 222 | +} |
0 commit comments