Repository navigation
Expand file tree
/
Copy pathMobileServiceClient.cs
More file actions
162 lines (141 loc) · 5.26 KB
/
Copy pathMobileServiceClient.cs
File metadata and controls
162 lines (141 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using UnityEngine;
using System.Collections;
using RestSharp;
using System.Collections.Generic;
using System;
using System.Text.RegularExpressions;
#if !NETFX_CORE || UNITY_ANDROID
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
#endif
namespace Unity3dAzure.AppServices
{
[CLSCompliant(false)]
public class MobileServiceClient : RestClient, IAzureMobileServiceClient
{
public string AppUrl { get; private set; }
public string AppKey { get; private set; }
public MobileServiceUser User { get; set; }
public const string URI_API = "api/";
/// <summary>
/// Creates a new RestClient using Azure Mobile Service's Application Url and App Key
/// NB: Mobile Services should be migrated to use Azure App Service with constructor below.
/// </summary>
public MobileServiceClient(string appUrl, string appKey) : base(appUrl)
{
AppUrl = appUrl;
AppKey = appKey;
// required for running in Windows and Android
#if !NETFX_CORE || UNITY_ANDROID
Debug.Log("ServerCertificateValidation");
ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidationCallback;
#endif
}
/// <summary>
/// Creates a new RestClient using Azure App Service's Application Url
/// </summary>
public MobileServiceClient(string appUrl) : base(appUrl)
{
AppUrl = appUrl;
Debug.Log("AppUrl: " + AppUrl);
// required for running in Windows and Android
#if !NETFX_CORE || UNITY_ANDROID
Debug.Log("ServerCertificateValidation");
ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidationCallback;
#endif
}
/// <summary>
/// Using factory method forces app url to be changed from 'http' to 'https' url
/// </summary>
public static MobileServiceClient Create(string appUrl)
{
return new MobileServiceClient (ForceHttps (appUrl));
}
public override string ToString()
{
return this.BaseUrl;
}
public MobileServiceTable<E> GetTable<E>(string tableName) where E : class
{
return new MobileServiceTable<E>(tableName, this);
}
/// <summary>
/// Client-directed single sign on (POST with access token)
/// </summary>
public void Login(MobileServiceAuthenticationProvider provider, string token, Action<IRestResponse<MobileServiceUser>> callback = null)
{
string p = provider.ToString().ToLower();
string uri = IsAppService() ? ".auth/login/" + p : "login/" + p;
ZumoRequest request = new ZumoRequest(this, uri, Method.POST);
Debug.Log("Login Request Uri: " + uri + " access token: " + token);
request.AddBodyAccessToken(token);
this.ExecuteAsync(request, callback);
}
/// <summary>
/// TODO: Service login (using GET via webview)
/// </summary>
/*
public void Login(MobileServiceAuthenticationProvider provider)
{
Debug.Log("TODO");
}
//*/
/// <summary>
/// GET custom API
/// </summary>
public void InvokeApi<T>(string apiName, Action<IRestResponse<T>> callback = null) where T : new()
{
InvokeApi<T> (apiName, Method.GET, callback);
}
/// <summary>
/// Invokes custom API for HTTP Methods: GET, POST, PUT, PATCH, DELETE
/// </summary>
public void InvokeApi<T>(string apiName, Method httpMethod, Action<IRestResponse<T>> callback = null) where T : new()
{
string uri = URI_API + apiName;
ZumoRequest request = new ZumoRequest(this, uri, httpMethod);
Debug.Log( httpMethod.ToString() + " custom API Request Uri: " + uri );
this.ExecuteAsync(request, callback);
}
/// <summary>
/// Invokes custom API with body
/// </summary>
public void InvokeApi<T>(string apiName, Method httpMethod, T body, Action<IRestResponse<T>> callback = null) where T : new()
{
string uri = URI_API + apiName;
ZumoRequest request = new ZumoRequest(this, uri, httpMethod);
request.AddBody (body);
Debug.Log( httpMethod.ToString() + " custom API Request Uri: " + uri );
this.ExecuteAsync(request, callback);
}
/// <summary>
/// When you copy the URL is is 'http' by default, but its preferable to use 'https'
/// </summary>
private static string ForceHttps(string appUrl)
{
return Regex.Replace(appUrl, "(?m)http://", "https://");
}
/// <summary>
/// Mobile Service uses an AppKey, but App Service does not.
/// </summary>
public bool IsAppService()
{
return String.IsNullOrEmpty(AppKey);
}
#if !NETFX_CORE || UNITY_ANDROID
private bool RemoteCertificateValidationCallback(System.Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
// Check the certificate to see if it was issued from Azure
if (certificate.Subject.Contains("azurewebsites.net"))
{
return true;
}
else
{
return false;
}
}
#endif
}
}