fixes GCM id sending to server

This commit is contained in:
Paul Schneider 2016-07-21 22:02:53 +02:00
commit fcbf3ffeb7
13 changed files with 366 additions and 53 deletions

View file

@ -0,0 +1,103 @@
//
// PostJson.cs
//
// Author:
// Paul Schneider <paulschneider@free.fr>
//
// 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 <http://www.gnu.org/licenses/>.
using System.Net;
using System.IO;
using System.Json;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System;
namespace Yavsc.Helpers
{
/// <summary>
/// Simple json post method.
/// </summary>
public class SimpleJsonPostMethod : IDisposable
{
private HttpWebRequest request=null;
/// <summary>
/// Initializes a new instance of the Yavsc.Helpers.SimpleJsonPostMethod class.
/// </summary>
/// <param name="pathToMethod">Path to method.</param>
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();
}
/// <summary>
/// Invoke the specified query.
/// </summary>
/// <param name="query">Query.</param>
public TAnswer Invoke<TAnswer>(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<TAnswer> (rdr.ReadToEnd ());
}
}
response.Close();
}
return ans;
}
public async Task<JsonValue> 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;
}
}
}

View file

@ -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<TAnswer>(string method, object arg)
{
throw new NotImplementedException();
using (var m =
new SimpleJsonPostMethod(method,
MainSettings.CurrentUser.YavscTokens.AccessToken
))
{
return m.Invoke<TAnswer>(arg);
}
}
public object InvokeApi(string method, object arg)
{
using (var m =
new SimpleJsonPostMethod(method,
MainSettings.CurrentUser.YavscTokens.AccessToken
))
{
return m.InvokeJson(arg);
}
}
}
}