/// <summary> /// Asynchronously gets an HTTP connection to an innovator instance (or proxy) at the given URL /// </summary> /// <param name="url">URL of the innovator instance (or proxy)</param> /// <param name="preferences">Object containing preferences for the connection</param> /// <param name="async">Whether or not to return the connection asynchronously. This is important /// as an HTTP request must be issued to determine the type of connection to create</param> /// <returns>A promise to return a connection object</returns> public static IPromise <IRemoteConnection> GetConnection(string url , ConnectionPreferences preferences, bool async) { preferences = preferences ?? new ConnectionPreferences(); preferences.Url = url; return(GetConnection(preferences, async)); }
/// <summary> /// Gets an HTTP connection to an innovator instance (or proxy) at the given URL /// </summary> /// <param name="url">URL of the innovator instance (or proxy)</param> /// <param name="userAgent">User agent string to use for the connection</param> /// <returns>A connection object</returns> /// <example> /// <code lang="C#"> /// using Innovator.Client; /// /// var conn = Factory.GetConnection("URL", "USER_AGENT"); /// conn.Login(new ExplicitCredentials("DATABASE", "USER_NAME", "PASSWORD")); /// </code> /// </example> public static IRemoteConnection GetConnection(string url, string userAgent) { var prefs = new ConnectionPreferences(); prefs.Url = url; prefs.Headers.UserAgent = userAgent; return(GetConnection(prefs, false).Value); }
public static IPromise<IAsyncConnection> ArasLogin(this ConnectionData credentials, bool async) { var cred = credentials.ArasCredentials(); var prefs = new ConnectionPreferences() { UserAgent = "InnovatorAdmin v" + Assembly.GetExecutingAssembly().GetName().Version.ToString() }; var localePref = credentials.Params.FirstOrDefault(p => p.Name == "LOCALE"); var tzPref = credentials.Params.FirstOrDefault(p => p.Name == "TIMEZONE_NAME"); if (localePref != null) prefs.Locale = localePref.Value; if (tzPref != null) prefs.TimeZone = tzPref.Value; return Factory.GetConnection(credentials.Url , prefs, async) .Continue(c => { return c.Login(cred, async) .Convert(u => (IAsyncConnection)c); }); }
public async Task Run(TestContext context) { var url = this.Url; var db = this.Database; var remoteConn = context.Connection as IRemoteConnection; if (string.IsNullOrEmpty(url) && remoteConn != null) url = remoteConn.Url.ToString(); if (string.IsNullOrEmpty(db)) db = context.Connection.Database; var prefs = new ConnectionPreferences() { UserAgent = "InnovatorAdmin UnitTest" }; var conn = await Factory.GetConnection(url, prefs, true).ToTask(); ICredentials cred; switch (this.Type) { case CredentialType.Anonymous: cred = new AnonymousCredentials(db); break; case CredentialType.Windows: cred = new WindowsCredentials(db); break; default: if (_password.IsNullOrEmpty()) { cred = context.CredentialStore.OfType<ExplicitCredentials>() .FirstOrDefault(c => string.Equals(c.Database, db) && string.Equals(c.Username, this.UserName)); } else { cred = new ExplicitCredentials(db, this.UserName, _password); } break; } if (cred == null) throw new InvalidOperationException("Could not create credentials for this login type"); await conn.Login(cred, true).ToTask(); context.PushConnection(conn); }
private static IRemoteConnection ArasConn(HttpClient arasService, string url, ConnectionPreferences preferences) { var result = new Connection.ArasHttpConnection(arasService, url, preferences.ItemFactory); if (preferences.Headers.Any()) { result.DefaultSettings(r => { if (!string.IsNullOrEmpty(preferences.Headers.UserAgent)) { r.UserAgent = preferences.Headers.UserAgent; } foreach (var header in preferences.Headers.NonUserAgentHeaders()) { r.SetHeader(header.Key, header.Value); } }); } return(result); }
/// <summary> /// Asynchronously gets an HTTP connection to an innovator instance (or proxy) at the given URL /// </summary> /// <param name="preferences">Object containing preferences for the connection</param> /// <param name="async">Whether or not to return the connection asynchronously. This is important /// as an HTTP request must be issued to determine the type of connection to create</param> /// <returns>A promise to return a connection object</returns> public static IPromise <IRemoteConnection> GetConnection(ConnectionPreferences preferences, bool async) { preferences = preferences ?? new ConnectionPreferences(); var url = preferences.Url; url = (url ?? "").TrimEnd('/'); if (url.EndsWith("Server/InnovatorServer.aspx", StringComparison.OrdinalIgnoreCase)) { url = url.Substring(0, url.Length - 21); } if (!url.EndsWith("/server", StringComparison.OrdinalIgnoreCase)) { url += "/Server"; } var configUrl = url + "/mapping.xml"; var masterService = preferences.HttpService ?? DefaultService.Invoke(); var arasSerice = preferences.HttpService ?? DefaultService.Invoke(); Func <ServerMapping, IRemoteConnection> connFactory = m => { var uri = (m.Url ?? "").TrimEnd('/'); if (!uri.EndsWith("/server", StringComparison.OrdinalIgnoreCase)) { url += "/Server"; } switch (m.Type) { case ServerType.Proxy: throw new NotSupportedException(); default: return(ArasConn(arasSerice, uri, preferences)); } }; var result = new Promise <IRemoteConnection>(); var req = new HttpRequest(); req.UserAgent = preferences.Headers.UserAgent; req.SetHeader("Accept", "text/xml"); foreach (var header in preferences.Headers.NonUserAgentHeaders()) { req.SetHeader(header.Key, header.Value); } var trace = new LogData(4, "Innovator: Try to download mapping file", Factory.LogListener) { { "url", configUrl }, }; result.CancelTarget(masterService.GetPromise(new Uri(configUrl), async, trace, req) .Progress((p, m) => result.Notify(p, m)) .Done(r => { var data = r.AsString(); if (string.IsNullOrEmpty(data)) { result.Resolve(ArasConn(arasSerice, url, preferences)); } else { try { var servers = ServerMapping.FromXml(data).ToArray(); if (servers.Length < 1) { result.Resolve(ArasConn(arasSerice, url, preferences)); } else if (servers.Length == 1) { result.Resolve(connFactory(servers.Single())); } else { foreach (var server in servers) { server.Factory = connFactory; } result.Resolve(new MappedConnection(servers, preferences.AuthCallback)); } } catch (XmlException) { result.Resolve(ArasConn(arasSerice, url, preferences)); } } }).Fail(ex => { result.Resolve(ArasConn(arasSerice, url, preferences)); })).Always(trace.Dispose); if (preferences.Credentials != null) { IRemoteConnection conn = null; return(result .Continue(c => { conn = c; return c.Login(preferences.Credentials, async); }) .Convert(u => conn)); } return(result); }
/// <summary> /// Gets an HTTP connection to an innovator instance (or proxy) at the given URL /// </summary> /// <param name="url">URL of the innovator instance (or proxy)</param> /// <param name="preferences">Object containing preferences for the connection</param> /// <returns>A connection object</returns> public static IRemoteConnection GetConnection(string url, ConnectionPreferences preferences) { preferences = preferences ?? new ConnectionPreferences(); preferences.Url = url; return(GetConnection(preferences, false).Value); }
/// <summary> /// Gets an HTTP connection to an innovator instance at the given URL /// </summary> /// <param name="preferences">Object containing preferences for the connection</param> /// <returns>A new <see cref="IRemoteConnection"/> object</returns> /// <example> /// Create a new connection using the default stored connection /// <code lang="C#"> /// var pref = SavedConnections.Load().Default; /// var conn = Factory.GetConnection(pref); /// </code> /// </example> public static IRemoteConnection GetConnection(ConnectionPreferences preferences) { return(GetConnection(preferences, false).Value); }
/// <summary> /// Asynchronously gets an HTTP connection to an innovator instance (or proxy) at the given URL /// </summary> /// <param name="url">URL of the innovator instance (or proxy)</param> /// <param name="preferences">Object containing preferences for the connection</param> /// <returns>A promise to return a connection object</returns> public static IPromise<IRemoteConnection> GetConnection(string url , ConnectionPreferences preferences, bool async) { url = (url ?? "").TrimEnd('/'); if (!url.EndsWith("/server", StringComparison.OrdinalIgnoreCase)) url += "/Server"; var configUrl = url + "/mapping.xml"; var masterService = preferences.HttpService ?? DefaultService.Invoke(); var arasSerice = preferences.HttpService ?? new DefaultHttpService() { Compression = CompressionType.none }; Func<ServerMapping, IRemoteConnection> connFactory = m => { var uri = (m.Url ?? "").TrimEnd('/'); if (!uri.EndsWith("/server", StringComparison.OrdinalIgnoreCase)) url += "/Server"; switch (m.Type) { case ServerType.Proxy: m.Endpoints.Base = new Uri(uri + "/"); var conn = new Connection.ProxyServerConnection(masterService, m.Endpoints); conn.SessionPolicy = preferences.SessionPolicy; if (!string.IsNullOrEmpty(preferences.UserAgent)) conn.DefaultSettings(req => req.UserAgent = preferences.UserAgent); return conn; default: return ArasConn(arasSerice, uri, preferences); } }; var result = new Promise<IRemoteConnection>(); result.CancelTarget(masterService.Execute("GET", configUrl, null, CredentialCache.DefaultCredentials , async, request => { request.UserAgent = preferences.UserAgent; request.SetHeader("Accept", "text/xml"); }).Progress((p, m) => result.Notify(p, m)) .Done(r => { var data = r.AsString(); if (string.IsNullOrEmpty(data)) { result.Resolve(ArasConn(arasSerice, url, preferences)); } else { try { var servers = ServerMapping.FromXml(data).ToArray(); if (servers.Length < 1) { result.Resolve(ArasConn(arasSerice, url, preferences)); } else if (servers.Length == 1) { result.Resolve(connFactory(servers.Single())); } else { foreach (var server in servers) { server.Factory = connFactory; } result.Resolve(new MappedConnection(servers)); } } catch (XmlException) { result.Resolve(ArasConn(arasSerice, url, preferences)); } } }).Fail(ex => { result.Resolve(ArasConn(arasSerice, url, preferences)); })); return result; }
/// <summary> /// Gets an HTTP connection to an innovator instance (or proxy) at the given URL /// </summary> /// <param name="url">URL of the innovator instance (or proxy)</param> /// <param name="preferences">Object containing preferences for the connection</param> /// <returns>A connection object</returns> public static IRemoteConnection GetConnection(string url, ConnectionPreferences preferences) { return GetConnection(url, preferences, false).Value; }
private static IRemoteConnection ArasConn(IHttpService arasService, string url, ConnectionPreferences preferences) { var result = new Connection.ArasHttpConnection(arasService, url); if (!string.IsNullOrEmpty(preferences.Locale) || !string.IsNullOrEmpty(preferences.TimeZone) || !string.IsNullOrEmpty(preferences.UserAgent)) { result.DefaultSettings(r => { if (!string.IsNullOrEmpty(preferences.Locale)) r.SetHeader("LOCALE", preferences.Locale); if (!string.IsNullOrEmpty(preferences.TimeZone)) r.SetHeader("TIMEZONE_NAME", preferences.TimeZone); if (!string.IsNullOrEmpty(preferences.UserAgent)) r.UserAgent = preferences.UserAgent; }); } return result; }
public PreferencesWriter(ConnectionPreferences prefs) { _prefs = prefs; }