-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add higher-level HTTP DSL methods #447
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bf47cac
Lay foundations
Xtansia b6d9bae
Implement generation
Xtansia 2e81ca2
Run generator
Xtansia a290e44
Update samples
Xtansia 490046b
Update guide
Xtansia 062a686
Add changelog entry
Xtansia 58315a8
PR comments
Xtansia fced4d7
Fix naming tests
Xtansia b9a9ab2
Why is the ordering of these statements load-bearing???
Xtansia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,33 +4,53 @@ | |
- [PUT](#put) | ||
- [POST](#post) | ||
- [DELETE](#delete) | ||
- [Using Different Types Of PostData](#using-different-types-of-postdata) | ||
- [PostData.String](#postdatastring) | ||
- [PostData.Bytes](#postdatabytes) | ||
- [PostData.Serializable](#postdataserializable) | ||
- [PostData.MultiJson](#postdatamultijson) | ||
- [Request Bodies](#request-bodies) | ||
- [String](#string) | ||
- [Bytes](#bytes) | ||
- [Serializable](#serializable) | ||
- [Multi Json](#multi-json) | ||
- [Response Bodies](#response-bodies) | ||
|
||
# Making Raw JSON REST Requests | ||
The OpenSearch client implements many high-level REST DSLs that invoke OpenSearch APIs. However you may find yourself in a situation that requires you to invoke an API that is not supported by the client. You can use `client.LowLevel.DoRequest` to do so. See [samples/Samples/RawJson/RawJsonSample.cs](../samples/Samples/RawJson/RawJsonSample.cs) for a complete working sample. | ||
The OpenSearch client implements many high-level REST DSLs that invoke OpenSearch APIs. However you may find yourself in a situation that requires you to invoke an API that is not supported by the client. You can use the methods defined within `client.Http` or `client.LowLevel.Http` to do so. See [samples/Samples/RawJson/RawJsonHighLevelSample.cs](../samples/Samples/RawJson/RawJsonHighLevelSample.cs) and [samples/Samples/RawJson/RawJsonLowLevelSample.cs](../samples/Samples/RawJson/RawJsonLowLevelSample.cs) for complete working samples. | ||
|
||
Older versions of the client that do not support the `client.Http` namespace can use the `client.LowLevel.DoRequest` method instead. | ||
|
||
## HTTP Methods | ||
|
||
### GET | ||
The following example returns the server version information via `GET /`. | ||
|
||
```csharp | ||
var info = await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.GET, "/", CancellationToken.None); | ||
var info = await client.Http.GetAsync<DynamicResponse>("/"); | ||
// OR | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would remove |
||
var info = await client.LowLevel.Http.GetAsync<DynamicResponse>("/"); | ||
|
||
Console.WriteLine($"Welcome to {info.Body.version.distribution} {info.Body.version.number}!"); | ||
``` | ||
|
||
### HEAD | ||
The following example checks if an index exists via `HEAD /movies`. | ||
|
||
```csharp | ||
var indexExists = await client.Http.HeadAsync<VoidResponse>("/movies"); | ||
// OR | ||
var indexExists = await client.LowLevel.Http.HeadAsync<VoidResponse>("/movies"); | ||
|
||
Console.WriteLine($"Index Exists: {indexExists.HttpStatusCode == 200}"); | ||
``` | ||
|
||
### PUT | ||
The following example creates an index. | ||
|
||
```csharp | ||
var indexBody = new { settings = new { index = new { number_of_shards = 4 } } }; | ||
|
||
var createIndex = await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.PUT, "/movies", CancellationToken.None, PostData.Serializable(indexBody)); | ||
Debug.Assert(createIndex.Success && (bool)createIndex.Body.acknowledged, createIndex.DebugInformation); | ||
var createIndex = await client.Http.PutAsync<DynamicResponse>("/movies", d => d.SerializableBody(indexBody)); | ||
// OR | ||
var createIndex = await client.LowLevel.Http.PutAsync<DynamicResponse>("/movies", PostData.Serializable(indexBody)); | ||
|
||
Console.WriteLine($"Create Index: {createIndex.Success && (bool)createIndex.Body.acknowledged}"); | ||
``` | ||
|
||
### POST | ||
|
@@ -45,25 +65,30 @@ var query = new | |
query = new { multi_match = new { query = q, fields = new[] { "title^2", "director" } } } | ||
}; | ||
|
||
var search = await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.POST, $"/{indexName}/_search", CancellationToken.None, PostData.Serializable(query)); | ||
Debug.Assert(search.Success, search.DebugInformation); | ||
var search = await client.Http.PostAsync<DynamicResponse>("/movies/_search", d => d.SerializableBody(query)); | ||
// OR | ||
var search = await client.LowLevel.Http.PostAsync<DynamicResponse>("/movies/_search", PostData.Serializable(query)); | ||
|
||
foreach (var hit in search.Body.hits.hits) Console.WriteLine(hit["_source"]["title"]); | ||
foreach (var hit in search.Body.hits.hits) Console.WriteLine($"Search Hit: {hit["_source"]["title"]}"); | ||
``` | ||
|
||
### DELETE | ||
The following example deletes an index. | ||
|
||
```csharp | ||
var deleteDocument = await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.DELETE, $"/{indexName}/_doc/{id}", CancellationToken.None); | ||
Debug.Assert(deleteDocument.Success, deleteDocument.DebugInformation); | ||
var deleteIndex = await client.Http.DeleteAsync<DynamicResponse>("/movies"); | ||
// OR | ||
var deleteIndex = await client.LowLevel.Http.DeleteAsync<DynamicResponse>("/movies"); | ||
|
||
Console.WriteLine($"Delete Index: {deleteIndex.Success && (bool)deleteIndex.Body.acknowledged}"); | ||
``` | ||
|
||
## Using Different Types Of PostData | ||
The OpenSearch .NET client provides a `PostData` class that is used to provide the request body for a request. The `PostData` class has several static methods that can be used to create a `PostData` object from different types of data. | ||
## Request Bodies | ||
For the methods that take a request body (PUT/POST/PATCH) it is possible use several different types to specify the body. The high-level methods provided in `OpenSearch.Client` provide overloaded fluent-methods for setting the body. While the lower level methods in `OpenSearch.Net` accept instances of `PostData`. | ||
The `PostData` class has several static methods that can be used to create a `PostData` object from different types of data. | ||
|
||
### PostData.String | ||
The following example shows how to use the `PostData.String` method to create a `PostData` object from a string. | ||
### String | ||
The following example shows how to pass a string as a request body: | ||
|
||
```csharp | ||
string indexBody = @" | ||
|
@@ -75,11 +100,13 @@ string indexBody = @" | |
} | ||
}}"; | ||
|
||
await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.PUT, "/movies", CancellationToken.None, PostData.String(indexBody)); | ||
await client.Http.PutAsync<DynamicResponse>("/movies", d => d.Body(indexBody)); | ||
// OR | ||
await client.LowLevel.Http.PutAsync<DynamicResponse>("/movies", PostData.String(indexBody)); | ||
``` | ||
|
||
### PostData.Bytes | ||
The following example shows how to use the `PostData.Bytes` method to create a `PostData` object from a byte array. | ||
### Bytes | ||
The following example shows how to pass a byte array as a request body: | ||
|
||
```csharp | ||
byte[] indexBody = Encoding.UTF8.GetBytes(@" | ||
|
@@ -91,11 +118,13 @@ byte[] indexBody = Encoding.UTF8.GetBytes(@" | |
} | ||
}}"); | ||
|
||
await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.PUT, "/movies", CancellationToken.None, PostData.Bytes(indexBody)); | ||
await client.Http.PutAsync<DynamicResponse>("/movies", d => d.Body(indexBody)); | ||
// OR | ||
await client.LowLevel.Http.PutAsync<DynamicResponse>("/movies", PostData.Bytes(indexBody)); | ||
``` | ||
|
||
### PostData.Serializable | ||
The following example shows how to use the `PostData.Serializable` method to create a `PostData` object from a serializable object. | ||
### Serializable | ||
The following example shows how to pass an object that will be serialized to JSON as a request body: | ||
|
||
```csharp | ||
var indexBody = new | ||
|
@@ -109,12 +138,14 @@ var indexBody = new | |
} | ||
}; | ||
|
||
await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.PUT, "/movies", CancellationToken.None, PostData.Serializable(indexBody)); | ||
await client.Http.PutAsync<DynamicResponse>("/movies", d => d.SerializableBody(indexBody)); | ||
// OR | ||
await client.LowLevel.Http.PutAsync<DynamicResponse>("/movies", PostData.Serializable(indexBody)); | ||
``` | ||
|
||
### PostData.MultiJson | ||
The following example shows how to use the `PostData.MultiJson` method to create a `PostData` object from a collection of serializable objects. | ||
The `PostData.MultiJson` method is useful when you want to send multiple documents in a bulk request. | ||
### Multi JSON | ||
The following example shows how to pass a collection of objects (or strings) that will be serialized to JSON and then newline-delimited as a request body. | ||
This formatting is primarily used when you want to make a bulk request. | ||
|
||
```csharp | ||
var bulkBody = new object[] | ||
|
@@ -125,5 +156,15 @@ var bulkBody = new object[] | |
new { title = "The Godfather: Part II", director = "Francis Ford Coppola", year = 1974 } | ||
}; | ||
|
||
await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.POST, "/_bulk", CancellationToken.None, PostData.MultiJson(bulkBody)); | ||
await client.Http.PostAsync<DynamicResponse>("/_bulk", d => d.MultiJsonBody(indexBody)); | ||
// OR | ||
await client.LowLevel.Http.PostAsync<DynamicResponse>("/_bulk", PostData.MultiJson(indexBody)); | ||
``` | ||
|
||
## Response Bodies | ||
There are a handful of response type implementations that can be used to retrieve the response body. These are specified as the generic argument to the request methods. The content of the body will then be available via the `Body` property of the response object. The following response types are available: | ||
|
||
- `VoidResponse`: The response body will not be read. Useful when you only care about the response status code, such as a `HEAD` request to check an index exists. | ||
- `StringResponse`: The response body will be read into a string. | ||
- `BytesResponse`: The response body will be read into a byte array. | ||
- `DynamicResponse`: The response body will be deserialized as JSON into a dynamic object. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
using System.Diagnostics; | ||
using OpenSearch.Client; | ||
using OpenSearch.Net; | ||
|
||
namespace Samples.RawJson; | ||
|
||
public class RawJsonHighLevelSample : Sample | ||
{ | ||
public RawJsonHighLevelSample() : base("raw-json-high-level", "A sample demonstrating how to use the high-level client to perform raw JSON requests") { } | ||
|
||
protected override async Task Run(IOpenSearchClient client) | ||
{ | ||
var info = await client.Http.GetAsync<DynamicResponse>("/"); | ||
Debug.Assert(info.Success, info.DebugInformation); | ||
Console.WriteLine($"Welcome to {info.Body.version.distribution} {info.Body.version.number}!"); | ||
|
||
const string indexName = "movies"; | ||
|
||
// Check if the index already exists | ||
|
||
var indexExists = await client.Http.HeadAsync<DynamicResponse>($"/{indexName}"); | ||
Debug.Assert(indexExists.HttpStatusCode == 404, indexExists.DebugInformation); | ||
Console.WriteLine($"Index Exists: {indexExists.HttpStatusCode == 200}"); | ||
|
||
// Create an index | ||
|
||
var indexBody = new { settings = new { index = new { number_of_shards = 4 } } }; | ||
|
||
var createIndex = await client.Http.PutAsync<DynamicResponse>($"/{indexName}", d => d.SerializableBody(indexBody)); | ||
Debug.Assert(createIndex.Success && (bool)createIndex.Body.acknowledged, createIndex.DebugInformation); | ||
Console.WriteLine($"Create Index: {createIndex.Success && (bool)createIndex.Body.acknowledged}"); | ||
|
||
// Add a document to the index | ||
var document = new { title = "Moneyball", director = "Bennett Miller", year = 2011 }; | ||
|
||
const string id = "1"; | ||
|
||
var addDocument = await client.Http.PutAsync<DynamicResponse>( | ||
$"/{indexName}/_doc/{id}", | ||
d => d | ||
.SerializableBody(document) | ||
.QueryString(qs => qs.Add("refresh", true))); | ||
Debug.Assert(addDocument.Success, addDocument.DebugInformation); | ||
|
||
// Search for a document | ||
const string q = "miller"; | ||
|
||
var query = new | ||
{ | ||
size = 5, | ||
query = new { multi_match = new { query = q, fields = new[] { "title^2", "director" } } } | ||
}; | ||
|
||
var search = await client.Http.PostAsync<DynamicResponse>($"/{indexName}/_search", d => d.SerializableBody(query)); | ||
Debug.Assert(search.Success, search.DebugInformation); | ||
|
||
foreach (var hit in search.Body.hits.hits) Console.WriteLine($"Search Hit: {hit["_source"]["title"]}"); | ||
|
||
// Delete the document | ||
var deleteDocument = await client.Http.DeleteAsync<DynamicResponse>($"/{indexName}/_doc/{id}"); | ||
Debug.Assert(deleteDocument.Success, deleteDocument.DebugInformation); | ||
Console.WriteLine($"Delete Document: {deleteDocument.Success}"); | ||
|
||
// Delete the index | ||
var deleteIndex = await client.Http.DeleteAsync<DynamicResponse>($"/{indexName}"); | ||
Debug.Assert(deleteIndex.Success && (bool)deleteIndex.Body.acknowledged, deleteIndex.DebugInformation); | ||
Console.WriteLine($"Delete Index: {deleteIndex.Success && (bool)deleteIndex.Body.acknowledged}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
using System.Diagnostics; | ||
using OpenSearch.Client; | ||
using OpenSearch.Net; | ||
using OpenSearch.Net.Specification.HttpApi; | ||
|
||
namespace Samples.RawJson; | ||
|
||
public class RawJsonLowLevelSample : Sample | ||
{ | ||
public RawJsonLowLevelSample() : base("raw-json-low-level", "A sample demonstrating how to use the low-level client to perform raw JSON requests") { } | ||
|
||
protected override async Task Run(IOpenSearchClient client) | ||
{ | ||
var info = await client.LowLevel.Http.GetAsync<DynamicResponse>("/"); | ||
Debug.Assert(info.Success, info.DebugInformation); | ||
Console.WriteLine($"Welcome to {info.Body.version.distribution} {info.Body.version.number}!"); | ||
|
||
const string indexName = "movies"; | ||
|
||
// Check if the index already exists | ||
|
||
var indexExists = await client.LowLevel.Http.HeadAsync<DynamicResponse>($"/{indexName}"); | ||
Debug.Assert(indexExists.HttpStatusCode == 404, indexExists.DebugInformation); | ||
Console.WriteLine($"Index Exists: {indexExists.HttpStatusCode == 200}"); | ||
|
||
// Create an index | ||
|
||
var indexBody = new { settings = new { index = new { number_of_shards = 4 } } }; | ||
|
||
var createIndex = await client.LowLevel.Http.PutAsync<DynamicResponse>($"/{indexName}", PostData.Serializable(indexBody)); | ||
Debug.Assert(createIndex.Success && (bool)createIndex.Body.acknowledged, createIndex.DebugInformation); | ||
Console.WriteLine($"Create Index: {createIndex.Success && (bool)createIndex.Body.acknowledged}"); | ||
|
||
// Add a document to the index | ||
var document = new { title = "Moneyball", director = "Bennett Miller", year = 2011}; | ||
|
||
const string id = "1"; | ||
|
||
var addDocument = await client.LowLevel.Http.PutAsync<DynamicResponse>( | ||
$"/{indexName}/_doc/{id}", | ||
PostData.Serializable(document), | ||
new HttpPutRequestParameters{ QueryString = {{"refresh", true}} }); | ||
Debug.Assert(addDocument.Success, addDocument.DebugInformation); | ||
|
||
// Search for a document | ||
const string q = "miller"; | ||
|
||
var query = new | ||
{ | ||
size = 5, | ||
query = new { multi_match = new { query = q, fields = new[] { "title^2", "director" } } } | ||
}; | ||
|
||
var search = await client.LowLevel.Http.PostAsync<DynamicResponse>($"/{indexName}/_search", PostData.Serializable(query)); | ||
Debug.Assert(search.Success, search.DebugInformation); | ||
|
||
foreach (var hit in search.Body.hits.hits) Console.WriteLine($"Search Hit: {hit["_source"]["title"]}"); | ||
|
||
// Delete the document | ||
var deleteDocument = await client.LowLevel.Http.DeleteAsync<DynamicResponse>($"/{indexName}/_doc/{id}"); | ||
Debug.Assert(deleteDocument.Success, deleteDocument.DebugInformation); | ||
Console.WriteLine($"Delete Document: {deleteDocument.Success}"); | ||
|
||
// Delete the index | ||
var deleteIndex = await client.LowLevel.Http.DeleteAsync<DynamicResponse>($"/{indexName}"); | ||
Debug.Assert(deleteIndex.Success && (bool)deleteIndex.Body.acknowledged, deleteIndex.DebugInformation); | ||
Console.WriteLine($"Delete Index: {deleteIndex.Success && (bool)deleteIndex.Body.acknowledged}"); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixing this :)