/**== Custom Connection Implementations
		*
		* The client abstracts sending the request and creating a response behind `IConnection`
		*
		* By default the client will use a WebRequest based version on the desktop CLR (.NET 4.5 and up targets)
		* and a HttpClient based HttpConnection specifically build for the Core CLR (netstandard 1.6).
		*
		* The reason for the split is because WebRequest and ServicePoint are not directly available on netstandard 1.6
		*
		* However the implementation written against WebRequest is the most matured implementation that we weren't ready to it give up.
		* There are also a couple of important toggles that are easy to set against a `ServicePoint` that we'd have to give up
		* had we jumped on the `HttpClient` completely.
		*
		* Another limitation is that `HttpClient` has no synchronous code paths and supporting that means doing hacky async patches which definitely
		* need time to bake.
		*
		* So why would you ever want to pass your own `IConnection`? Let's look at a couple of examples
		*
		*/

		public void OverrideHow()
		{
			var connection = new InMemoryConnection();
			var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
			var settings = new ConnectionSettings(connectionPool, connection);
			var client = new ElasticClient(settings);
		}
		private IRequestPipeline CreatePipeline(
			Func<IEnumerable<Uri>, IConnectionPool> setupPool, Func<ConnectionSettings, ConnectionSettings> settingsSelector = null, IDateTimeProvider dateTimeProvider = null, InMemoryConnection connection = null)
		{
			var pool = setupPool(new[] { TestClient.CreateUri(), TestClient.CreateUri(9201) });
			var settings = new ConnectionSettings(pool, connection ?? new InMemoryConnection());
			settings = settingsSelector?.Invoke(settings) ?? settings;
			return new FixedPipelineFactory(settings, dateTimeProvider ?? DateTimeProvider.Default).Pipeline;
		}
		public IElasticClient CreateClient(string jsonResponse, Action<JsonSerializerSettings, IConnectionSettingsValues> settingsOverride)
		{
			var connection = new InMemoryConnection(Encoding.UTF8.GetBytes(jsonResponse));
			var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
			var connectionSettings = new ConnectionSettings(connectionPool, connection, new MyCustomJsonFactory(settingsOverride))
				.DefaultIndex("default-index");
			var client = new ElasticClient(connectionSettings);
			return client;
		}
		public IElasticClient CreateClient(string jsonResponse, Action<JsonSerializerSettings, IConnectionSettingsValues> settingsOverride)
		{
			var connection = new InMemoryConnection(Encoding.UTF8.GetBytes(jsonResponse));
			var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
#pragma warning disable CS0618 // Type or member is obsolete
			var connectionSettings = new ConnectionSettings(connectionPool, connection, settings =>
				new LocalJsonNetSerializer(settings.DefaultIndex("default-index"), settingsOverride));
#pragma warning restore CS0618 // Type or member is obsolete
			var client = new ElasticClient(connectionSettings);
			return client;
		}
		public static IElasticClient GetFixedReturnClient(object responseJson)
		{
			var serializer = new NestSerializer(new ConnectionSettings());
			byte[] fixedResult;
			using (var ms = new MemoryStream())
			{
				serializer.Serialize(responseJson, ms);
				fixedResult = ms.ToArray();
			}
			var connection = new InMemoryConnection(fixedResult);
			var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
			var settings = new ConnectionSettings(connectionPool, connection);
			return new ElasticClient(settings);
		}
		public void CanDeserializeNestedBulkError()
		{
			var nestedCausedByError = @"{
			   ""took"": 4,
			   ""errors"": true,
			   ""items"": [{
					""update"": {
						""_index"": ""index-name"",
						""_type"": ""type-name"",
						""_id"": ""1"",
						""status"": 400,
						""error"": {
							""type"": ""illegal_argument_exception"",
							""reason"": ""failed to execute script"",
							""caused_by"": {
								""type"": ""script_exception"",
								""reason"": ""failed to run inline script [use(java.lang.Exception) {throw new Exception(\""Customized Exception\"")}] using lang [groovy]"",
								""caused_by"": {
									""type"": ""privileged_action_exception"",
									""reason"": null,
									""caused_by"": {
										""type"": ""exception"",
										""reason"": ""Custom Exception""
									}
								}
							}
						}
					}
				}]
			}";

			var bytes = Encoding.UTF8.GetBytes(nestedCausedByError);
			var connection = new InMemoryConnection(bytes);
			var settings = new ConnectionSettings(new SingleNodeConnectionPool(new Uri("http://localhost:9200")), connection);
			var client = new ElasticClient(settings);

			var bulkResponse = client.Bulk(new BulkDescriptor());

			bulkResponse.Errors.Should().BeTrue();

			var firstOperation = bulkResponse.ItemsWithErrors.First();

			firstOperation.Error.Should().NotBeNull();
			firstOperation.Error.CausedBy.Should().NotBeNull();
			firstOperation.Error.CausedBy.InnerCausedBy.Should().NotBeNull();
			firstOperation.Error.CausedBy.InnerCausedBy.InnerCausedBy.Should().NotBeNull();
		}
		public void CanDeserializeCopyTo()
		{
			var json = "{\"test-events-v1-201412\":{\"mappings\":{\"events\":{\"dynamic\":\"false\",\"_size\":{\"enabled\":true},\"properties\":{\"created_utc\":{\"type\":\"date\"},\"data\":{\"properties\":{\"@environment\":{\"properties\":{\"o_s_name\":{\"type\":\"text\",\"index\":false,\"copy_to\":[\"os\"]}}}}},\"id\":{\"type\":\"keyword\"},\"os\":{\"type\":\"text\",\"fields\":{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":256}}}}}}},\"test-events-v1-201502\":{\"mappings\":{\"events\":{\"dynamic\":\"false\",\"_size\":{\"enabled\":true},\"properties\":{\"created_utc\":{\"type\":\"date\"},\"data\":{\"properties\":{\"@environment\":{\"properties\":{\"o_s_name\":{\"type\":\"text\",\"index\":false,\"copy_to\":[\"os\"]}}}}},\"id\":{\"type\":\"keyword\"},\"os\":{\"type\":\"text\",\"fields\":{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":256}}}}}}}}";

			var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

			var connection = new InMemoryConnection(Encoding.UTF8.GetBytes(json));
			var settings = new ConnectionSettings(pool, connection).DefaultIndex("test-events-v1-201412");
			var client = new ElasticClient(settings);

			var mappingResponse = client.GetMapping<Events>();

			mappingResponse.IsValid.Should().BeTrue();

			var mappingWalker = new MappingWalker(new CopyToVisitor());
			mappingWalker.Accept(mappingResponse);
		}
Beispiel #8
0
 /// <summary>
 /// Instantiate connection settings using a <see cref="SingleNodeConnectionPool" /> using the provided
 /// <see cref="InMemoryConnection" /> that never uses any IO.
 /// </summary>
 public ConnectionSettings(InMemoryConnection connection)
     : this(new SingleNodeConnectionPool(new Uri("http://localhost:9200")), connection)
 {
 }
		public void CanDeserializeNestedError()
		{
			var nestedCausedByError = @"{
				""status"": 400,
				""error"": {
					""type"": ""illegal_argument_exception"",
					""reason"": ""failed to execute script"",
					""caused_by"": {
						""type"": ""script_exception"",
						""reason"": ""failed to run inline script [use(java.lang.Exception) {throw new Exception(\""Customized Exception\"")}] using lang [groovy]"",
						""caused_by"": {
							""type"": ""privileged_action_exception"",
							""reason"": null,
							""caused_by"": {
								""type"": ""exception"",
								""reason"": ""Custom Exception""
							}
						}
					}
				}
			}";

			var bytes = Encoding.UTF8.GetBytes(nestedCausedByError);
			var connection = new InMemoryConnection(bytes, 400);
			var settings = new ConnectionSettings(new SingleNodeConnectionPool(new Uri("http://localhost:9200")), connection);
			var client = new ElasticClient(settings);

			var searchResponse = client.Search<object>(s => s.Index("index"));

			searchResponse.ShouldNotBeValid();
			searchResponse.ServerError.Should().NotBeNull();
			searchResponse.ServerError.Error.Should().NotBeNull();
			searchResponse.ServerError.Error.CausedBy.Should().NotBeNull();
			searchResponse.ServerError.Error.CausedBy.InnerCausedBy.Should().NotBeNull();
			searchResponse.ServerError.Error.CausedBy.InnerCausedBy.InnerCausedBy.Should().NotBeNull();
		}
Beispiel #10
0
		public static IElasticClient GetFixedReturnClient(
			object response,
			int statusCode = 200,
			Func<ConnectionSettings, ConnectionSettings> modifySettings = null,
			string contentType = "application/json",
			Exception exception = null)
		{
			var serializer = new JsonNetSerializer(new ConnectionSettings());
			byte[] fixedResult;

			if (contentType == "application/json")
			{
				using (var ms = new MemoryStream())
				{
					serializer.Serialize(response, ms);
					fixedResult = ms.ToArray();
				}
			}
			else
			{
				fixedResult = Encoding.UTF8.GetBytes(response.ToString());
			}

			var connection = new InMemoryConnection(fixedResult, statusCode, exception);
			var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
			var defaultSettings = new ConnectionSettings(connectionPool, connection)
				.DefaultIndex("default-index");
			var settings = (modifySettings != null) ? modifySettings(defaultSettings) : defaultSettings;
			return new ElasticClient(settings);
		}
		public static IElasticClient GetFixedReturnClient(
			object response,
			int statusCode = 200,
			Func<ConnectionSettings, ConnectionSettings> modifySettings = null,
			string contentType = "application/json",
			Exception exception = null)
		{
			var serializer = Default.Serializer;
			var fixedResult = contentType == "application/json"
				? serializer.SerializeToBytes(response)
				: Encoding.UTF8.GetBytes(response.ToString());

			var connection = new InMemoryConnection(fixedResult, statusCode, exception);
			var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
			var defaultSettings = new ConnectionSettings(connectionPool, connection)
				.DefaultIndex("default-index");
			var settings = (modifySettings != null) ? modifySettings(defaultSettings) : defaultSettings;
			return new ElasticClient(settings);
		}