diff --git a/BookAStar/BookAStar.Droid/Helpers/SimpleJsonPostMethod.cs b/BookAStar/BookAStar.Droid/Helpers/SimpleJsonPostMethod.cs new file mode 100644 index 00000000..7b200659 --- /dev/null +++ b/BookAStar/BookAStar.Droid/Helpers/SimpleJsonPostMethod.cs @@ -0,0 +1,103 @@ +// +// PostJson.cs +// +// Author: +// Paul Schneider +// +// Copyright (c) 2015 Paul Schneider +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +using System.Net; +using System.IO; +using System.Json; +using System.Threading.Tasks; +using Newtonsoft.Json; +using System; + +namespace Yavsc.Helpers +{ + /// + /// Simple json post method. + /// + public class SimpleJsonPostMethod : IDisposable + { + private HttpWebRequest request=null; + + /// + /// Initializes a new instance of the Yavsc.Helpers.SimpleJsonPostMethod class. + /// + /// Path to method. + public SimpleJsonPostMethod (string pathToMethod, string authorizationHeader = null) + { + request = (HttpWebRequest) WebRequest.Create ( + BasePath + pathToMethod); + request.Method = "POST"; + request.Accept = "application/json"; + request.ContentType = "application/json"; + request.SendChunked = true; + request.TransferEncoding = "UTF-8"; + if (authorizationHeader!=null) + request.Headers.Add($"Authorization: Bearer {authorizationHeader}"); + } + + public static string BasePath { get; private set; } = "http://dev.pschneider.fr/api/"; + + public void Dispose() + { + request.Abort(); + } + + /// + /// Invoke the specified query. + /// + /// Query. + public TAnswer Invoke(object query) + { + + using (Stream streamQuery = request.GetRequestStream()) { + using (StreamWriter writer = new StreamWriter(streamQuery)) { + writer.Write (JsonConvert.SerializeObject(query)); + }} + TAnswer ans = default (TAnswer); + using (WebResponse response = request.GetResponse ()) { + using (Stream responseStream = response.GetResponseStream ()) { + using (StreamReader rdr = new StreamReader (responseStream)) { + ans = (TAnswer) JsonConvert.DeserializeObject (rdr.ReadToEnd ()); + } + } + response.Close(); + } + return ans; + } + + public async Task InvokeJson(object query) + { + + JsonValue jsonDoc=null; + using (Stream streamQuery = request.GetRequestStream()) { + using (StreamWriter writer = new StreamWriter(streamQuery)) { + writer.Write (JsonConvert.SerializeObject(query)); + }} + using (WebResponse response = request.GetResponse ()) { + using (Stream stream = response.GetResponseStream ()) { + if (stream.Length>0) + jsonDoc = await Task.Run (() => JsonObject.Load (stream)); + } + response.Close(); + } + return jsonDoc; + } + } +} + diff --git a/BookAStar/BookAStar.Droid/MainActivity.cs b/BookAStar/BookAStar.Droid/MainActivity.cs index 453da8df..834ee3b6 100644 --- a/BookAStar/BookAStar.Droid/MainActivity.cs +++ b/BookAStar/BookAStar.Droid/MainActivity.cs @@ -20,6 +20,8 @@ using System.Net.Http; using System.Text; using BookAStar.Model.Auth.Account; using BookAStar.Droid.OAuth; +using Yavsc.Helpers; +using Yavsc.Models.Identity; namespace BookAStar.Droid { @@ -256,24 +258,58 @@ namespace BookAStar.Droid { throw new NotImplementedException("Auth_Error"); } - - public GoogleCloudMobileDeclaration GetDeviceInfo() + public class GCMDeclaration : IGCMDeclaration + { + public string DeviceId + { get; set; } + + public string GCMRegistrationId + { get; set; } + + public string Model + { get; set; } + + public string Platform + { get; set; } + + public string Version + { get; set; } + } + + public IGCMDeclaration GetDeviceInfo() { var devinfo = DeviceInfo.Plugin.CrossDeviceInfo.Current; - return new GoogleCloudMobileDeclaration + return new GCMDeclaration { DeviceId = devinfo.Id, + GCMRegistrationId = MainSettings.GoogleRegId, Model = devinfo.Model, Platform = devinfo.Platform.ToString(), - Version = devinfo.Version, - GCMRegistrationId = MainSettings.GoogleRegId + Version = devinfo.Version }; } - public void PostDeviceInfo() + public TAnswer InvokeApi(string method, object arg) { - throw new NotImplementedException(); + using (var m = + new SimpleJsonPostMethod(method, + MainSettings.CurrentUser.YavscTokens.AccessToken + )) + { + return m.Invoke(arg); + } + } + + public object InvokeApi(string method, object arg) + { + using (var m = + new SimpleJsonPostMethod(method, + MainSettings.CurrentUser.YavscTokens.AccessToken + )) + { + return m.InvokeJson(arg); + } } } } diff --git a/BookAStar/BookAStar/App.xaml.cs b/BookAStar/BookAStar/App.xaml.cs index acacec48..412de277 100644 --- a/BookAStar/BookAStar/App.xaml.cs +++ b/BookAStar/BookAStar/App.xaml.cs @@ -8,11 +8,12 @@ using System.Text; using System.Threading.Tasks; using Xamarin.Forms; using Xamarin.Forms.Xaml; +using Yavsc.Helpers; /* Glyphish icons from - http://www.glyphish.com/ +http://www.glyphish.com/ under - http://creativecommons.org/licenses/by/3.0/us/ +http://creativecommons.org/licenses/by/3.0/us/ support them by buying the full set / Retina versions */ @@ -82,35 +83,12 @@ namespace BookAStar else deviceInfoPage.Focus(); } - public async void RegisterThisDevice() + public async Task PostDeviceInfo() { - HttpClient client = new HttpClient(); - // var request = new HttpRequestMessage(HttpMethod.Post, MainSettings.MobileRegistrationUrl); - // request.Headers.Authorization - client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", MainSettings.CurrentUser.YavscTokens.AccessToken); - /* client.DefaultRequestHeaders.Accept.Clear(); - client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json") - );*/ - - var info =PlateformSpecificInstance.GetDeviceInfo(); - - /* var serSettings = new JsonSerializerSettings(); serSettings.Formatting = Formatting.None; serSettings.StringEscapeHandling = StringEscapeHandling.EscapeNonAscii;*/ - - client.MaxResponseContentBufferSize = 256000; - using ( client) - { - var json = Newtonsoft.Json.JsonConvert.SerializeObject(info); - /* - var dataString = JsonConvert.SerializeObject(info,serSettings); */ - var content = new StringContent(json, Encoding.UTF8, "application/json"); - var response = await client.PostAsync(MainSettings.MobileRegistrationUrl, content); - if (response.IsSuccessStatusCode) - Debug.WriteLine(@"Device info successfully saved."); - - } + var res = PlateformSpecificInstance.InvokeApi( + "gcm/register", PlateformSpecificInstance.GetDeviceInfo()); } - } } diff --git a/BookAStar/BookAStar/BookAStar.csproj b/BookAStar/BookAStar/BookAStar.csproj index 45319b4c..37081a4e 100644 --- a/BookAStar/BookAStar/BookAStar.csproj +++ b/BookAStar/BookAStar/BookAStar.csproj @@ -126,16 +126,14 @@ - + ..\..\packages\Json.NET.Web.1.0.49\lib\portable45-net45+win8+wpa81\Json.NET.Web.dll - True ..\..\..\..\..\..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\MonoAndroid\v6.0\Mono.Android.dll - + ..\..\packages\Newtonsoft.Json.9.0.1\lib\portable-net45+wp80+win8+wpa81\Newtonsoft.Json.dll - True ..\..\packages\Xam.Plugins.Settings.2.1.0\lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\Plugin.Settings.dll @@ -162,6 +160,12 @@ True + + + {67F9D3A8-F71E-4428-913F-C37AE82CDB24} + Yavsc.Client + + diff --git a/BookAStar/BookAStar/DeviceInfoPage.cs b/BookAStar/BookAStar/DeviceInfoPage.cs index 7fb5665c..eb1d3e54 100644 --- a/BookAStar/BookAStar/DeviceInfoPage.cs +++ b/BookAStar/BookAStar/DeviceInfoPage.cs @@ -5,12 +5,13 @@ using System.Linq; using System.Text; using Xamarin.Forms; +using Yavsc.Models.Identity; namespace BookAStar { public class DeviceInfoPage : ContentPage { - public DeviceInfoPage(GoogleCloudMobileDeclaration infos) + public DeviceInfoPage(IGCMDeclaration infos) { Content = new StackLayout { diff --git a/BookAStar/BookAStar/Helpers/MainSettings.cs b/BookAStar/BookAStar/Helpers/MainSettings.cs index f757ee54..33bd6581 100644 --- a/BookAStar/BookAStar/Helpers/MainSettings.cs +++ b/BookAStar/BookAStar/Helpers/MainSettings.cs @@ -6,7 +6,7 @@ using Plugin.Settings.Abstractions; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; - +using System.Threading.Tasks; using Xamarin.Forms; namespace BookAStar @@ -69,14 +69,19 @@ namespace BookAStar } } - public static string GoogleRegId + public static string GoogleRegId { - set { + set { var oldregid = GoogleRegId; AppSettings.AddOrUpdateValue(GoogleRegIdKey, value); // TODO If it changed, and there's an identified user, // Inform the server of it. - if (oldregid != value) App.CurrentApp.RegisterThisDevice(); + if (oldregid != value) + { + Task.Run( async () => { + await App.CurrentApp.PostDeviceInfo(); + }); + } } get { return AppSettings.GetValueOrDefault(GoogleRegIdKey); } } @@ -126,7 +131,9 @@ namespace BookAStar { if (olduserid != value.Id) { - App.CurrentApp.RegisterThisDevice(); + Task.Run(async () => { + await App.CurrentApp.PostDeviceInfo(); + }); } } } diff --git a/BookAStar/BookAStar/IPlatform.cs b/BookAStar/BookAStar/IPlatform.cs index 85ec1f96..0483f67f 100644 --- a/BookAStar/BookAStar/IPlatform.cs +++ b/BookAStar/BookAStar/IPlatform.cs @@ -1,4 +1,5 @@ using BookAStar.Model.Auth.Account; +using Yavsc.Models.Identity; namespace BookAStar { @@ -16,10 +17,11 @@ namespace BookAStar void RevokeAccount(string userName); - GoogleCloudMobileDeclaration GetDeviceInfo(); + IGCMDeclaration GetDeviceInfo(); - void PostDeviceInfo(); - + TAnswer InvokeApi(string method, object arg); + + object InvokeApi(string method, object arg); } } diff --git a/BookAStar/BookAStar/Model/Auth/MobileAppDeclaration.cs b/BookAStar/BookAStar/Model/Auth/MobileAppDeclaration.cs index c7b95899..250bc045 100644 --- a/BookAStar/BookAStar/Model/Auth/MobileAppDeclaration.cs +++ b/BookAStar/BookAStar/Model/Auth/MobileAppDeclaration.cs @@ -1,12 +1,36 @@ +using System; +using Yavsc.Models; +using Yavsc.Models.Identity; + namespace BookAStar.Model.Auth.Account { - public class GoogleCloudMobileDeclaration + public class GoogleCloudMobileDeclaration : IGoogleCloudMobileDeclaration { public string GCMRegistrationId { get; set; } public string DeviceId { get; set; } public string Model { get; set; } public string Platform { get; set; } public string Version { get; set; } + + public IApplicationUser DeviceOwner + { + get + { + throw new NotImplementedException(); + } + + set + { + throw new NotImplementedException(); + } + } + + public string DeviceOwnerId + { + get; + + set; + } } } \ No newline at end of file diff --git a/Yavsc.Client/Helpers/SimpleJsonPostMethod.cs b/Yavsc.Client/Helpers/SimpleJsonPostMethod.cs new file mode 100644 index 00000000..0917d6f4 --- /dev/null +++ b/Yavsc.Client/Helpers/SimpleJsonPostMethod.cs @@ -0,0 +1,128 @@ +// +// PostJson.cs +// +// Author: +// Paul Schneider +// +// Copyright (c) 2015 Paul Schneider +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +using System.Net; +using System.IO; +using System.Threading.Tasks; +using Newtonsoft.Json; +using System; +using System.Diagnostics; + +namespace Yavsc.Helpers +{ + /// + /// Simple json post method. + /// + public class NotSoSimpleJsonPostMethod : IDisposable + { + private HttpWebRequest request = null; + + /// + /// Initializes a new instance of the Yavsc.Helpers.SimpleJsonPostMethod class. + /// + /// Path to method. + public NotSoSimpleJsonPostMethod(string pathToMethod, string authorizationHeader = null) + { + request = (HttpWebRequest)WebRequest.Create(pathToMethod); + request.Method = "POST"; + request.Accept = "application/json"; + request.ContentType = "application/json"; + // request.SendChunked = true; + // request.TransferEncoding = "UTF-8"; + request.Headers = new WebHeaderCollection(); + if (authorizationHeader != null) + request.Headers["Authorization"] = "Bearer " + authorizationHeader; + + } + + public void Dispose() + { + request.Abort(); + } + + /// + /// Invoke the specified query. + /// + /// Query. + public async Task Invoke(object query) + { + + using (Stream streamQuery = await request.GetRequestStreamAsync()) + { + using (StreamWriter writer = new StreamWriter(streamQuery)) + { + writer.Write(JsonConvert.SerializeObject(query)); + } + } + TAnswer ans = default(TAnswer); + using (WebResponse response = await request.GetResponseAsync()) + { + using (Stream responseStream = response.GetResponseStream()) + { + using (StreamReader rdr = new StreamReader(responseStream)) + { + ans = (TAnswer)JsonConvert.DeserializeObject(rdr.ReadToEnd()); + } + } + } + return ans; + } + + public async Task InvokeAnonymous(object query) + { + object result = null; + using (Stream streamQuery = await request.GetRequestStreamAsync()) + { + using (StreamWriter writer = new StreamWriter(streamQuery)) + { + var json = JsonConvert.SerializeObject(query); + + writer.Write(json); + + } + } + try + { + using (WebResponse response = await request.GetResponseAsync()) + { + using (Stream stream = response.GetResponseStream()) + { + if (stream.Length > 0) + { + using (var sr = new StreamReader(stream)) + { + var json = await sr.ReadToEndAsync(); + result = JsonConvert.DeserializeObject(json); + + } + } + + } + } + } + catch (Exception ex) + { + Debug.WriteLine(ex.Message.ToString()); + } + return result; + } + } +} + diff --git a/Yavsc.Client/IGoogleCloudMobileDeclaration.cs b/Yavsc.Client/IGoogleCloudMobileDeclaration.cs index 01c7213e..9ea3033c 100644 --- a/Yavsc.Client/IGoogleCloudMobileDeclaration.cs +++ b/Yavsc.Client/IGoogleCloudMobileDeclaration.cs @@ -1,13 +1,18 @@ namespace Yavsc.Models.Identity { - public interface IGoogleCloudMobileDeclaration + public interface IGCMDeclaration { string DeviceId { get; set; } - IApplicationUser DeviceOwner { get; set; } - string DeviceOwnerId { get; set; } string GCMRegistrationId { get; set; } string Model { get; set; } string Platform { get; set; } string Version { get; set; } + + } + + public interface IGoogleCloudMobileDeclaration: IGCMDeclaration + { + IApplicationUser DeviceOwner { get; set; } + string DeviceOwnerId { get; set; } } } \ No newline at end of file diff --git a/Yavsc.Client/Yavsc.Client.csproj b/Yavsc.Client/Yavsc.Client.csproj index 5e0bb9e2..b21ce6f0 100644 --- a/Yavsc.Client/Yavsc.Client.csproj +++ b/Yavsc.Client/Yavsc.Client.csproj @@ -35,6 +35,7 @@ 4 + @@ -47,7 +48,19 @@ - + + Designer + + + + + ..\packages\Json.NET.Web.1.0.49\lib\portable45-net45+win8+wpa81\Json.NET.Web.dll + True + + + ..\packages\Newtonsoft.Json.8.0.3\lib\portable-net40+sl5+wp80+win8+wpa81\Newtonsoft.Json.dll + True + diff --git a/Yavsc.Client/packages.config b/Yavsc.Client/packages.config index 6b8deb9c..dc205314 100644 --- a/Yavsc.Client/packages.config +++ b/Yavsc.Client/packages.config @@ -1,3 +1,5 @@  + + \ No newline at end of file diff --git a/wrap/mscorlib/project.json b/wrap/mscorlib/project.json new file mode 100644 index 00000000..d1d3a4c9 --- /dev/null +++ b/wrap/mscorlib/project.json @@ -0,0 +1,10 @@ +{ + "version": "1.0.0-*", + "frameworks": { + "net45+win+wpa81+MonoAndroid10+Xamarin.iOS10+MonoTouch10": { + "bin": { + "assembly": "../../../../../../../Program Files (x86)/Reference Assemblies/Microsoft/Framework/MonoAndroid/v1.0/mscorlib.dll" + } + } + } +} \ No newline at end of file