@@ -38,22 +38,28 @@ public class GithubRest
3838 private const string ApiUrl = "https://api.github.com" ;
3939 private const string AcceptHeader = "application/vnd.github.v3+json" ;
4040
41- private static async Task < JObject > SendRequestAsync ( HttpMethod method , string endpoint , string extra = null )
41+ private static async Task < JObject > SendRequestAsync ( HttpMethod method , string endpoint , string extra = null , string acceptHeader = null )
4242 {
4343 using HttpClient http = new ( ) ;
4444 HttpRequestMessage request = new ( method , $ "{ ApiUrl } { endpoint } { extra } ") ;
45- request . Headers . Add ( "Accept" , AcceptHeader ) ;
45+ if ( acceptHeader == null )
46+ {
47+ acceptHeader = "application/vnd.github.v3+json" ;
48+ }
49+
50+ request . Headers . Add ( "Accept" , acceptHeader ) ;
4651 request . Headers . Add ( "User-Agent" , "DSharpPlus Docs Bot/1.0" ) ;
52+ request . Headers . Add ( "Authorization" , $ "Bearer { Environment . GetEnvironmentVariable ( "GITHUB_TOKEN" ) } ") ;
4753 HttpResponseMessage response = await http . SendAsync ( request ) ;
4854 return response . IsSuccessStatusCode
4955 ? JObject . Parse ( await response . Content . ReadAsStringAsync ( ) )
5056 : throw new InvalidOperationException ( $ "{ response . ReasonPhrase } : { await response . Content . ReadAsStringAsync ( ) } ") ;
5157 }
5258
53- public static async Task < List < GitSearchResult > > SearchAsync ( string search , string filename = null )
59+ private static async Task < List < GitSearchResult > > SearchAsync ( string search , string filename = null )
5460 {
55- string extra = $ "?q=repo:DSharpPlus/DSharpPlus+language:cs +in:file{ ( filename == null ? "" : $ "+filename:{ filename } ") } +{ search . Replace ( ' ' , '+' ) } &per_page=100";
56- JObject result = await SendRequestAsync ( HttpMethod . Get , "/search/code" , extra ) ;
61+ string extra = $ "?q={ search . Replace ( ' ' , '+' ) } + repo:DSharpPlus/DSharpPlus+language:c# +in:file{ ( filename == null ? "" : $ "+filename:{ filename } ") } +{ search . Replace ( ' ' , '+' ) } &per_page=100";
62+ JObject result = await SendRequestAsync ( HttpMethod . Get , "/search/code" , extra , "application/vnd.github+json" ) ;
5763 JArray items = ( JArray ) result [ "items" ] ;
5864 List < GitSearchResult > list = new ( ) ;
5965 foreach ( JToken item in items )
@@ -67,7 +73,7 @@ public static async Task<List<GitSearchResult>> SearchAsync(string search, strin
6773 int pages = ( int ) Math . Floor ( totalCount / 100f ) ;
6874 for ( int i = 2 ; i <= pages + 1 ; i ++ )
6975 {
70- extra = $ "?q=repo:DSharpPlus/DSharpPlus+language:cs+in:file{ ( filename == null ? "" : $ "+filename:{ filename } ") } +{ search . Replace ( ' ' , '+' ) } &per_page=100&page={ i } ";
76+ extra = $ "?q={ search . Replace ( ' ' , '+' ) } +repo:DSharpPlus/DSharpPlus+?q= repo:DSharpPlus/DSharpPlus+language:cs+in:file{ ( filename == null ? "" : $ "+filename:{ filename } ") } +{ search . Replace ( ' ' , '+' ) } &per_page=100&page={ i } ";
7177 result = await SendRequestAsync ( HttpMethod . Get , "/search/code" , extra ) ;
7278 items = ( JArray ) result [ "items" ] ;
7379 foreach ( JToken item in items )
@@ -76,6 +82,7 @@ public static async Task<List<GitSearchResult>> SearchAsync(string search, strin
7682 }
7783 }
7884 }
85+
7986 return list ;
8087 }
8188
0 commit comments