mirror of
https://github.com/foss42/apidash.git
synced 2025-05-24 01:36:46 +08:00
Re-organize utilities based on category
This commit is contained in:
@ -43,7 +43,7 @@ class _ResponseBodyState extends ConsumerState<ResponseBody> {
|
|||||||
message: 'Response body is empty. $kUnexpectedRaiseIssue');
|
message: 'Response body is empty. $kUnexpectedRaiseIssue');
|
||||||
}
|
}
|
||||||
var responseBodyView = getResponseBodyViewOptions(mediaType);
|
var responseBodyView = getResponseBodyViewOptions(mediaType);
|
||||||
print(responseBodyView);
|
//print(responseBodyView);
|
||||||
var options = responseBodyView.$0;
|
var options = responseBodyView.$0;
|
||||||
var highlightLanguage = responseBodyView.$1;
|
var highlightLanguage = responseBodyView.$1;
|
||||||
if (options == kNoBodyViewOptions) {
|
if (options == kNoBodyViewOptions) {
|
||||||
|
68
lib/utils/http_utils.dart
Normal file
68
lib/utils/http_utils.dart
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
import 'package:http_parser/http_parser.dart';
|
||||||
|
import 'package:xml/xml.dart';
|
||||||
|
import '../consts.dart';
|
||||||
|
|
||||||
|
String getRequestTitleFromUrl(String? url) {
|
||||||
|
if (url == null || url.trim() == "") {
|
||||||
|
return "untitled";
|
||||||
|
}
|
||||||
|
if (url.contains("://")) {
|
||||||
|
String rem = url.split("://")[1];
|
||||||
|
if (rem.trim() == "") {
|
||||||
|
return "untitled";
|
||||||
|
}
|
||||||
|
return rem;
|
||||||
|
}
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
(List<ResponseBodyView>, String?) getResponseBodyViewOptions(MediaType mediaType){
|
||||||
|
var type = mediaType.type;
|
||||||
|
var subtype = mediaType.subtype;
|
||||||
|
//print(mediaType);
|
||||||
|
if(kResponseBodyViewOptions.containsKey(type)){
|
||||||
|
if(subtype.contains(kSubTypeJson)){
|
||||||
|
subtype = kSubTypeJson;
|
||||||
|
}
|
||||||
|
if(subtype.contains(kSubTypeXml)){
|
||||||
|
subtype = kSubTypeXml;
|
||||||
|
}
|
||||||
|
if (kResponseBodyViewOptions[type]!.containsKey(subtype)){
|
||||||
|
return (kResponseBodyViewOptions[type]![subtype]!, kCodeHighlighterMap[subtype] ?? subtype);
|
||||||
|
}
|
||||||
|
return (kResponseBodyViewOptions[type]![kSubTypeDefaultViewOptions]!, subtype);
|
||||||
|
}
|
||||||
|
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 = kEncoder.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 = kSplitter.convert(body);
|
||||||
|
var numOfLines = lines.length;
|
||||||
|
if(numOfLines !=0 && len/numOfLines <= kCodeCharsPerLineLimit){
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
36
lib/utils/text_utils.dart
Normal file
36
lib/utils/text_utils.dart
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
String humanizeDuration(Duration? duration) {
|
||||||
|
if (duration == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
if (duration.inMinutes >= 1) {
|
||||||
|
var min = duration.inMinutes;
|
||||||
|
var secs = duration.inSeconds.remainder(60);
|
||||||
|
return "$min.$secs m";
|
||||||
|
}
|
||||||
|
if (duration.inSeconds >= 1) {
|
||||||
|
var secs = duration.inSeconds;
|
||||||
|
var mili = duration.inMilliseconds.remainder(1000) ~/ 10;
|
||||||
|
return "$secs.$mili s";
|
||||||
|
} else {
|
||||||
|
var mili = duration.inMilliseconds.remainder(1000);
|
||||||
|
return "$mili ms";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String capitalizeFirstLetter(String? text) {
|
||||||
|
if (text == null || text == "") {
|
||||||
|
return "";
|
||||||
|
} else if (text.length == 1) {
|
||||||
|
return text.toUpperCase();
|
||||||
|
} else {
|
||||||
|
var first = text[0];
|
||||||
|
var rest = text.substring(1);
|
||||||
|
return first.toUpperCase() + rest;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String formatHeaderCase(String text) {
|
||||||
|
var sp = text.split("-");
|
||||||
|
sp = sp.map((e) => capitalizeFirstLetter(e)).toList();
|
||||||
|
return sp.join("-");
|
||||||
|
}
|
54
lib/utils/ui_utils.dart
Normal file
54
lib/utils/ui_utils.dart
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import '../consts.dart';
|
||||||
|
|
||||||
|
Color getResponseStatusCodeColor(int? statusCode,
|
||||||
|
{Brightness brightness = Brightness.light}) {
|
||||||
|
Color col = Colors.grey.shade700;
|
||||||
|
if (statusCode != null) {
|
||||||
|
if (statusCode >= 200) {
|
||||||
|
col = Colors.green.shade800;
|
||||||
|
}
|
||||||
|
if (statusCode >= 300) {
|
||||||
|
col = Colors.blue.shade800;
|
||||||
|
}
|
||||||
|
if (statusCode >= 400) {
|
||||||
|
col = Colors.red.shade800;
|
||||||
|
}
|
||||||
|
if (statusCode >= 500) {
|
||||||
|
col = Colors.amber.shade900;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (brightness == Brightness.dark) {
|
||||||
|
col = Color.alphaBlend(col.withOpacity(0.4), Colors.white);
|
||||||
|
}
|
||||||
|
return col;
|
||||||
|
}
|
||||||
|
|
||||||
|
Color getHTTPMethodColor(HTTPVerb method,
|
||||||
|
{Brightness brightness = Brightness.light}) {
|
||||||
|
Color col;
|
||||||
|
switch (method) {
|
||||||
|
case HTTPVerb.get:
|
||||||
|
col = Colors.green.shade800;
|
||||||
|
break;
|
||||||
|
case HTTPVerb.head:
|
||||||
|
col = Colors.green.shade800;
|
||||||
|
break;
|
||||||
|
case HTTPVerb.post:
|
||||||
|
col = Colors.blue.shade800;
|
||||||
|
break;
|
||||||
|
case HTTPVerb.put:
|
||||||
|
col = Colors.amber.shade900;
|
||||||
|
break;
|
||||||
|
case HTTPVerb.patch:
|
||||||
|
col = Colors.amber.shade900;
|
||||||
|
break;
|
||||||
|
case HTTPVerb.delete:
|
||||||
|
col = Colors.red.shade800;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (brightness == Brightness.dark) {
|
||||||
|
col = Color.alphaBlend(col.withOpacity(0.4), Colors.white);
|
||||||
|
}
|
||||||
|
return col;
|
||||||
|
}
|
@ -1,158 +1,3 @@
|
|||||||
import 'dart:convert';
|
export 'ui_utils.dart';
|
||||||
import 'package:flutter/material.dart';
|
export 'text_utils.dart';
|
||||||
import 'package:http_parser/http_parser.dart';
|
export 'http_utils.dart';
|
||||||
import 'package:xml/xml.dart';
|
|
||||||
import '../consts.dart';
|
|
||||||
|
|
||||||
Color getResponseStatusCodeColor(int? statusCode,
|
|
||||||
{Brightness brightness = Brightness.light}) {
|
|
||||||
Color col = Colors.grey.shade700;
|
|
||||||
if (statusCode != null) {
|
|
||||||
if (statusCode >= 200) {
|
|
||||||
col = Colors.green.shade800;
|
|
||||||
}
|
|
||||||
if (statusCode >= 300) {
|
|
||||||
col = Colors.blue.shade800;
|
|
||||||
}
|
|
||||||
if (statusCode >= 400) {
|
|
||||||
col = Colors.red.shade800;
|
|
||||||
}
|
|
||||||
if (statusCode >= 500) {
|
|
||||||
col = Colors.amber.shade900;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (brightness == Brightness.dark) {
|
|
||||||
col = Color.alphaBlend(col.withOpacity(0.4), Colors.white);
|
|
||||||
}
|
|
||||||
return col;
|
|
||||||
}
|
|
||||||
|
|
||||||
Color getHTTPMethodColor(HTTPVerb method,
|
|
||||||
{Brightness brightness = Brightness.light}) {
|
|
||||||
Color col;
|
|
||||||
switch (method) {
|
|
||||||
case HTTPVerb.get:
|
|
||||||
col = Colors.green.shade800;
|
|
||||||
break;
|
|
||||||
case HTTPVerb.head:
|
|
||||||
col = Colors.green.shade800;
|
|
||||||
break;
|
|
||||||
case HTTPVerb.post:
|
|
||||||
col = Colors.blue.shade800;
|
|
||||||
break;
|
|
||||||
case HTTPVerb.put:
|
|
||||||
col = Colors.amber.shade900;
|
|
||||||
break;
|
|
||||||
case HTTPVerb.patch:
|
|
||||||
col = Colors.amber.shade900;
|
|
||||||
break;
|
|
||||||
case HTTPVerb.delete:
|
|
||||||
col = Colors.red.shade800;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (brightness == Brightness.dark) {
|
|
||||||
col = Color.alphaBlend(col.withOpacity(0.4), Colors.white);
|
|
||||||
}
|
|
||||||
return col;
|
|
||||||
}
|
|
||||||
|
|
||||||
String getRequestTitleFromUrl(String? url) {
|
|
||||||
if (url == null || url.trim() == "") {
|
|
||||||
return "untitled";
|
|
||||||
}
|
|
||||||
if (url.contains("://")) {
|
|
||||||
String rem = url.split("://")[1];
|
|
||||||
if (rem.trim() == "") {
|
|
||||||
return "untitled";
|
|
||||||
}
|
|
||||||
return rem;
|
|
||||||
}
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
|
|
||||||
String humanizeDuration(Duration? duration) {
|
|
||||||
if (duration == null) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
if (duration.inMinutes >= 1) {
|
|
||||||
var min = duration.inMinutes;
|
|
||||||
var secs = duration.inSeconds.remainder(60);
|
|
||||||
return "$min.$secs m";
|
|
||||||
}
|
|
||||||
if (duration.inSeconds >= 1) {
|
|
||||||
var secs = duration.inSeconds;
|
|
||||||
var mili = duration.inMilliseconds.remainder(1000) ~/ 10;
|
|
||||||
return "$secs.$mili s";
|
|
||||||
} else {
|
|
||||||
var mili = duration.inMilliseconds.remainder(1000);
|
|
||||||
return "$mili ms";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
String capitalizeFirstLetter(String? text) {
|
|
||||||
if (text == null || text == "") {
|
|
||||||
return "";
|
|
||||||
} else if (text.length == 1) {
|
|
||||||
return text.toUpperCase();
|
|
||||||
} else {
|
|
||||||
var first = text[0];
|
|
||||||
var rest = text.substring(1);
|
|
||||||
return first.toUpperCase() + rest;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
String formatHeaderCase(String text) {
|
|
||||||
var sp = text.split("-");
|
|
||||||
sp = sp.map((e) => capitalizeFirstLetter(e)).toList();
|
|
||||||
return sp.join("-");
|
|
||||||
}
|
|
||||||
|
|
||||||
(List<ResponseBodyView>, String?) getResponseBodyViewOptions(MediaType mediaType){
|
|
||||||
var type = mediaType.type;
|
|
||||||
var subtype = mediaType.subtype;
|
|
||||||
print(mediaType);
|
|
||||||
if(kResponseBodyViewOptions.containsKey(type)){
|
|
||||||
if(subtype.contains(kSubTypeJson)){
|
|
||||||
subtype = kSubTypeJson;
|
|
||||||
}
|
|
||||||
if(subtype.contains(kSubTypeXml)){
|
|
||||||
subtype = kSubTypeXml;
|
|
||||||
}
|
|
||||||
if (kResponseBodyViewOptions[type]!.containsKey(subtype)){
|
|
||||||
return (kResponseBodyViewOptions[type]![subtype]!, kCodeHighlighterMap[subtype] ?? subtype);
|
|
||||||
}
|
|
||||||
return (kResponseBodyViewOptions[type]![kSubTypeDefaultViewOptions]!, subtype);
|
|
||||||
}
|
|
||||||
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 = kEncoder.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 = kSplitter.convert(body);
|
|
||||||
var numOfLines = lines.length;
|
|
||||||
if(numOfLines !=0 && len/numOfLines <= kCodeCharsPerLineLimit){
|
|
||||||
return body;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user