1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . IO ;
4+ using System . Net ;
5+ using System . Text . Json ;
6+ using System . Web ;
7+ using ComputerUtils . Android . Logging ;
8+ using OculusGraphQLApiLib . Results ;
9+
10+ namespace OculusGraphQLApiLib
11+ {
12+ public class GraphQLClient
13+ {
14+ public string uri { get ; set ; } = "" ;
15+ public GraphQLOptions options { get ; set ; } = new GraphQLOptions ( ) ;
16+ public const string oculusUri = "https://graph.oculus.com/graphql" ;
17+ public static string oculusStoreToken = "OC|752908224809889|" ;
18+ public static string forcedLocale = "" ;
19+ public static bool throwException = true ;
20+ public static bool log = true ;
21+ public static int retryTimes = 3 ;
22+ public static JsonSerializerOptions jsonOptions = new JsonSerializerOptions
23+ {
24+ DefaultIgnoreCondition = System . Text . Json . Serialization . JsonIgnoreCondition . WhenWritingNull
25+ } ;
26+
27+ public GraphQLClient ( string uri , GraphQLOptions options )
28+ {
29+ this . uri = uri ;
30+ this . options = options ;
31+ }
32+
33+ public GraphQLClient ( string uri )
34+ {
35+ this . uri = uri ;
36+ }
37+
38+ public GraphQLClient ( ) { }
39+
40+ public string GetForcedLocale ( )
41+ {
42+ return forcedLocale != "" ? "?forced_locale=" + forcedLocale : "" ;
43+ }
44+
45+ public string Request ( GraphQLOptions options )
46+ {
47+ WebClient c = new WebClient ( ) ;
48+ //c.Headers.Add("x-requested-with", "RiftDowngrader");
49+ if ( log ) Logger . Log ( "Doing POST Request to " + uri + " with args " + options . ToLoggingString ( ) ) ;
50+ try
51+ {
52+ string returning = c . UploadString ( uri + GetForcedLocale ( ) , "POST" , options . ToStringEncoded ( ) ) ;
53+ return returning ;
54+ }
55+ catch ( WebException e )
56+ {
57+ if ( log ) Logger . Log ( "Request failed (" + e . Status + "): \n " + new StreamReader ( e . Response . GetResponseStream ( ) ) . ReadToEnd ( ) , LoggingType . Error ) ;
58+ Console . ForegroundColor = ConsoleColor . Red ;
59+ if ( log ) Console . WriteLine ( "Request to Oculus failed. Please try again later and/or contact ComputerElite." ) ;
60+ if ( throwException ) throw new Exception ( e . Status . ToString ( ) . StartsWith ( "4" ) ? "I fuqed up" : "Some Request to Oculus failed so yeah idk how to handle it." ) ;
61+ }
62+ return "{}" ;
63+ }
64+
65+ public string Request ( bool asBody = false , Dictionary < string , string > customHeaders = null , int retry = 0 , string status = "200" )
66+ {
67+ if ( retry == retryTimes )
68+ {
69+ if ( log ) Logger . Log ( "Retry limit exceeded. Stopping requests" ) ;
70+ Console . ForegroundColor = ConsoleColor . Red ;
71+ if ( log ) Console . WriteLine ( "Request to Oculus failed. Please try again later and/or contact ComputerElite." ) ;
72+ if ( throwException ) throw new Exception ( status . StartsWith ( "4" ) ? "I fuqed up" : "Some Request to Oculus failed so yeah idk how to handle it." ) ;
73+ return "{}" ;
74+ }
75+ if ( log && retry != 0 ) Logger . Log ( "Starting retry number " + retry ) ;
76+ WebClient c = new WebClient ( ) ;
77+ if ( customHeaders != null )
78+ {
79+ foreach ( KeyValuePair < string , string > header in customHeaders )
80+ {
81+ c . Headers . Add ( header . Key , header . Value ) ;
82+ }
83+ }
84+ if ( log ) Logger . Log ( "Doing POST Request to " + uri + " with args " + options . ToLoggingString ( ) ) ;
85+ try
86+ {
87+ string res = "" ;
88+ if ( asBody ) res = c . UploadString ( uri + GetForcedLocale ( ) , "POST" , options . ToStringEncoded ( ) ) ;
89+ else res = c . UploadString ( uri + "?" + this . options . ToString ( ) + GetForcedLocale ( ) . Replace ( "?" , "&" ) , "POST" , "" ) ;
90+ if ( log ) Logger . Log ( res ) ;
91+ return res ;
92+ }
93+ catch ( WebException e )
94+ {
95+
96+ if ( log ) Logger . Log ( "Request failed, retrying (" + e . Status . ToString ( ) + ", " + ( int ) e . Status + "): \n " + new StreamReader ( e . Response . GetResponseStream ( ) ) . ReadToEnd ( ) , LoggingType . Error ) ;
97+ return Request ( asBody , customHeaders , retry + 1 , e . Status . ToString ( ) ) ;
98+ }
99+ return "{}" ;
100+ }
101+
102+ public static Data < Application > VersionHistory ( string appid )
103+ {
104+ GraphQLClient c = OculusTemplate ( ) ;
105+ c . options . doc_id = "1586217024733717" ;
106+ c . options . variables = "{\" id\" :\" " + appid + "\" }" ;
107+ return JsonSerializer . Deserialize < Data < Application > > ( c . Request ( ) , jsonOptions ) ;
108+ }
109+
110+ public static ViewerData < OculusUserWrapper > GetCurrentUser ( )
111+ {
112+ GraphQLClient c = OculusTemplate ( ) ;
113+ c . options . doc_id = "4149322231793299" ;
114+ c . options . variables = "{}" ;
115+ return JsonSerializer . Deserialize < ViewerData < OculusUserWrapper > > ( c . Request ( ) , jsonOptions ) ;
116+ }
117+
118+ public static Data < AppStoreAllAppsSection > AllApps ( Headset headset , string cursor = null , int maxApps = 500 )
119+ {
120+ GraphQLClient c = OculusTemplate ( ) ;
121+ c . options . doc_id = "3821696797949516" ;
122+ string id = "" ;
123+ switch ( headset )
124+ {
125+ case Headset . MONTEREY :
126+ id = "1888816384764129" ;
127+ break ;
128+ case Headset . HOLLYWOOD :
129+ id = "1888816384764129" ;
130+ break ;
131+ case Headset . RIFT :
132+ id = "1736210353282450" ;
133+ break ;
134+ case Headset . LAGUNA :
135+ id = "1736210353282450" ;
136+ break ;
137+ case Headset . GEARVR :
138+ id = "174868819587665" ;
139+ break ;
140+ case Headset . PACIFIC :
141+ id = "174868819587665" ;
142+ break ;
143+ }
144+ c . options . variables = "{\" sectionId\" :\" " + id + "\" ,\" sortOrder\" :null,\" sectionItemCount\" :" + maxApps + ",\" sectionCursor\" :" + ( cursor == null ? "null" : "\" " + cursor + "\" " ) + ",\" hmdType\" :\" " + HeadsetTools . GetHeadsetCodeName ( headset ) + "\" }" ;
145+ return JsonSerializer . Deserialize < Data < AppStoreAllAppsSection > > ( c . Request ( ) ) ;
146+ }
147+
148+ public static Data < NodesPrimaryBinaryApplication > AllVersionsOfApp ( string appid ) // DONE
149+ {
150+ GraphQLClient c = OculusTemplate ( ) ;
151+ c . options . doc_id = "2885322071572384" ;
152+ c . options . variables = "{\" applicationID\" :\" " + appid + "\" }" ;
153+ return JsonSerializer . Deserialize < Data < NodesPrimaryBinaryApplication > > ( c . Request ( ) , jsonOptions ) ;
154+ }
155+
156+ public static ViewerData < OculusUserWrapper > GetActiveEntitelments ( ) // DONE
157+ {
158+ GraphQLClient c = OculusTemplate ( ) ;
159+ c . options . doc_id = "4850747515044496" ;
160+ c . options . variables = "{}" ;
161+ return JsonSerializer . Deserialize < ViewerData < OculusUserWrapper > > ( c . Request ( ) , jsonOptions ) ;
162+ }
163+
164+ public static Data < EdgesPrimaryBinaryApplication > ReleaseChannelsOfApp ( string appid ) // DONE
165+ {
166+ GraphQLClient c = OculusTemplate ( ) ;
167+ c . options . doc_id = "3828663700542720" ;
168+ c . options . variables = "{\" applicationID\" :\" " + appid + "\" }" ;
169+ return JsonSerializer . Deserialize < Data < EdgesPrimaryBinaryApplication > > ( c . Request ( ) , jsonOptions ) ;
170+ }
171+
172+ public static Data < ReleaseChannel > ReleaseChannelReleases ( string channelId ) // DONE
173+ {
174+ GraphQLClient c = OculusTemplate ( ) ;
175+ c . options . doc_id = "3973666182694273" ;
176+ c . options . variables = "{\" releaseChannelID\" :\" " + channelId + "\" }" ;
177+ return JsonSerializer . Deserialize < Data < ReleaseChannel > > ( c . Request ( ) , jsonOptions ) ;
178+ }
179+
180+ public static ViewerData < ContextualSearch > StoreSearch ( string query , Headset headset ) // DONE
181+ {
182+ GraphQLClient c = OculusTemplate ( ) ;
183+ c . options . doc_id = "3928907833885295" ;
184+ c . options . variables = "{\" query\" :\" " + query + "\" ,\" hmdType\" :\" " + HeadsetTools . GetHeadsetCodeName ( headset ) + "\" ,\" firstSearchResultItems\" :100}" ;
185+ return JsonSerializer . Deserialize < ViewerData < ContextualSearch > > ( c . Request ( ) , jsonOptions ) ;
186+ }
187+
188+ public static GraphQLClient CurrentVersionOfApp ( string appid )
189+ {
190+ GraphQLClient c = OculusTemplate ( ) ;
191+ c . options . doc_id = "1586217024733717" ;
192+ c . options . variables = "{\" id\" :\" " + appid + "\" }" ;
193+ return c ;
194+ }
195+
196+ public static Data < Application > GetAppDetail ( string id , Headset headset )
197+ {
198+ GraphQLClient c = OculusTemplate ( ) ;
199+ c . options . doc_id = "4282918028433524" ;
200+ c . options . variables = "{\" itemId\" :\" " + id + "\" ,\" first\" :20,\" last\" :null,\" after\" :null,\" before\" :null,\" forward\" :true,\" ordering\" :null,\" ratingScores\" :null,\" hmdType\" :\" " + HeadsetTools . GetHeadsetCodeName ( headset ) + "\" }" ;
201+ return JsonSerializer . Deserialize < Data < Application > > ( c . Request ( ) , jsonOptions ) ;
202+ }
203+
204+ public static Data < Application > GetDLCs ( string appId )
205+ {
206+ GraphQLClient c = OculusTemplate ( ) ;
207+ c . options . doc_id = "3853229151363174" ;
208+ c . options . variables = "{\" id\" :\" " + appId + "\" ,\" first\" :200,\" last\" :null,\" after\" :null,\" before\" :null,\" forward\" :true}" ;
209+ return JsonSerializer . Deserialize < Data < Application > > ( c . Request ( ) , jsonOptions ) ;
210+ }
211+
212+ public static Data < AndroidBinary > GetBinaryDetails ( string binaryId )
213+ {
214+ GraphQLClient c = OculusTemplate ( ) ;
215+ c . options . doc_id = "4734929166632773" ;
216+ c . options . variables = "{\" binaryID\" :\" " + binaryId + "\" }" ;
217+ return JsonSerializer . Deserialize < Data < AndroidBinary > > ( c . Request ( ) , jsonOptions ) ;
218+ }
219+
220+ public static PlainData < AppBinaryInfoContainer > GetAssetFiles ( string appId , long versionCode )
221+ {
222+ GraphQLClient c = OculusTemplate ( ) ;
223+ c . options . doc = "query ($params: AppBinaryInfoArgs!) { app_binary_info(args: $params) { info { binary { ... on AndroidBinary { id package_name version_code asset_files { edges { node { ... on AssetFile { file_name uri size } } } } } } } }}" ;
224+ c . options . variables = "{\" params\" :{\" app_params\" :[{\" app_id\" :\" " + appId + "\" ,\" version_code\" :\" " + versionCode + "\" }]}}" ;
225+ return JsonSerializer . Deserialize < PlainData < AppBinaryInfoContainer > > ( c . Request ( ) , jsonOptions ) ;
226+ }
227+
228+ public static GraphQLClient OculusTemplate ( )
229+ {
230+ GraphQLClient c = new GraphQLClient ( oculusUri ) ;
231+ GraphQLOptions o = new GraphQLOptions ( ) ;
232+ o . access_token = oculusStoreToken ;
233+ c . options = o ;
234+ return c ;
235+ }
236+ }
237+
238+ public class GraphQLOptions
239+ {
240+ public string access_token { get ; set ; } = "" ;
241+ public string variables { get ; set ; } = "" ;
242+ public string doc_id { get ; set ; } = "" ;
243+ public string doc { get ; set ; } = "" ;
244+
245+ public override string ToString ( )
246+ {
247+ return "access_token=" + access_token + "&variables=" + variables + "&doc_id=" + doc_id + "&doc=" + doc ;
248+ }
249+
250+ public string ToStringEncoded ( )
251+ {
252+ return "access_token=" + access_token + "&variables=" + variables + "&doc_id=" + doc_id + "&doc=" + doc ;
253+ }
254+
255+ public string ToLoggingString ( )
256+ {
257+ return "access_token=aSecret:)&variables=" + variables + "&doc_id=" + doc_id + "&doc=" + doc ;
258+ }
259+ } }
0 commit comments