/// <summary> /// Create a new YouMail object /// </summary> /// <param name="username">The username to login</param> /// <param name="password">The user's password</param> /// <param name="authToken">An authentication token to user</param> /// <param name="userAgent">The UserAgent to use for the web requests</param> /// <param name="responseFormat">The format of the response, JSON or XML</param> /// <param name="secureConnections">Flag to specify if we use secure connections for our requests</param> public YouMailService( string username, string password, string authToken, string userAgent, ResponseFormat responseFormat = ResponseFormat.JSON, bool secureConnections = true) { _username = username; _password = password; _userAgent = userAgent; _responseFormat = responseFormat; // Create the HttpClient before setting the AuthToken var handler = new HttpClientHandler { AllowAutoRedirect = false }; _httpClient = new HttpClient(handler); _httpClient.DefaultRequestHeaders.Add("User-Agent", _userAgent); // Set the response format for the requests if (_responseFormat == ResponseFormat.JSON) { _responseFormatString = "application/json"; } else if (_responseFormat == ResponseFormat.XML) { _responseFormatString = "application/xml"; } else { throw new InvalidOperationException(string.Format("Unsupported ResponseFormat: {0}, is not supported.", _responseFormat.ToString())); } _httpClient.DefaultRequestHeaders.Add("Accept", ResponseFormatString); if (s_gzipSupported) { _httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "gzip,defalte"); } AuthToken = authToken; SecureConnections = secureConnections; #if WINDOWS_UWP NetworkInformation.NetworkStatusChanged += NetworkStatusChanged; #endif _disconnectedCalls = new YouMailAPICalls(); InvalidateNetworkStatus(); }
private async Task SaveAPICallsToFileAsync(YouMailAPICalls APICalls) { try { var disconnectedFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync( DisconnectedFileName, CreationCollisionOption.ReplaceExisting); using (var stream = await disconnectedFile.OpenStreamForWriteAsync()) { APICalls.ToXml(stream); } } catch { } }
private async Task FlushCachedAPICallsAsync() { bool fReleaseSemaphore = false; AddPendingOp(); StorageFile storageFile = null; YouMailAPICalls APICalls = null; try { await _disconnectedSemaphore.WaitAsync(); fReleaseSemaphore = true; // We are now connected. Clear the disconnected calls and replay the new ones. _disconnectedCalls.APICalls.Clear(); storageFile = await ApplicationData.Current.TemporaryFolder.GetFileAsync(DisconnectedFileName); using (var stream = await ApplicationData.Current.TemporaryFolder.OpenStreamForReadAsync(DisconnectedFileName)) { try { APICalls = stream.FromXml <YouMailAPICalls>(); } catch { } } try { // Delete the file now await storageFile.DeleteAsync(); } catch { } _disconnectedSemaphore.Release(); fReleaseSemaphore = false; if (APICalls != null && APICalls.APICalls != null) { foreach (var call in APICalls.APICalls) { try { StringContent content = null; if (call.Data != null) { // For now expect only to serialize strings content = new StringContent(call.Data, Encoding.UTF8, "text/xml"); } await YouMailApiAsync(call.URL, content, call.Verb, call.Auth); } catch { } } } } catch { // Something wrong happened. Ignore the failure } finally { if (fReleaseSemaphore) { _disconnectedSemaphore.Release(); } RemovePendingOp(); } }