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