|
9 | 9 | import io.github.easy4j.opencli.browser.result.OpenCliBrowserSourceComparison.Metrics; |
10 | 10 | import io.github.easy4j.opencli.core.OpenCliExecutionDetails; |
11 | 11 | import io.github.easy4j.opencli.core.OpenCliResult; |
| 12 | +import java.math.BigInteger; |
12 | 13 | import java.util.ArrayList; |
13 | 14 | import java.util.Collections; |
14 | 15 | import java.util.List; |
@@ -72,6 +73,26 @@ public static OpenCliBrowserResult<OpenCliBrowserSourceComparison> compareSource |
72 | 73 | return decode(raw, OpenCliBrowserDecoder::readComparison); |
73 | 74 | } |
74 | 75 |
|
| 76 | + /** @param raw browser find result @return strict selector matches */ |
| 77 | + public static OpenCliBrowserResult<OpenCliBrowserFindResult> find(OpenCliResult raw) { |
| 78 | + return decode(raw, OpenCliBrowserDecoder::readFind); |
| 79 | + } |
| 80 | + |
| 81 | + /** @param raw one-shot browser console result @return strict console capture */ |
| 82 | + public static OpenCliBrowserResult<OpenCliBrowserConsoleResult> console(OpenCliResult raw) { |
| 83 | + return decode(raw, OpenCliBrowserDecoder::readConsole); |
| 84 | + } |
| 85 | + |
| 86 | + /** @param raw one-shot browser network listing @return strict network capture */ |
| 87 | + public static OpenCliBrowserResult<OpenCliBrowserNetworkResult> network(OpenCliResult raw) { |
| 88 | + return decode(raw, OpenCliBrowserDecoder::readNetwork); |
| 89 | + } |
| 90 | + |
| 91 | + /** @param raw browser analyze result @return strict analysis observation */ |
| 92 | + public static OpenCliBrowserResult<OpenCliBrowserAnalysisResult> analyze(OpenCliResult raw) { |
| 93 | + return decode(raw, OpenCliBrowserDecoder::readAnalysis); |
| 94 | + } |
| 95 | + |
75 | 96 | private static Status unavailableStatus(OpenCliResult raw) { |
76 | 97 | Objects.requireNonNull(raw, "raw"); |
77 | 98 | OpenCliExecutionDetails execution = raw.getExecutionDetails(); |
@@ -164,6 +185,94 @@ private static Metrics readMetrics(JsonNode json) { |
164 | 185 | counter(json.path("frame_sections")), interactive.isMissingNode() ? null : counter(interactive), null, null); |
165 | 186 | } |
166 | 187 |
|
| 188 | + private static OpenCliBrowserFindResult readFind(JsonNode json) { |
| 189 | + long total = counter(json.path("matches_n")); |
| 190 | + JsonNode reported = json.path("entries"); |
| 191 | + if (total < reported.size()) { |
| 192 | + throw invalidShape(); |
| 193 | + } |
| 194 | + List<OpenCliBrowserFindResult.Entry> entries = entries(reported, item -> { |
| 195 | + JsonNode ref = item.path("ref"); |
| 196 | + JsonNode visible = item.path("visible"); |
| 197 | + BigInteger numericRef = ref.isNull() ? null : unsignedInteger(ref); |
| 198 | + if (!visible.isBoolean() || !item.path("attrs").isObject()) { |
| 199 | + throw invalidShape(); |
| 200 | + } |
| 201 | + return new OpenCliBrowserFindResult.Entry(counter(item.path("nth")), numericRef, text(item.path("tag")), |
| 202 | + text(item.path("role")), text(item.path("text")), visible.booleanValue(), item); |
| 203 | + }); |
| 204 | + return new OpenCliBrowserFindResult(total, entries, json); |
| 205 | + } |
| 206 | + |
| 207 | + private static OpenCliBrowserConsoleResult readConsole(JsonNode json) { |
| 208 | + JsonNode reported = json.path("messages"); |
| 209 | + validateCount(json, reported); |
| 210 | + List<OpenCliBrowserConsoleResult.Message> messages = entries(reported, item -> |
| 211 | + new OpenCliBrowserConsoleResult.Message(optionalText(item.path("type")), optionalText(item.path("text")), |
| 212 | + text(item.path("timestamp")), item)); |
| 213 | + return new OpenCliBrowserConsoleResult(text(json.path("session")), text(json.path("captured_at")), messages, json); |
| 214 | + } |
| 215 | + |
| 216 | + private static OpenCliBrowserNetworkResult readNetwork(JsonNode json) { |
| 217 | + JsonNode reported = json.path("entries"); |
| 218 | + validateCount(json, reported); |
| 219 | + List<OpenCliBrowserNetworkResult.Entry> entries = entries(reported, item -> { |
| 220 | + JsonNode status = item.path("status"); |
| 221 | + if (!status.isIntegralNumber() || !status.canConvertToInt() || status.intValue() < 0) { |
| 222 | + throw invalidShape(); |
| 223 | + } |
| 224 | + return new OpenCliBrowserNetworkResult.Entry(text(item.path("key")), text(item.path("method")), |
| 225 | + status.intValue(), text(item.path("url")), text(item.path("ct")), counter(item.path("size")), |
| 226 | + optionalText(item.path("timestamp")), item); |
| 227 | + }); |
| 228 | + return new OpenCliBrowserNetworkResult(text(json.path("session")), text(json.path("captured_at")), |
| 229 | + counter(json.path("filtered_out")), entries, json); |
| 230 | + } |
| 231 | + |
| 232 | + private static OpenCliBrowserAnalysisResult readAnalysis(JsonNode json) { |
| 233 | + JsonNode pattern = json.path("pattern"); |
| 234 | + if (!json.path("anti_bot").isObject() || !json.path("initial_state").isObject() |
| 235 | + || !json.path("api_candidates").isArray() || !json.path("recommended_next_step").isTextual()) { |
| 236 | + throw invalidShape(); |
| 237 | + } |
| 238 | + return new OpenCliBrowserAnalysisResult(text(json.path("requested_url")), text(json.path("final_url")), |
| 239 | + text(json.path("title")), text(pattern.path("pattern")), json); |
| 240 | + } |
| 241 | + |
| 242 | + private static <T> List<T> entries(JsonNode array, Function<JsonNode, T> entry) { |
| 243 | + if (!array.isArray()) { |
| 244 | + throw invalidShape(); |
| 245 | + } |
| 246 | + List<T> result = new ArrayList<>(array.size()); |
| 247 | + for (JsonNode item : array) { |
| 248 | + if (!item.isObject()) { |
| 249 | + throw invalidShape(); |
| 250 | + } |
| 251 | + result.add(entry.apply(item)); |
| 252 | + } |
| 253 | + return Collections.unmodifiableList(result); |
| 254 | + } |
| 255 | + |
| 256 | + private static void validateCount(JsonNode json, JsonNode entries) { |
| 257 | + if (counter(json.path("count")) != entries.size()) { |
| 258 | + throw invalidShape(); |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + private static BigInteger unsignedInteger(JsonNode node) { |
| 263 | + if (!node.isIntegralNumber() || node.bigIntegerValue().signum() < 0) { |
| 264 | + throw invalidShape(); |
| 265 | + } |
| 266 | + return node.bigIntegerValue(); |
| 267 | + } |
| 268 | + |
| 269 | + private static String text(JsonNode node) { |
| 270 | + if (!node.isTextual()) { |
| 271 | + throw invalidShape(); |
| 272 | + } |
| 273 | + return node.textValue(); |
| 274 | + } |
| 275 | + |
167 | 276 | private static long counter(JsonNode node) { |
168 | 277 | if (!node.isIntegralNumber() || !node.canConvertToLong() || node.longValue() < 0) { |
169 | 278 | throw invalidShape(); |
|
0 commit comments