@@ -30,7 +30,7 @@ public static class ApiGenerator
3030 static ApiGenerator ( )
3131 {
3232 _razorMachine = new RazorMachine ( ) ;
33- _assembly = typeof ( ApiGenerator ) . Assembly ;
33+ _assembly = typeof ( ApiGenerator ) . Assembly ;
3434 }
3535 public static string PascalCase ( string s )
3636 {
@@ -44,11 +44,11 @@ public static RestApiSpec GetRestSpec(bool useCache)
4444 string html = string . Empty ;
4545 using ( var client = new WebClient ( ) )
4646 html = client . DownloadString ( useCache ? LocalUri ( "root.html" ) : _listingUrl ) ;
47-
47+
4848 var dom = CQ . Create ( html ) ;
49- if ( ! useCache )
50- WriteToCache ( "root.html" , html ) ;
51-
49+ if ( ! useCache )
50+ WriteToCache ( "root.html" , html ) ;
51+
5252 var endpoints = dom [ ".js-directory-link" ]
5353 . Select ( s => s . InnerText )
5454 . Where ( s => ! string . IsNullOrEmpty ( s ) && s . EndsWith ( ".json" ) )
@@ -78,11 +78,11 @@ private static KeyValuePair<string, ApiEndpoint> CreateApiDocumentation(bool use
7878 using ( var client = new WebClient ( ) )
7979 {
8080 var rawFile = _rawUrlPrefix + s ;
81- var fileName = rawFile . Split ( new [ ] { '/' } ) . Last ( ) ;
81+ var fileName = rawFile . Split ( new [ ] { '/' } ) . Last ( ) ;
8282 Console . WriteLine ( "Downloading {0}" , rawFile ) ;
8383 var json = client . DownloadString ( useCache ? LocalUri ( fileName ) : rawFile ) ;
84- if ( ! useCache )
85- WriteToCache ( fileName , json ) ;
84+ if ( ! useCache )
85+ WriteToCache ( fileName , json ) ;
8686
8787 var apiDocumentation = JsonConvert . DeserializeObject < Dictionary < string , ApiEndpoint > > ( json ) . First ( ) ;
8888 apiDocumentation . Value . CsharpMethodName = CreateMethodName (
@@ -95,30 +95,30 @@ private static KeyValuePair<string, ApiEndpoint> CreateApiDocumentation(bool use
9595 private static readonly Dictionary < string , string > MethodNameOverrides =
9696 ( from f in new DirectoryInfo ( _nestFolder + "/DSL" ) . GetFiles ( "*.cs" , SearchOption . TopDirectoryOnly )
9797 where f . FullName . EndsWith ( "Descriptor.cs" )
98- let contents = File . ReadAllText ( f . FullName )
99- let c = Regex . Replace ( contents , @"^.+\[DescriptorFor\(""([^ \r\n]+)""\)\].*$" , "$1" , RegexOptions . Singleline )
100- where ! c . Contains ( " " ) //filter results that did not match
101- select new { Value = f . Name . Replace ( "Descriptor.cs" , "" ) , Key = c } )
102- . DistinctBy ( v=> v . Key )
98+ let contents = File . ReadAllText ( f . FullName )
99+ let c = Regex . Replace ( contents , @"^.+\[DescriptorFor\(""([^ \r\n]+)""\)\].*$" , "$1" , RegexOptions . Singleline )
100+ where ! c . Contains ( " " ) //filter results that did not match
101+ select new { Value = f . Name . Replace ( "Descriptor.cs" , "" ) , Key = c } )
102+ . DistinctBy ( v => v . Key )
103103 . ToDictionary ( k => k . Key , v => v . Value ) ;
104104
105105 private static readonly Dictionary < string , string > KnownDescriptors =
106106 ( from f in new DirectoryInfo ( _nestFolder + "/DSL" ) . GetFiles ( "*.cs" , SearchOption . TopDirectoryOnly )
107107 where f . FullName . EndsWith ( "Descriptor.cs" )
108- let contents = File . ReadAllText ( f . FullName )
109- let c = Regex . Replace ( contents , @"^.+class ([^ \r\n]+).*$" , "$1" , RegexOptions . Singleline )
110- select new { Key = Regex . Replace ( c , "<.*$" , "" ) , Value = Regex . Replace ( c , @"^.*?(?:(\<.+>).*?)?$" , "$1" ) } )
111- . DistinctBy ( v=> v . Key )
108+ let contents = File . ReadAllText ( f . FullName )
109+ let c = Regex . Replace ( contents , @"^.+class ([^ \r\n]+).*$" , "$1" , RegexOptions . Singleline )
110+ select new { Key = Regex . Replace ( c , "<.*$" , "" ) , Value = Regex . Replace ( c , @"^.*?(?:(\<.+>).*?)?$" , "$1" ) } )
111+ . DistinctBy ( v => v . Key )
112112 . ToDictionary ( k => k . Key , v => v . Value ) ;
113-
113+
114114 private static readonly Dictionary < string , string > KnownRequests =
115115 ( from f in new DirectoryInfo ( _nestFolder + "/DSL" ) . GetFiles ( "*.cs" , SearchOption . TopDirectoryOnly )
116116 where f . FullName . EndsWith ( "Descriptor.cs" )
117- let contents = File . ReadAllText ( f . FullName )
118- let c = Regex . Replace ( contents , @"^.+interface ([^ \r\n]+).*$" , "$1" , RegexOptions . Singleline )
119- where c . StartsWith ( "I" ) && c . Contains ( "Request" )
120- select new { Key = Regex . Replace ( c , "<.*$" , "" ) , Value = Regex . Replace ( c , @"^.*?(?:(\<.+>).*?)?$" , "$1" ) } )
121- . DistinctBy ( v=> v . Key )
117+ let contents = File . ReadAllText ( f . FullName )
118+ let c = Regex . Replace ( contents , @"^.+interface ([^ \r\n]+).*$" , "$1" , RegexOptions . Singleline )
119+ where c . StartsWith ( "I" ) && c . Contains ( "Request" )
120+ select new { Key = Regex . Replace ( c , "<.*$" , "" ) , Value = Regex . Replace ( c , @"^.*?(?:(\<.+>).*?)?$" , "$1" ) } )
121+ . DistinctBy ( v => v . Key )
122122 . ToDictionary ( k => k . Key , v => v . Value ) ;
123123
124124
@@ -132,10 +132,10 @@ public static void PatchMethod(CsharpMethod method)
132132
133133 if ( ms ( "Indices" ) && ! pc ( "{index}" ) )
134134 method . FullName = ( method . FullName + "ForAll" ) . Replace ( "AsyncForAll" , "ForAllAsync" ) ;
135-
135+
136136 if ( ms ( "Nodes" ) && ! pc ( "{node_id}" ) )
137137 method . FullName = ( method . FullName + "ForAll" ) . Replace ( "AsyncForAll" , "ForAllAsync" ) ;
138-
138+
139139 //remove duplicate occurance of the HTTP method name
140140 var m = method . HttpMethod . ToPascalCase ( ) ;
141141 method . FullName =
@@ -146,57 +146,68 @@ public static void PatchMethod(CsharpMethod method)
146146 if ( MethodNameOverrides . TryGetValue ( key , out manualOverride ) )
147147 method . QueryStringParamName = manualOverride + "RequestParameters" ;
148148
149- method . DescriptorType = method . QueryStringParamName . Replace ( "RequestParameters" , "Descriptor" ) ;
150- method . RequestType = method . QueryStringParamName . Replace ( "RequestParameters" , "Request" ) ;
149+ method . DescriptorType = method . QueryStringParamName . Replace ( "RequestParameters" , "Descriptor" ) ;
150+ method . RequestType = method . QueryStringParamName . Replace ( "RequestParameters" , "Request" ) ;
151151 string requestGeneric ;
152152 if ( KnownRequests . TryGetValue ( "I" + method . RequestType , out requestGeneric ) )
153153 method . RequestTypeGeneric = requestGeneric ;
154154 else method . RequestTypeUnmapped = true ;
155-
155+
156156 method . Allow404 = ApiEndpointsThatAllow404 . Endpoints . Contains ( method . DescriptorType . Replace ( "Descriptor" , "" ) ) ;
157-
157+
158158 string generic ;
159159 if ( KnownDescriptors . TryGetValue ( method . DescriptorType , out generic ) )
160160 method . DescriptorTypeGeneric = generic ;
161161 else method . Unmapped = true ;
162162
163163 try
164164 {
165+ IEnumerable < string > skipList = new List < string > ( ) ;
166+ IDictionary < string , string > renameList = new Dictionary < string , string > ( ) ;
167+
165168 var typeName = "CodeGeneration.LowLevelClient.Overrides.Descriptors." + method . DescriptorType + "Overrides" ;
166169 var type = _assembly . GetType ( typeName ) ;
167- if ( type == null )
168- return ;
169- var overrides = Activator . CreateInstance ( type ) as IDescriptorOverrides ;
170- if ( overrides == null )
171- return ;
170+ if ( type != null )
171+ {
172+ var overrides = Activator . CreateInstance ( type ) as IDescriptorOverrides ;
173+ if ( overrides != null )
174+ {
175+ skipList = overrides . SkipQueryStringParams ?? skipList ;
176+ renameList = overrides . RenameQueryStringParams ?? renameList ;
177+ }
178+ }
179+
172180
173181 var globalQueryStringRenames = new Dictionary < string , string >
174182 {
175- { "__source " , "_source_enabled " } ,
176- { "__source_include " , "_source_include " } ,
177- { "__source_exclude " , "_source_exclude " } ,
183+ { "_source " , "source_enabled " } ,
184+ { "_source_include " , "source_include " } ,
185+ { "_source_exclude " , "source_exclude " } ,
178186 } ;
179- var qs = overrides . RenameQueryStringParams ?? new Dictionary < string , string > ( ) ;
187+
180188 foreach ( var kv in globalQueryStringRenames )
181- qs [ kv . Key ] = kv . Value ;
189+ if ( ! renameList . ContainsKey ( kv . Key ) )
190+ renameList [ kv . Key ] = kv . Value ;
182191
183192 foreach ( var kv in method . Url . Params )
184193 {
185- if ( overrides . SkipQueryStringParams . Contains ( kv . Key ) )
194+ if ( skipList . Contains ( kv . Key ) )
195+ {
186196 method . Url . Params . Remove ( kv . Key ) ;
187-
197+ continue ;
198+ }
188199
189200 string newName ;
190- if ( ! qs . TryGetValue ( kv . Key , out newName ) )
201+ if ( ! renameList . TryGetValue ( kv . Key , out newName ) )
191202 continue ;
192203
193204 method . Url . Params . Remove ( kv . Key ) ;
194205 method . Url . Params . Add ( newName , kv . Value ) ;
195-
206+
196207 }
197208 }
198- // ReSharper disable once EmptyGeneralCatchClause
199- catch
209+ // ReSharper disable once EmptyGeneralCatchClause
210+ catch
200211 {
201212 }
202213
@@ -262,12 +273,12 @@ public static void GenerateEnums(RestApiSpec model)
262273 File . WriteAllText ( targetFile , source ) ;
263274 }
264275
265- private static void WriteToCache ( string filename , string contents )
266- {
267- if ( ! Directory . Exists ( _cacheFolder ) )
268- Directory . CreateDirectory ( _cacheFolder ) ;
276+ private static void WriteToCache ( string filename , string contents )
277+ {
278+ if ( ! Directory . Exists ( _cacheFolder ) )
279+ Directory . CreateDirectory ( _cacheFolder ) ;
269280
270- File . WriteAllText ( _cacheFolder + filename , contents ) ;
271- }
281+ File . WriteAllText ( _cacheFolder + filename , contents ) ;
282+ }
272283 }
273284}
0 commit comments