public static JObject GetPurchasesResult(EnjinHttpClient httpClient, int epoch) { var body = new EnjinRequestBody() { jsonrpc = "2.0", id = Random.Next().ToString(), parameters = new EnjinRequestBody.Params() { api_key = pluginInstance.Configuration.Instance.APIKey, preset_id = pluginInstance.Configuration.Instance.PresetId.ToString(), date_start = epoch.ToString() }, method = "Shop.getPurchases" }; var response = httpClient.SendRequest(body); if (response["id"].ToString() != body.id) { Logger.LogWarning($"Response ID didn't match the request ID"); return(null); } return(response); }
public JObject SendRequest(EnjinRequestBody body) { var content = JsonConvert.SerializeObject(body); var data = Encoding.ASCII.GetBytes(content); var request = WebRequest.CreateHttp(APIUrl); // without user agent enjin returns Forbidden (403) request.UserAgent = "CustomEnjinAPI.Agent"; request.Method = "POST"; request.ContentType = "application/json"; request.ContentLength = data.Length; request.Timeout = pluginInstance.Configuration.Instance.RequestTimeoutMiliseconds; try { using (var stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } } catch (WebException e) { Logger.LogException(e, $"An exception occurated while writing enjin request body for method {body.method}"); return(null); } string responseContent; try { using (var response = request.GetResponse()) { using (var stream = response.GetResponseStream()) { var reader = new StreamReader(stream); responseContent = reader.ReadToEnd(); } } } catch (WebException e) { Logger.LogException(e, $"An exception occurated while reading enjin response body for method {body.method}"); return(null); } return(JObject.Parse(responseContent)); }