Restructure and add formatted code to response model

XML & JSON formatting
This commit is contained in:
Ankit Mahato
2023-03-19 20:28:40 +05:30
parent af11727a6c
commit 49b1fee053
5 changed files with 111 additions and 70 deletions

View File

@ -1,2 +1,3 @@
export 'kvrow_model.dart';
export 'request_model.dart';
export 'response_model.dart';

View File

@ -1,12 +1,7 @@
import 'dart:io';
import 'dart:convert';
import 'dart:typed_data';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:http_parser/http_parser.dart';
import 'package:apidash/consts.dart';
import 'kvrow_model.dart';
import '../consts.dart';
import 'response_model.dart';
@immutable
class RequestModel {
@ -96,65 +91,3 @@ class RequestModel {
].join("\n");
}
}
@immutable
class ResponseModel {
const ResponseModel({
this.statusCode,
this.headers,
this.requestHeaders,
this.contentType,
this.mediaType,
this.body,
this.bodyBytes,
this.time,
});
final int? statusCode;
final Map<String, String>? headers;
final Map<String, String>? requestHeaders;
final String? contentType;
final MediaType? mediaType;
final String? body;
final Uint8List? bodyBytes;
final Duration? time;
ResponseModel fromResponse({
required Response response,
Duration? time,
}) {
var contentType = response.headers[HttpHeaders.contentTypeHeader];
MediaType? mediaType;
try {
mediaType = MediaType.parse(contentType!);
} catch (e) {
mediaType = null;
}
final responseHeaders = mergeMaps(
{HttpHeaders.contentLengthHeader: response.contentLength.toString()},
response.headers);
return ResponseModel(
statusCode: response.statusCode,
headers: responseHeaders,
requestHeaders: response.request?.headers,
contentType: contentType,
mediaType: mediaType,
body: (mediaType?.subtype == kSubTypeJson)
? utf8.decode(response.bodyBytes)
: response.body,
bodyBytes: response.bodyBytes,
time: time,
);
}
@override
String toString() {
return [
"Response Status: $statusCode",
"Response Time: $time",
"Response Headers: ${headers.toString()}",
"Response Request Headers: ${requestHeaders.toString()}",
"Response Body: $body",
].join("\n");
}
}

View File

@ -0,0 +1,76 @@
import 'dart:io';
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:collection/collection.dart';
import 'package:http/http.dart';
import 'package:http_parser/http_parser.dart';
import 'package:apidash/utils/utils.dart';
import 'package:apidash/consts.dart';
@immutable
class ResponseModel {
const ResponseModel({
this.statusCode,
this.headers,
this.requestHeaders,
this.contentType,
this.mediaType,
this.body,
this.formattedBody,
this.bodyBytes,
this.time,
});
final int? statusCode;
final Map<String, String>? headers;
final Map<String, String>? requestHeaders;
final String? contentType;
final MediaType? mediaType;
final String? body;
final String? formattedBody;
final Uint8List? bodyBytes;
final Duration? time;
ResponseModel fromResponse({
required Response response,
Duration? time,
}) {
MediaType? mediaType;
var contentType = response.headers[HttpHeaders.contentTypeHeader];
try {
mediaType = MediaType.parse(contentType!);
} catch (e) {
mediaType = null;
}
final responseHeaders = mergeMaps(
{HttpHeaders.contentLengthHeader: response.contentLength.toString()},
response.headers);
final body = (mediaType?.subtype == kSubTypeJson)
? utf8.decode(response.bodyBytes)
: response.body;
final formattedBody = formatBody(body, mediaType);
return ResponseModel(
statusCode: response.statusCode,
headers: responseHeaders,
requestHeaders: response.request?.headers,
contentType: contentType,
mediaType: mediaType,
body: body,
formattedBody: formattedBody,
bodyBytes: response.bodyBytes,
time: time,
);
}
@override
String toString() {
return [
"Response Status: $statusCode",
"Response Time: $time",
"Response Headers: ${headers.toString()}",
"Response Request Headers: ${requestHeaders.toString()}",
"Response Body: $body",
].join("\n");
}
}

View File

@ -1,5 +1,7 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http_parser/http_parser.dart';
import 'package:xml/xml.dart';
import '../consts.dart';
Color getResponseStatusCodeColor(int? statusCode,
@ -124,4 +126,32 @@ String formatHeaderCase(String text) {
else {
return (kNoBodyViewOptions, null);
}
}
String? formatBody(String body, MediaType? mediaType){
if(mediaType != null){
var subtype = mediaType.subtype;
try {
if(subtype.contains(kSubTypeJson)){
final tmp = jsonDecode(body);
String result = encoder.convert(tmp);
return result;
}
if(subtype.contains(kSubTypeXml)){
final document = XmlDocument.parse(body);
String result = document.toXmlString(pretty: true, indent: ' ');
return result;
}
if(subtype == kSubTypeHtml){
var len = body.length;
var lines = body.split("\n").length;
if(lines !=0 && len/lines <= kCodeCharsPerLineLimit){
return body;
}
}
} catch (e) {
return null;
}
}
return null;
}

View File

@ -18,8 +18,9 @@ dependencies:
http_parser: ^4.0.2
collection: ^1.17.0
google_fonts: ^4.0.3
flutter_highlighter: ^0.1.1
highlighter: ^0.1.1
string_scanner: ^1.2.0
xml: ^6.2.2
dev_dependencies:
flutter_test: