RestSharp AddParameter - c#

I want to make a RestSharp request and the following desired result is like :
tes = {
"paye": "0",
"type": "0",
"lines": [
{
"desc": "1",
"note": "10",
},
{
"desc": "2",
"note": "20",
},
{
"desc": "3",
"note": "30",
}
]
}
so I make a request like this :
var client = new RestClient(url);
var request = new RestRequest("tes", Method.POST);
request.AddParameter("paye", 0);
request.AddParameter("type", 0);
and the problem is how to finish the lines[] part?
Thank you

Create class like :
class tes {
//include all the properties here
}
Serialize the object and pass it in parameter with the RestSharp request.

Related

UpdateExpression to flatten the record

I have such items in Dynamo DB:
[{
"Id": "1",
"Data": {
"Value": "test"
}
},
{
"Id": "2",
"Data": {}
},
{
"Id": "3"
},
{
"Id": "4",
"Data": {
"Wrong": "234"
}
}]
And I'm trying to make it flatten, but for the Data.Value field only:
[{
"Id": "1",
"Value": "test"
},
{
"Id": "2"
},
{
"Id": "3"
},
{
"Id": "4"
}]
My Update request looks like this:
var request = new UpdateItemRequest {
TableName = "<table>",
Key = new Dictionary<string, AttributeValue> {{"Id", new AttributeValue("<item-id>")}},
UpdateExpression = "SET #a = #c.#a REMOVE #c",
ExpressionAttributeNames = new Dictionary<string, string> {
{"#c", "Data"},
{"#a", "Value"}
}
};
This works well for Id = 1 and 3. But does not work for 2 and 4. I assume because it can not do a SET. It does not throw any errors, but simply does not delete the Data attribute.
Is there a way to make it in a single call?
you just need a condition with your expression, to check if the attribute exist or not, thats it.
ConditionExpression ="attribute_exists (#c.#a)"
For more détails in Amazon doncs here.

How to upload/index a GeoJson file in Elasticsearch using NEST(C#)

I have GeoJson File with me which i want to index on Elastic Search through NEST
But Due to lack of expertise I am having trouble in indexing the document
I have created a class which represent the Mapping on ElasticSearch:
public class GeoDocument
{
[Nest.Keyword(Name = "DocId")]
public string DocId { get; set; }
[Nest.GeoShape(Name = "GeoField")]
public object GeoField { get; set; }
}
But when i use this Mapping to index a Document
var polygon = "{\"type\":\"Polygon\",\"coordinates\":[[[5.856956,51.002753],[5.856928,51.002771],[5.856687,51.002853],[5.856956,51.002753]]]}";
var geoDocument = new GeoJson
{
DocId = "1",
GeoField = JsonConvert.DeserializeObject<object>(polygon)
};
var IndexResponse = client.IndexDocument(geoDocument);
I get a Result Something like This
"_source": {
"DocId": "1",
"GeoField": [
[
[]
],
[
[
[
[
[],
[]
],
[
[],
[]
],
[
[],
[]
],
[
[],
[]
]
]
]
]
]
}
}
In order to make that JObject saved correctly, you have to tell the ElasticClient to use NewtonSoft .Net serializer.
Install NEST.JsonNetSerializer package
Reference the JsonNetSerializer in your ConnectionSettings
If you get 400 after changing the settings, you might need to create a new Index.
Sample code
using Nest;
using Elasticsearch.Net;
using Nest.JsonNetSerializer;
SingleNodeConnectionPool node = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
ConnectionSettings settings = new ConnectionSettings(
node,
JsonNetSerializer.Default
);
settings.DefaultMappingFor<GeoDocument>(m => m.IndexName("project2"));
ElasticClient client = new ElasticClient(settings);
// This is Supposed to be GeoDocument as per your question.
GeoDocument geoDocument = new GeoDocument
{
DocId = "1",
GeoField = JObject.Parse(polygon)
// GeoField = JsonConvert.DeserializeObject<object>(polygon) // <-- Works too.
};
IndexResponse IndexResponse = client.IndexDocument(geoDocument);
Response
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "project2",
"_type": "_doc",
"_id": "COQRXW8BNG2RJmIOyoO0",
"_score": 1.0,
"_source": {
"DocId": "1",
"GeoField": {
"type": "Polygon",
"coordinates": [
[
[
5.856956,
51.002753
],
[
5.856928,
51.002771
],
[
5.856687,
51.002853
],
[
5.856956,
51.002753
]
]
]
}
}
}
]
}
}

how to post array json .net?

i am inserting json format but need to know how i can insert in this format as mention below
{
"products": [
{
"product_id": "55",
"name": "testing data",
"price": "77",
"total": "77",
"quantity": "1",
"type": "kg"
}],
],
"totals": [
{
"code": "sub_total",
"title": "Sub-Total",
"text": "Rs277.00",
"value": "277.0000",
"sort_order": "1"
}]
}
here is my code which i am trying
items local = new items();
foreach (var item in _LocalItem){
local = new items
{
name = item.name
};
}
var json = JsonConvert.SerializeObject(local);
var request = new HttpRequestMessage(HttpMethod.Post, "http://orangepotato.rjcpacking.com/index.php?route=api/login/addcustomerOrder");
request.Content = new StringContent(json);
i dont understand where i can add "products" array in json format
Simply: don't serialize an array - serialize something that has an array in a member called products:
var json = JsonConvert.SerializeObject(new { products = local });

Create Graph Event with Room as location using C# and Microsoft.Graph nuget

Not sure how to do the location with a room in C# and Microsoft.Graph.
I want to create an event that would put the event in a room. I now the room’s email - Rainier#M365x947151.onmicrosoft.com . The code I am trying is as follows using Microsoft.Graph.
Microsoft.Graph.PhysicalAddress address = new Microsoft.Graph.PhysicalAddress();
Microsoft.Graph.Location loc = new Location();
loc.Address = address;
loc.DisplayName = "Rainier conf room";
loc.LocationEmailAddress = rainier;
var newEvent = new Event();
newEvent.Subject = subject + DateTime.Now.ToLongDateString();
newEvent.Location = loc;
newEvent.Attendees = attendees;
newEvent.Body = eventBody;
newEvent.Start = eventStartTime;
newEvent.End = eventEndTime;
Microsoft.Graph.Event createdEvent = null;
try
{
// graphclient is passed into this method
// var graphClient = AuthenticationHelper.GetAuthenticatedClient();
// var graphClient = devfish.Graph.AuthenticationHelper.MyGraphClient;
createdEvent = await graphClient.Me.Events.Request().AddAsync(newEvent);
The payload I am sending up SHOULD look something like this, but what it doesn’t look like is below. Outlook doesn’t treat it as a “room”. Thanks…
PAYLOAD WE WANT - note the odatatype and microsoft.graph.physicaladdress ...
{
"subject": "Test meeting",
"body": {
"contentType": "HTML",
"content": "Does this work (note the dates are in the past)?"
},
"start": {
"dateTime": "2017-12-01T12:00:00",
"timeZone": "Pacific Standard Time"
},
"end": {
"dateTime": "2017-12-01T14:00:00",
"timeZone": "Pacific Standard Time"
},
"location":{
"address": {"#odata.type": "microsoft.graph.physicalAddress"},
"displayName": "Rainier conf room"
},
"attendees": [
{
"emailAddress": {
"address":"joseph.healy#microsoft.com",
"name": "Joe"
},
"type": "required"
},
{
"emailAddress": {
"address":"Rainier#M365x947151.onmicrosoft.com",
"name": "Rainier"
},
"type": "Resource"
}
]
}
But instead up payload looks like this when created with C# graph.
{
"subject": "23: 42:39: BASKETBALL IS OUR SUBJECT FOR TODAYTuesday, January 9, 2018",
"body": {
"contentType": "text",
"content": "Status updates, blocking issues, and next steps"
},
"start": {
"dateTime": "2017-12-01T19:30:00.0000000",
"timeZone": "UTC"
},
"end": {
"dateTime": "2017-12-01T20:00:00.0000000",
"timeZone": "UTC"
},
"location": {
"displayName": "Rainier conf room",
"locationEmailAddress": "Rainier#M365x947151.onmicrosoft.com",
"address": {}
},
"attendees": [
{
"type": "required",
"emailAddress": {
"address": "alexw#m365x947151.onmicrosoft.com"
}
},
{
"type": "required",
"emailAddress": {
"address": "maria#fabrikam.com"
}
},
{
"type": "resource",
"emailAddress": {
"address": "Rainier#M365x947151.onmicrosoft.com"
}
}
]
}
Thanks for any help.
To get the payload you want, you'll need to:
Remove loc.LocationEmailAddress = rainier;
Add the key-value "#odata.type", "microsoft.graph.physicalAddress" to
location.AdditionalData. We had someone else recently ask for
automatic #odata.type generation so this is an additional data point
for that.
Add the name property to the room emailAddress object.

Newtonsoft JSON - create JArray in JArray

I am trying to create JSON array using Newtonsoft JSON API but its giving me error. I want to achieve structure like
[
{
"id":"26",
"appsurvey":"1",
"fk_curriculumid":"70",
"status":"Completed",
"lastaccessedon":"2014-06-20 09:18:54",
"questions":[
{
"feedback":"6",
"questionid":"1"
},
{
"feedback":"8",
"questionid":"2"
},
{
"feedback":"1",
"questionid":"3"
}
],
"fk_clientid":"24",
"learnerid":"260"
}
]
I want ot add questions array for multiple time but it is giving me error
Can not add property questions to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.
Here is my code:
JArray surveytrackingA = new JArray();
/*code to add
[
{"id":"26",
"appsurvey":"1",
"fk_curriculumid":"70",
"status":"Completed",
"lastaccessedon":"2014-06-20 09:18:54"}]
*/
for (int i = 0; i < surveytrackingA.Count; i++)
{
JObject surveytrackD = (JObject)surveytrackingA[i];
string queryOne = "select * from table101 where fk_curriculumid='"
+ surveytrackD["fk_curriculumid"].ToString()
+ "' and fk_surveyid='"
+ surveytrackD["appsurvey"].ToString() + "'";
JArray questionsA = new JArray();
using (var stmt = await App.localDB.PrepareStatementAsync(queryOne))
{
while (await stmt.StepAsync())
{
JObject questionD = new JObject();
questionD.Add("questionid", stmt.GetTextAt(5));
questionD.Add("feedback", stmt.GetTextAt(6));
questionsA.Add(questionD);
}
}
surveytrackD.Add("questions", questionsA); /*error occurred here when second question array is getting inserted in surveyTrackD*/
surveytrackingA.Add(surveytrackD);
}
Can anyone please correct me. Thanks in advance.
Update:
surveytrackD have the json data,
{
"fk_clientid": "24",
"learnerid": "260",
"appsurvey": "1",
"id": "26",
"fk_curriculumid": "70",
"status": "completed",
"lastaccessedon": "2014-06-20 09:18:54"
}
You can achieve the same result (JArray in JArray) using regular C# classes and at the end serialize to JSon.
I posted a sample in Github; here a fragment of the code that produces your expected output:
var Surveys = new List<SurveytrackD>();
Surveys.Add( new SurveytrackD { id = "26", appsurvey = "1", fk_curriculumid = "70", status = "Completed", learnerid = "240" } );
Surveys.Add( new SurveytrackD { id = "27", appsurvey = "1", fk_curriculumid = "71", status = "Completed", learnerid = "241" });
foreach (var survey in Surveys)
{
survey.questions = new List<Question>();
survey.questions.Add(new Question { questionid = "1", feedback = "0" });
survey.questions.Add(new Question { questionid = "2", feedback = "1" });
}
var json = JsonConvert.SerializeObject(Surveys, Formatting.Indented);
Console.WriteLine(json);
The output is:
[
{
"fk_clientid": null,
"learnerid": "240",
"appsurvey": "1",
"id": "26",
"fk_curriculumid": "70",
"status": "Completed",
"lastaccessedon": null,
"questions": [
{
"feedback": "0",
"questionid": "1"
},
{
"feedback": "1",
"questionid": "2"
}
]
},
{
"fk_clientid": null,
"learnerid": "241",
"appsurvey": "1",
"id": "27",
"fk_curriculumid": "71",
"status": "Completed",
"lastaccessedon": null,
"questions": [
{
"feedback": "0",
"questionid": "1"
},
{
"feedback": "1",
"questionid": "2"
}
]
}
]

Categories

Resources