2018-05-22 10:11:50 +02:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.IO ;
using System.Net ;
using System.Net.Security ;
using System.Security.Cryptography.X509Certificates ;
using System.Net.Http ;
using System.Text ;
using System.Threading.Tasks ;
using Core.Main ;
using Core.Helper ;
using Core.Main.DataObjects.PTMagicData ;
using Newtonsoft.Json ;
2018-12-15 22:07:29 +01:00
namespace Core.ProfitTrailer
{
public static class SettingsAPI
{
2018-12-27 15:04:10 +01:00
public static void GetInitialProfitTrailerSettings ( PTMagicConfiguration systemConfiguration )
{
string html = "" ;
string url = systemConfiguration . GeneralSettings . Application . ProfitTrailerMonitorURL + "api/data?token=" + systemConfiguration . GeneralSettings . Application . ProfitTrailerServerAPIToken ;
try
{
HttpWebRequest request = ( HttpWebRequest ) WebRequest . Create ( url ) ;
request . AutomaticDecompression = DecompressionMethods . GZip ;
WebResponse response = request . GetResponse ( ) ;
Stream dataStream = response . GetResponseStream ( ) ;
StreamReader reader = new StreamReader ( dataStream ) ;
html = reader . ReadToEnd ( ) ;
reader . Close ( ) ;
response . Close ( ) ;
}
catch ( System . Exception )
{
throw ;
}
dynamic json = JsonConvert . DeserializeObject ( html ) ;
systemConfiguration . GeneralSettings . Application . Exchange = json . exchange ;
systemConfiguration . GeneralSettings . Application . TimezoneOffset = json . timeZoneOffset ;
systemConfiguration . GeneralSettings . Application . StartBalance = json . startBalance ;
2018-12-29 04:09:00 +01:00
systemConfiguration . GeneralSettings . Application . MainFiatCurrency = json . settings . currency ;
2018-12-27 15:04:10 +01:00
}
2018-12-15 22:07:29 +01:00
public static List < string > GetPropertyLinesFromAPI ( string ptFileName , PTMagicConfiguration systemConfiguration , LogHelper log )
{
2018-05-22 10:11:50 +02:00
List < string > result = null ;
2018-12-15 22:07:29 +01:00
try
{
2018-05-22 10:11:50 +02:00
ServicePointManager . Expect100Continue = true ;
ServicePointManager . DefaultConnectionLimit = 9999 ;
ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls | SecurityProtocolType . Tls11 | SecurityProtocolType . Tls12 ;
ServicePointManager . ServerCertificateValidationCallback + = new RemoteCertificateValidationCallback ( CertificateHelper . AllwaysGoodCertificate ) ;
HttpWebRequest httpWebRequest = ( HttpWebRequest ) WebRequest . Create ( systemConfiguration . GeneralSettings . Application . ProfitTrailerMonitorURL + "settingsapi/settings/load" ) ;
httpWebRequest . ContentType = "application/x-www-form-urlencoded" ;
httpWebRequest . Method = "POST" ;
// PT is using ordinary POST data, not JSON
string query = "fileName=" + ptFileName + "&configName=" + systemConfiguration . GeneralSettings . Application . ProfitTrailerDefaultSettingName + "&license=" + systemConfiguration . GeneralSettings . Application . ProfitTrailerLicense ;
byte [ ] formData = Encoding . ASCII . GetBytes ( query ) ;
httpWebRequest . ContentLength = formData . Length ;
2018-12-15 22:07:29 +01:00
using ( Stream stream = httpWebRequest . GetRequestStream ( ) )
{
2018-05-22 10:11:50 +02:00
stream . Write ( formData , 0 , formData . Length ) ;
}
//using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) {
// string json = JsonConvert.SerializeObject(new {
// fileName = ptFileName,
// configName = systemConfiguration.GeneralSettings.Application.ProfitTrailerDefaultSettingName,
// license = systemConfiguration.GeneralSettings.Application.ProfitTrailerLicense
// });
// streamWriter.Write(json);
//}
HttpWebResponse httpResponse = ( HttpWebResponse ) httpWebRequest . GetResponse ( ) ;
2018-12-15 22:07:29 +01:00
using ( StreamReader streamReader = new StreamReader ( httpResponse . GetResponseStream ( ) ) )
{
2018-05-22 10:11:50 +02:00
string jsonResult = streamReader . ReadToEnd ( ) ;
result = JsonConvert . DeserializeObject < List < string > > ( jsonResult ) ;
}
2018-12-15 22:07:29 +01:00
}
catch ( WebException ex )
{
2018-05-22 10:11:50 +02:00
// Manual error handling as PT doesn't seem to provide a proper error response...
2018-12-15 22:07:29 +01:00
if ( ex . Message . IndexOf ( "401" ) > - 1 )
{
2018-05-22 10:11:50 +02:00
log . DoLogError ( "Loading " + ptFileName + ".properties failed for setting '" + systemConfiguration . GeneralSettings . Application . ProfitTrailerDefaultSettingName + "': Unauthorized! The specified Profit Trailer license key '" + systemConfiguration . GetProfitTrailerLicenseKeyMasked ( ) + "' is invalid!" ) ;
2018-12-15 22:07:29 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
log . DoLogCritical ( "Loading " + ptFileName + ".properties failed for setting '" + systemConfiguration . GeneralSettings . Application . ProfitTrailerDefaultSettingName + "': " + ex . Message , ex ) ;
}
2018-12-15 22:07:29 +01:00
}
catch ( Exception ex )
{
2018-05-22 10:11:50 +02:00
log . DoLogCritical ( "Loading " + ptFileName + ".properties failed for setting '" + systemConfiguration . GeneralSettings . Application . ProfitTrailerDefaultSettingName + "': " + ex . Message , ex ) ;
}
return result ;
}
2018-12-16 15:22:38 +01:00
public static void SendPropertyLinesToAPI ( List < string > pairsLines , List < string > dcaLines , List < string > indicatorsLines , PTMagicConfiguration systemConfiguration , LogHelper log )
2018-12-15 22:07:29 +01:00
{
2018-05-22 10:11:50 +02:00
int retryCount = 0 ;
int maxRetries = 3 ;
bool transferCompleted = false ;
bool transferCanceled = false ;
2018-12-15 22:07:29 +01:00
while ( ! transferCompleted & & ! transferCanceled )
{
try
{
2018-05-22 10:11:50 +02:00
ServicePointManager . Expect100Continue = true ;
ServicePointManager . DefaultConnectionLimit = 9999 ;
ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls | SecurityProtocolType . Tls11 | SecurityProtocolType . Tls12 ;
ServicePointManager . ServerCertificateValidationCallback + = new RemoteCertificateValidationCallback ( CertificateHelper . AllwaysGoodCertificate ) ;
2018-12-16 15:22:38 +01:00
HttpWebRequest httpWebRequest = ( HttpWebRequest ) WebRequest . Create ( systemConfiguration . GeneralSettings . Application . ProfitTrailerMonitorURL + "settingsapi/settings/saveAll" ) ;
2018-05-22 10:11:50 +02:00
httpWebRequest . ContentType = "application/x-www-form-urlencoded" ;
httpWebRequest . Method = "POST" ;
httpWebRequest . Proxy = null ;
httpWebRequest . Timeout = 30000 ;
// PT is using ordinary POST data, not JSON
2018-12-16 15:22:38 +01:00
string query = "configName=" + systemConfiguration . GeneralSettings . Application . ProfitTrailerDefaultSettingName + "&license=" + systemConfiguration . GeneralSettings . Application . ProfitTrailerLicense ;
string pairsPropertiesString = SystemHelper . ConvertListToTokenString ( pairsLines , Environment . NewLine , false ) ;
string dcaPropertiesString = SystemHelper . ConvertListToTokenString ( dcaLines , Environment . NewLine , false ) ;
string indicatorsPropertiesString = SystemHelper . ConvertListToTokenString ( indicatorsLines , Environment . NewLine , false ) ;
query + = "&pairsData=" + WebUtility . UrlEncode ( pairsPropertiesString ) + "&dcaData=" + WebUtility . UrlEncode ( dcaPropertiesString ) + "&indicatorsData=" + WebUtility . UrlEncode ( indicatorsPropertiesString ) ;
2018-05-22 10:11:50 +02:00
byte [ ] formData = Encoding . ASCII . GetBytes ( query ) ;
httpWebRequest . ContentLength = formData . Length ;
2018-12-15 22:07:29 +01:00
using ( Stream stream = httpWebRequest . GetRequestStream ( ) )
{
2018-05-22 10:11:50 +02:00
stream . Write ( formData , 0 , formData . Length ) ;
}
2018-12-16 15:22:38 +01:00
log . DoLogDebug ( "Built POST request for Properties" ) ;
2018-05-22 10:11:50 +02:00
//using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) {
// string json = JsonConvert.SerializeObject(new {
// fileName = ptFileName,
// configName = systemConfiguration.GeneralSettings.Application.ProfitTrailerDefaultSettingName,
// license = systemConfiguration.GeneralSettings.Application.ProfitTrailerLicense,
// saveData = propertiesString
// });
// streamWriter.Write(json);
//}
2018-12-16 15:22:38 +01:00
log . DoLogInfo ( "Sending Properties..." ) ;
2018-05-22 10:11:50 +02:00
HttpWebResponse httpResponse = ( HttpWebResponse ) httpWebRequest . GetResponse ( ) ;
2018-12-16 15:22:38 +01:00
log . DoLogInfo ( "Properties sent!" ) ;
2018-05-22 10:11:50 +02:00
httpResponse . Close ( ) ;
2018-12-16 15:22:38 +01:00
log . DoLogDebug ( "Properties response object closed." ) ;
2018-05-22 10:11:50 +02:00
transferCompleted = true ;
2018-12-15 22:07:29 +01:00
}
catch ( WebException ex )
{
2018-05-22 10:11:50 +02:00
// Manual error handling as PT doesn't seem to provide a proper error response...
2018-12-15 22:07:29 +01:00
if ( ex . Message . IndexOf ( "401" ) > - 1 )
{
2018-12-16 15:22:38 +01:00
log . DoLogError ( "Saving Properties failed for setting '" + systemConfiguration . GeneralSettings . Application . ProfitTrailerDefaultSettingName + "': Unauthorized! The specified Profit Trailer license key '" + systemConfiguration . GetProfitTrailerLicenseKeyMasked ( ) + "' is invalid!" ) ;
2018-05-22 10:11:50 +02:00
transferCanceled = true ;
2018-12-15 22:07:29 +01:00
}
else if ( ex . Message . IndexOf ( "timed out" ) > - 1 )
{
2018-05-22 10:11:50 +02:00
// Handle timeout seperately
retryCount + + ;
2018-12-15 22:07:29 +01:00
if ( retryCount < = maxRetries )
{
2018-12-16 15:22:38 +01:00
log . DoLogError ( "Saving Properties failed for setting '" + systemConfiguration . GeneralSettings . Application . ProfitTrailerDefaultSettingName + "': Timeout! Starting retry number " + retryCount + "/" + maxRetries . ToString ( ) + "!" ) ;
2018-12-15 22:07:29 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
transferCanceled = true ;
2018-12-16 15:22:38 +01:00
log . DoLogError ( "Saving Properties failed for setting '" + systemConfiguration . GeneralSettings . Application . ProfitTrailerDefaultSettingName + "': Timeout! Canceling transfer after " + maxRetries . ToString ( ) + " failed retries." ) ;
2018-05-22 10:11:50 +02:00
}
2018-12-15 22:07:29 +01:00
}
else
{
2018-12-16 15:22:38 +01:00
log . DoLogCritical ( "Saving Properties failed for setting '" + systemConfiguration . GeneralSettings . Application . ProfitTrailerDefaultSettingName + "': " + ex . Message , ex ) ;
2018-05-22 10:11:50 +02:00
transferCanceled = true ;
}
2018-12-15 22:07:29 +01:00
}
catch ( TimeoutException ex )
{
2018-05-22 10:11:50 +02:00
retryCount + + ;
2018-12-15 22:07:29 +01:00
if ( retryCount < = maxRetries )
{
2018-12-16 15:22:38 +01:00
log . DoLogError ( "Saving Properties failed for setting '" + systemConfiguration . GeneralSettings . Application . ProfitTrailerDefaultSettingName + "': Timeout (" + ex . Message + ")! Starting retry number " + retryCount + "/" + maxRetries . ToString ( ) + "!" ) ;
2018-12-15 22:07:29 +01:00
}
else
{
2018-05-22 10:11:50 +02:00
transferCanceled = true ;
2018-12-16 15:22:38 +01:00
log . DoLogError ( "Saving Properties failed for setting '" + systemConfiguration . GeneralSettings . Application . ProfitTrailerDefaultSettingName + "': Timeout (" + ex . Message + ")! Canceling transfer after " + maxRetries . ToString ( ) + " failed retries." ) ;
2018-05-22 10:11:50 +02:00
}
2018-12-15 22:07:29 +01:00
}
catch ( Exception ex )
{
2018-12-16 15:22:38 +01:00
log . DoLogCritical ( "Saving Properties failed for setting '" + systemConfiguration . GeneralSettings . Application . ProfitTrailerDefaultSettingName + "': " + ex . Message , ex ) ;
2018-05-22 10:11:50 +02:00
transferCanceled = true ;
}
}
}
}
}