cleaning obsolete WebRequest usage
This commit is contained in:
parent
579e1d25c2
commit
2144500942
4 changed files with 114 additions and 177 deletions
|
|
@ -36,6 +36,10 @@ namespace ZicMoove
|
|||
using Model.Auth;
|
||||
using Plugin.DeviceInfo;
|
||||
using Yavsc.Models.Identity;
|
||||
using System.Json;
|
||||
using Newtonsoft.Json;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
|
||||
public partial class App : Application // superclass new in 1.3
|
||||
{
|
||||
|
|
@ -231,15 +235,20 @@ namespace ZicMoove
|
|||
BindingContext = bookQueries
|
||||
};
|
||||
|
||||
homePage = new HomePage() {
|
||||
homePage = new HomePage()
|
||||
{
|
||||
Title = "Accueil",
|
||||
Icon = "icon.png" };
|
||||
Icon = "icon.png"
|
||||
};
|
||||
|
||||
homePage.BindingContext = new HomeViewModel {
|
||||
homePage.BindingContext = new HomeViewModel
|
||||
{
|
||||
BookQueries = bookQueries,
|
||||
UserProfile = userprofile };
|
||||
UserProfile = userprofile
|
||||
};
|
||||
|
||||
userProfilePage = new UserProfilePage {
|
||||
userProfilePage = new UserProfilePage
|
||||
{
|
||||
Title = "Profile utilisateur",
|
||||
Icon = "ic_corp_icon.png",
|
||||
BindingContext = userprofile
|
||||
|
|
@ -286,7 +295,8 @@ namespace ZicMoove
|
|||
Text = "Accueil",
|
||||
Icon = "icon.png",
|
||||
Command = new Command(
|
||||
() => {
|
||||
() =>
|
||||
{
|
||||
ShowPage(homePage);
|
||||
})
|
||||
};
|
||||
|
|
@ -373,7 +383,8 @@ namespace ZicMoove
|
|||
chatHubConnection.Error += ChatHubConnection_Error;
|
||||
|
||||
chatHubProxy = chatHubConnection.CreateHubProxy("ChatHub");
|
||||
chatHubProxy.On<string, string>("addPV", (n, m) => {
|
||||
chatHubProxy.On<string, string>("addPV", (n, m) =>
|
||||
{
|
||||
var msg = new ChatMessage
|
||||
{
|
||||
Message = m,
|
||||
|
|
@ -431,24 +442,52 @@ namespace ZicMoove
|
|||
return chatHubProxy;
|
||||
}
|
||||
}
|
||||
public static Task<bool> HasSomeCloud {
|
||||
public static Task<bool> HasSomeCloud
|
||||
{
|
||||
get
|
||||
{
|
||||
return CrossConnectivity.Current.IsReachable(Constants.YavscHomeUrl, Constants.CloudTimeout);
|
||||
}
|
||||
}
|
||||
|
||||
public static void PostDeviceInfo()
|
||||
public static async Task PostDeviceInfo()
|
||||
{
|
||||
var info = GetDeviceInfo();
|
||||
if (!string.IsNullOrWhiteSpace(info.GCMRegistrationId))
|
||||
{
|
||||
if (MainSettings.CurrentUser != null)
|
||||
{
|
||||
InvokeApi("gcm/register", info);
|
||||
DataManager.Instance.Activities.Execute(null);
|
||||
}
|
||||
using (var client = UserHelpers.CreateJsonClient())
|
||||
{
|
||||
try
|
||||
{
|
||||
var stringContent = JsonConvert.SerializeObject(info);
|
||||
HttpContent content = new StringContent(
|
||||
stringContent, Encoding.UTF8, "application/json"
|
||||
);
|
||||
|
||||
using (var response = await client.PostAsync(Constants.MobileRegistrationUrl, content))
|
||||
{
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var rcontent = await response.Content.ReadAsStringAsync();
|
||||
var jvalue = JsonValue.Parse(rcontent);
|
||||
if (jvalue != null)
|
||||
if (jvalue.ContainsKey("UpdateActivities"))
|
||||
if ((bool)jvalue["UpdateActivities"])
|
||||
{
|
||||
DataManager.Instance.Activities.Execute(null);
|
||||
DataManager.Instance.Activities.SaveEntity();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// TODO err reporting
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public static IGCMDeclaration GetDeviceInfo()
|
||||
|
|
@ -470,30 +509,7 @@ namespace ZicMoove
|
|||
};
|
||||
}
|
||||
|
||||
[Obsolete("Use RemoteEntity to manage entities from API")]
|
||||
public async Task<TAnswer> InvokeApi<TAnswer>(string method, object arg)
|
||||
{
|
||||
using (var m =
|
||||
new SimpleJsonPostMethod(
|
||||
method, "Bearer " +
|
||||
MainSettings.CurrentUser.YavscTokens.AccessToken
|
||||
))
|
||||
{
|
||||
return await m.Invoke<TAnswer>(arg);
|
||||
}
|
||||
}
|
||||
|
||||
public static object InvokeApi(string method, object arg)
|
||||
{
|
||||
using (var m =
|
||||
new SimpleJsonPostMethod(
|
||||
method, "Bearer " +
|
||||
MainSettings.CurrentUser.YavscTokens.AccessToken
|
||||
))
|
||||
{
|
||||
return m.InvokeJson(arg);
|
||||
}
|
||||
}
|
||||
public static void ShowBookQuery(BookQuery query)
|
||||
{
|
||||
var page = new BookQueryPage
|
||||
|
|
|
|||
|
|
@ -1,118 +0,0 @@
|
|||
//
|
||||
// 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;
|
||||
using System.Diagnostics;
|
||||
using ZicMoove;
|
||||
|
||||
namespace ZicMoove.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";
|
||||
|
||||
if (authorizationHeader != null)
|
||||
request.Headers["Authorization"] = authorizationHeader;
|
||||
}
|
||||
|
||||
public static string BasePath { get; private set; } = Constants.YavscApiUrl;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
request.Abort();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invoke the specified query.
|
||||
/// </summary>
|
||||
/// <param name="query">Query.</param>
|
||||
public async Task<TAnswer> Invoke<TAnswer>(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<TAnswer> (rdr.ReadToEnd ());
|
||||
}
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
public async Task<JsonValue> InvokeJson(object query)
|
||||
{
|
||||
JsonValue jsonDoc = null;
|
||||
|
||||
try
|
||||
{
|
||||
using (Stream streamQuery = await request.GetRequestStreamAsync())
|
||||
{
|
||||
using (StreamWriter writer = new StreamWriter(streamQuery))
|
||||
{
|
||||
writer.Write(JsonConvert.SerializeObject(query));
|
||||
}
|
||||
}
|
||||
using (WebResponse response = await request.GetResponseAsync())
|
||||
{
|
||||
using (Stream stream = response.GetResponseStream())
|
||||
{
|
||||
if (stream.Length > 0)
|
||||
jsonDoc = await Task.Run(() => JsonObject.Load(stream));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (WebException ex)
|
||||
{
|
||||
// TODO err logging
|
||||
Debug.WriteLine($"Web request failed: {request.ToString()}\n" + ex.ToString());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"Web request failed: {request.ToString()}\n" + ex.ToString());
|
||||
}
|
||||
|
||||
return jsonDoc;
|
||||
}
|
||||
}
|
||||
}
|
||||
39
ZicMoove/ZicMoove/Views/CommandFormView.cs
Normal file
39
ZicMoove/ZicMoove/Views/CommandFormView.cs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection.Emit;
|
||||
using System.Text;
|
||||
|
||||
using Xamarin.Forms;
|
||||
using ZicMoove.Model.Workflow;
|
||||
|
||||
namespace ZicMoove.Views
|
||||
{
|
||||
public class CommandFormView : ContentView
|
||||
{
|
||||
public static readonly BindableProperty CommandType = BindableProperty.Create(
|
||||
"Type",
|
||||
typeof(string),
|
||||
typeof(CommandFormView)
|
||||
);
|
||||
|
||||
public string Type
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetValue(CommandType) as string;
|
||||
}
|
||||
set {
|
||||
SetValue(CommandType, value);
|
||||
}
|
||||
}
|
||||
|
||||
CommandForm Context { get { return BindingContext as CommandForm; } }
|
||||
|
||||
public CommandFormView()
|
||||
{
|
||||
Content = new Label { Text = Context?.Title };
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -67,7 +67,6 @@
|
|||
<Compile Include="Data\ApiCallFailedException.cs" />
|
||||
<Compile Include="Data\EstimateEntity.cs" />
|
||||
<Compile Include="Data\NonCrUD\RemoteFiles.cs" />
|
||||
<Compile Include="Helpers\SimpleJsonPostMethod.cs" />
|
||||
<Compile Include="Model\Access\BlackListed.cs" />
|
||||
<Compile Include="Model\Auth\GCRegIdDeclaration.cs" />
|
||||
<Compile Include="Model\Booking\MusicalPreference.cs" />
|
||||
|
|
@ -130,6 +129,7 @@
|
|||
<Compile Include="ViewModels\UserProfile\UserProfileViewModel.cs" />
|
||||
<Compile Include="ViewModels\UserProfile\DirectoryInfoViewModel.cs" />
|
||||
<Compile Include="ViewModels\Validation\ErrorSeverity.cs" />
|
||||
<Compile Include="Views\CommandFormView.cs" />
|
||||
<Compile Include="Views\EnumPicker.cs" />
|
||||
<Compile Include="Converters\BooleanToObjectConverter.cs" />
|
||||
<Compile Include="Converters\EnumConverter.cs" />
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue