Netcore Elasticsearch (NEST) DefaultIndex is UriEncoded - c#

Trying to get a basic Elasticsearch Count to work in my netcore API.
Following the documentation I believe this should work-
var node = new Uri(host);
var settings = new ConnectionSettings(node);
settings.DefaultIndex("foo/bar");
var client = new ElasticClient(settings);
var x = await client.CountAsync<dynamic>();
return x.Count;
There is a problem with this, however. The DefaultIndex is encoding slashes (/foo%2Fbar/object/_count). I'm getting a no_index_found_exception exception.
It seems like an easy problem to fix, but I can't find the documentation for this.
How do I prevent uri encoding on my DefaultIndex?

To specify multiple indices as the default index, separate them with a comma. NEST will URI encode the comma
var defaultIndex = "users,posts";
var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
.DefaultIndex(defaultIndex)
.DefaultTypeName("doc");
var client = new ElasticClient(settings);
var searchResponse = client.Search<object>(s => s);
Will send a search request to
POST http://localhost:9200/users%2Cposts/doc/_search

Related

INVALID_REQUEST_BODY Error calling ListStatusAsync using C# DocuSign SDK

I am using C# DocuSign SDK. I am simply trying to retrieve Envelopes, so using EnvelopesApi.ListStatusAsync. Like this:
EnvelopesApi envelopeApi = new EnvelopesApi(ApiClient.Configuration);
var options = new ListStatusOptions();
var date = DateTime.Now.AddDays(-30);
options.fromDate = date.ToString("yyyy/MM/dd");
var envelopesList = envelopeApi.ListStatusAsync(AccountId, null, options);
Response:
Error calling ListStatus:
{
"errorCode": "INVALID_REQUEST_BODY",
"message": "The request body is missing or improperly formatted."
}
Fiddler shows a 400. I can see Access Token is included in the request (Bearer Authorization Header), so no issues there. Fiddler shows PUT request:
https://demo.docusign.net/restapi/v2.1/accounts/[Account_ID_Guid]/envelopes/status?from_date=2019%2f12%2f14
Basically, this is code retrieved from: https://github.com/docusign/qs-csharp. Only difference is using ListStatusAsync instead of ListStatus. Am I missing something related to Body?
Your code doesn't quite match what the QuickStart example does. In the QS, the method used is ListStatusChanges, not ListStatus.
Try this to get a list of envelopes from the past 30 days:
EnvelopesApi envelopesApi = new EnvelopesApi(apiClient.Configuration);
var options = new EnvelopesApi.ListStatusChangesOptions();
var date = DateTime.Now.AddDays(-30);
options.fromDate = date.ToString("yyyy/MM/dd");
var envelopesList = envelopesApi.ListStatusChanges(accountId, options);
var envelopesListFromAsync = envelopesApi.ListStatusChangesAsync(accountId, options);

Append a Stringsegment to a MS Graph Request with GraphServiceClient

Is it possible to append a segment to a MS Graph GraphServiceClient Request and fetch that resource?
The scenario:
I want to get the root site of a group (more specifically its weburl property)
https://graph.microsoft.com/v1.0/groups/{group-id}/sites/root
but it is not possible to append the /root segment with the QueryBuilder and enumerating sites is not allowed and throws an exception
var task = graphClient.Groups[group.Id].Sites.Request().GetAsync() // exception
I can get the string for the request
var url = graphClient.Groups[group.Id].Sites.Request().AppendSegmentToRequestUrl("root")
But then I would need a method to which I can supply a full Graph Url, for example:
graphClient.MakeRequest(url).GetAsync()
I know I could use the HttpClient Class but that would introduce a different pattern to fetch Graph Resources and I would like to avoid that.
Edit - Solution
Seems as if you have to play with the RequestBuilders that are available under the Microsoft.Graph namespace until you find one that matches the type of your request, all the others return null.
var requestBuilder = client.Groups["guid-of-group"].Sites;
var url = requestBuilder.AppendSegmentToRequestUrl("root");
GroupRequestBuilder builder = new GroupRequestBuilder(url, client);
var result = await builder.Request().GetAsync();
Perharps you could try something like this and pass in the graphServiceClient and created url to a new instance of a request builder.
// create the url from the builders
var requestBuilder = graphClient.Groups["groupId"].Sites;
var url = request.AppendSegmentToRequestUrl("root");
// we have to create a new builder as the url property cannot be set/modified publicly
GroupSitesCollectionRequestBuilder groupSitesCollectionRequestBuilder = new GroupSitesCollectionRequestBuilder(url, graphClient);
// Make the call
var result = await groupSitesCollectionRequestBuilder.Request().GetAsync();

C# How to use Uri equal?

Some Test:
This is Ture:
[Fact]
public void UriEqualTest()
{
//Act
var uri1 = new Uri("https://www.baidu.com");
var uri2 = new Uri("https://www.baidu.com/");
var boolResult = uri2.Equals(uri1);
//Assert
Assert.Equal(uri1, uri2);
Assert.True(boolResult);//True
}
This is Ture:
[Fact]
public void UriUpperEqualTest()
{
//Act
var uri1 = new Uri("https://wWw.bAidu.com");
var uri2 = new Uri("https://www.baidu.com/");
var boolResult = uri2.Equals(uri1);
var operatorResult = (uri1 == uri2);
//Assert
Assert.Equal(uri1, uri2);
Assert.True(boolResult);//True
}
This is False:
[Fact]
public void UrlEqualTest()
{
//Act
var uri1 = new Uri("https://www.baidu.com/aaaa/bbbb");
var uri2 = new Uri("https://www.baidu.com/aaaa/bbbb/");
var boolResult = uri2.Equals(uri1);
//Assert
Assert.Equal(uri1, uri2);
Assert.True(boolResult);//False
}
This is False:
[Fact]
public void UrlUpperEqualTest()
{
//Act
var uri1 = new Uri("https://www.baidu.com/AAaa/bbbb");
var uri2 = new Uri("https://www.baidu.com/aAAa/bbbb");
var boolResult = uri2.Equals(uri1);
var operatorResult = (uri1 == uri2);
//Assert
Assert.Equal(uri1, uri2);
Assert.True(boolResult);//False
}
This is True:
[Fact]
public void UriUpperEqualAndPathTest()
{
//Act
var uri1 = new Uri("https://www.baiDu.com/aaaa/bbbb");
var uri2 = new Uri("https://www.Baidu.com/aaaa/bbbb");
var boolResult = uri2.Equals(uri1);
//Assert
Assert.Equal(uri1, uri2);
Assert.True(boolResult);//True
}
So,The Host not case sensitive? but path case sensitive??
And I want all Uri dot not case sensitive and dot not case '/',What should I do?
And I want all Uri dot not case sensitive and dot not case '/',What should I do?
And I want all Uri dot not case sensitive and dot not case '/',What should I do?
And in aspnet core mvc, if i use route
[HttpGet("/private/head")] and [HttpGet("/private/HeAd")] and [HttpGet("/private/head/")]
It's error! the error is:
AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:
https://stackoverflow.com/a/2581418/34092 states:
As far as the protocol is concerned, http://example.com/something and
http://example.com/something/ are quite different. Some servers might
redirect you from one to the other if it is implemented in such a way.
As for the pure domain names, it always sends a request ending with a
slash. (The domain name itself is not included in the path section of
an HTTP request, just as Greg Hewgill and the others wrote. It is,
however, included in the headers.)
So, looking at your examples:
var uri1 = new Uri("https://www.baidu.com");
var uri2 = new Uri("https://www.baidu.com/");
They are the same, since always sends a request ending with a slash. They are thus equivalent.
https://serverfault.com/a/261344 states:
Names resolved from DNS are case insensitive. This is important to
prevent confusion.
var uri1 = new Uri("https://wWw.bAidu.com");
var uri2 = new Uri("https://www.baidu.com/");
Thus, the two are equivalent (since they differ only by case and the slash immediately after the host).
var uri1 = new Uri("https://www.baidu.com/aaaa/bbbb");
var uri2 = new Uri("https://www.baidu.com/aaaa/bbbb/");
OK, this seems like the first scenario, but it isn't. The first scenario treats them as equivalent since it is 'pure domain name' (i.e. straight after the host). This is different (i.e. the slash is at the end, not straight after the host), and thus they aren't equivalent (on all web servers). Thus not equal.
var uri1 = new Uri("https://www.baidu.com/AAAaa/bbbb");
var uri2 = new Uri("https://www.baidu.com/aAAa/bbbb");
The path and querystring are case sensitive. Thus these are not equal. Some web servers / programming environments (e.g. ASP.NET MVC) may act case-insensitive, but according to the spec the path and querystring are case sensitive (since some web servers are case sensitive).
var uri1 = new Uri("https://www.baiDu.com/aaaa/bbbb");
var uri2 = new Uri("https://www.Baidu.com/aaaa/bbbb");
The only difference is the case of the host. Thus they are equal.
It's error! the error is: AmbiguousActionException: Multiple actions
matched. The following actions matched route data and had all
constraints satisfied:
This is because ASP.NET MVC is generally not case sensitive. Force case-sensitive routing in ASP.NET MVC may be useful for this part of your problem.

Elastic search bulk index with C# API

I want to bulk index json records to elastic search with NEST or Elasticsearch.Net API
My json data is :
{"index":{"_index":"abc","_type":"abc","_id":1}},{"Name":"ApplicationFrameHost","CPU":1.25,"Company":null,"Product":null,"Path":null},{"index":{"_index":"abc","_type":"abc","_id":2}},{"Name":"Calculator","CPU":0.5,"Company":null,"Product":null,"Path":null},{"index":{"_index":"abc","_type":"abc","_id":3}},{"Name":"chrome","CPU":142.9375,"Company":null,"Product":null,"Path":null},{"index":{"_index":"abc","_type":"abc","_id":4}},{"Name":"chrome","CPU":3336.34375,"Company":null,"Product":null,"Path":null},{"index":{"_index":"abc","_type":"abc","_id":5}},{"Name":"chrome","CPU":7.1875,"Company":null,"Product":null,"Path":null}\n\n
my code:
var connectionSettings = new ConnectionSettings(new
Uri("http://localhost:9200/"));
var client2 = new ElasticClient(connectionSettings);
var jsonPostData = new PostData<object>(myJson);
var bulkRequestParameters = new BulkRequestParameters
{
};
Func<BulkRequestParameters, BulkRequestParameters> convert = delegate
(BulkRequestParameters s)
{
s.ErrorTrace(true);
return s.Refresh(Refresh.True);
};
var response = client2.LowLevel.Bulk<VoidResponse>("abc", "abc",
jsonPostData, convert);
In the response elastic return success with no error but still, data is not available on elastic?
Debug info from elastic:Successful low level call on POST: /abc/abc/_bulk?error_trace=true&refresh=true
It would be very helpful if someone can provide any clue what I am doing wrong here?
Solved this by modifying input JSON format after each record-set it does not requires comma:
{"index":{"_index":"abc","_type":"abc","_id":1}}{"Name":"ApplicationFrameHost","CPU":1.25,"Company":null,"Product":null,"Path":null}{"index":{"_index":"abc","_type":"abc","_id":2}},{"Name":"audiodg","CPU":1.5625,"Company":null,"Product":null,"Path":null}{"index":{"_index":"abc","_type":"abc","_id":3}},{"Name":"Calculator","CPU":0.5,"Company":null,"Product":null,"Path":null}{"index":{"_index":"abc","_type":"abc","_id":4}},{"Name":"chrome","CPU":144.109375,"Company":null,"Product":null,"Path":null}{"index":{"_index":"abc","_type":"abc","_id":5}},{"Name":"chrome","CPU":3384.609375,"Company":null,"Product":null,"Path":null}

Error: "Photo data or source id must be included" when I try to Upload a photo to PicasaWeb in Gdata .NET

Somebody know what I'm doing wrong?
var service = new Service("lh2", "exampleCo-exampleApp-1");
service.setUserCredentials("myuser", "mypass");
var myPhoto = new PhotoEntry
{
Title = new AtomTextConstruct(
AtomTextConstructElementType.Title,
"Puppies FTW")
};
var myMedia = new MediaFileSource(#"C:\puppies.jpg", "image/jpeg");
myPhoto.MediaSource = myMedia;
var u="https://picasaweb.google.com/data/feed/api/user/default/albumid/default";
var returnedPhoto = service.Insert(new Uri(u), myPhoto);
Actually is returning bad request error (400) with the following response string: Photo data or source id must be included
It was returning error because the Uri was wrong:
I must use:
var u="https://picasaweb.google.com/data/media/api/user/default/albumid/default";
instead:
var u="https://picasaweb.google.com/data/feed/api/user/default/albumid/default";

Categories