1- using System . Linq ;
1+ using System ;
2+ using System . Linq ;
3+ using System . Text ;
4+ using System . Text . RegularExpressions ;
25using Elasticsearch . Net ;
36
47namespace Nest
@@ -12,8 +15,53 @@ public LowLevelDispatch(IElasticLowLevelClient rawElasticClient)
1215 this . _lowLevel = rawElasticClient ;
1316 }
1417
15- internal bool AllSet ( params string [ ] pathVariables ) => pathVariables . All ( p => ! p . IsNullOrEmpty ( ) ) && ! pathVariables . All ( p=> p == "_all" ) ;
18+ internal bool AllSet ( params string [ ] pathVariables ) => pathVariables . All ( p => ! p . IsNullOrEmpty ( ) ) && ! pathVariables . All ( p => p == "_all" ) ;
19+
20+ internal bool AllSet ( params IUrlParameter [ ] pathVariables ) => pathVariables . All ( p => p != null ) ;
21+
22+ internal static Exception InvalidDispatch ( string apiCall , IRequest provided , HttpMethod [ ] methods , params string [ ] endpoints )
23+ {
24+ var sb = new StringBuilder ( ) ;
25+ sb . AppendLine ( $ "Dispatching { apiCall } () from NEST into to Elasticsearch.NET failed") ;
26+ sb . AppendLine ( $ "Received a request marked as { provided . HttpMethod . GetStringValue ( ) } ") ;
27+ sb . AppendLine ( $ "This endpoint accepts { string . Join ( "," , methods . Select ( p=> p . GetStringValue ( ) ) ) } ") ;
28+ sb . AppendLine ( $ "The request might not have enough information provided to make any of these endpoints:") ;
29+ foreach ( var endpoint in endpoints )
30+ sb . AppendLine ( $ " - { PrettyPrintEndpoint ( provided , endpoint ) } ") ;
31+
32+ return new ArgumentException ( sb . ToString ( ) ) ;
33+ }
34+
35+ private static Regex ReplaceParams = new Regex ( @"\{(.+?)\}" ) ;
36+
37+ internal static string PrettyPrintEndpoint ( IRequest request , string endpoint )
38+ {
39+ var pretty = ReplaceParams . Replace ( endpoint , ( m ) =>
40+ {
41+ var key = m . Groups [ 1 ] . Value . ToLowerInvariant ( ) ;
42+ switch ( key )
43+ {
44+ case "index" : return PrettyParam ( key , request . RouteValues . Index ) ;
45+ case "name" : return PrettyParam ( key , request . RouteValues . Name ) ;
46+ case "feature" : return PrettyParam ( key , request . RouteValues . Feature ) ;
47+ case "field" : return PrettyParam ( key , request . RouteValues . Field ) ;
48+ case "fields" : return PrettyParam ( key , request . RouteValues . Fields ) ;
49+ case "id" : return PrettyParam ( key , request . RouteValues . Id ) ;
50+ case "index_metric" : return PrettyParam ( key , request . RouteValues . IndexMetric ) ;
51+ case "lang" : return PrettyParam ( key , request . RouteValues . Lang ) ;
52+ case "metric" : return PrettyParam ( key , request . RouteValues . Metric ) ;
53+ case "node_id" : return PrettyParam ( key , request . RouteValues . NodeId ) ;
54+ case "repository" : return PrettyParam ( key , request . RouteValues . Repository ) ;
55+ case "scroll_id" : return PrettyParam ( key , request . RouteValues . ScrollId ) ;
56+ case "snapshot" : return PrettyParam ( key , request . RouteValues . Snapshot ) ;
57+ case "type" : return PrettyParam ( key , request . RouteValues . Type ) ;
58+ default : return PrettyParam ( key , "<Unknown route variable>" ) ;
59+ }
60+ } ) ;
61+ return pretty ;
62+ }
63+
64+ private static string PrettyParam ( string key , string value ) => $ "{{{key}={ ( value . IsNullOrEmpty ( ) ? "<NULL>" : value ) } }}";
1665
17- internal bool AllSet ( params IUrlParameter [ ] pathVariables ) => pathVariables . All ( p => p != null ) ;
1866 }
1967}
0 commit comments