Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
_view_graphql.py73 linesDownload Raw Back to contentviews
1import json2from typing import Any3 4from mitmproxy.contentviews._api import Contentview5from mitmproxy.contentviews._api import Metadata6 7 8def format_graphql(data):9    query = data["query"]10    header_data = data.copy()11    header_data["query"] = "..."12    return """{header}13---14{query}15""".format(header=json.dumps(header_data, indent=2), query=query)16 17 18def format_query_list(data: list[Any]):19    num_queries = len(data) - 120    result = ""21    for i, op in enumerate(data):22        result += f"--- {i}/{num_queries}\n"23        result += format_graphql(op)24    return result25 26 27def is_graphql_query(data):28    return isinstance(data, dict) and "query" in data and "\n" in data["query"]29 30 31def is_graphql_batch_query(data):32    return (33        isinstance(data, list)34        and len(data) > 035        and isinstance(data[0], dict)36        and "query" in data[0]37    )38 39 40class GraphQLContentview(Contentview):41    def prettify(42        self,43        data: bytes,44        metadata: Metadata,45    ) -> str:46        gql = json.loads(data)47        if is_graphql_query(gql):48            return format_graphql(gql)49        elif is_graphql_batch_query(gql):50            return format_query_list(gql)51        else:52            raise ValueError("Not a GraphQL message.")53 54    def render_priority(55        self,56        data: bytes,57        metadata: Metadata,58    ) -> float:59        if metadata.content_type != "application/json" or not data:60            return 061 62        try:63            data = json.loads(data)64            if is_graphql_query(data) or is_graphql_batch_query(data):65                return 266        except ValueError:67            pass68 69        return 070 71 72graphql = GraphQLContentview()73 
codekingpro/portable-devtools · Team Ai