C# Mailjet SDK: Adding attachment - c#

I am using the official C# Mailjet SDK (https://github.com/mailjet/mailjet-apiv3-dotnet). Works fine so far.
But how do I add attachments?
I see
Mailjet.Client.Resources
has InlineAttachments and Attachments, but how do I use it?
This is the code snippet so far:
MailjetRequest request = new MailjetRequest { Resource = Send.Resource }
.Property(Send.FromEmail, emailOperatable.FromEmailaddress)
.Property(Send.FromName, emailOperatable.FromName)
.Property(Send.Subject, emailOperatable.Subject)
.Property(Send.TextPart, emailOperatable.TextBody)
.Property(Send.HtmlPart, emailOperatable.HtmlBody)
.Property(Send.Recipients, new JArray { new JObject { { "Email", emailOperatable.ContactEmailaddress }, { "Name", emailOperatable.CreateSendToName() } } });
Tried sth. like
request.Property(Send.Attachments, "path/to/file.zip");
But that does not work.
Update
Works like this:
.Property(Send.Attachments, new JArray { new JObject { { "Content-Type", "<content type>" }, { "Filename", "<file name>" }, { "content", "<base 64 encoded content>" } } });

It appears that the naming is a bit different, this worked for me in 2022 with the API v3
Mind the capitalization of some letters (or the lack thereof...)!
Note: content field is base64 encoded filedata.
.Property(
Send.Attachments,
new JArray {
new JObject {
{"Content-type", "text/plain"},
{"Filename", "test.txt"},
{"content", "VGhpcyBpcyB5b3VyIGF0dGFjaGVkIGZpbGUhISEK"}
}
}
)

According to their docs, the way to do it is:
MailjetClient client = new MailjetClient(Environment.GetEnvironmentVariable("MJ_APIKEY_PUBLIC"), Environment.GetEnvironmentVariable("MJ_APIKEY_PRIVATE"))
{
Version = ApiVersion.V3_1,
};
MailjetRequest request = new MailjetRequest
{
Resource = Send.Resource,
}
.Property(Send.Messages, new JArray {
new JObject {
{"From", new JObject {
{"Email", "pilot#mailjet.com"},
{"Name", "Mailjet Pilot"}
}},
{"To", new JArray {
new JObject {
{"Email", "passenger1#mailjet.com"},
{"Name", "passenger 1"}
}
}},
{"Subject", "Your email flight plan!"},
{"TextPart", "Dear passenger 1, welcome to Mailjet! May the delivery force be with you!"},
{"HTMLPart", "<h3>Dear passenger 1, welcome to Mailjet!</h3><br />May the delivery force be with you!"},
{"Attachments", new JArray {
new JObject {
{"ContentType", "text/plain"},
{"Filename", "test.txt"},
{"Base64Content", "VGhpcyBpcyB5b3VyIGF0dGFjaGVkIGZpbGUhISEK"}
}
}}
}
});
MailjetResponse response = await client.PostAsync(request);
if (response.IsSuccessStatusCode)
{
Console.WriteLine(string.Format("Total: {0}, Count: {1}\n", response.GetTotal(), response.GetCount()));
Console.WriteLine(response.GetData());
}
else
{
Console.WriteLine(string.Format("StatusCode: {0}\n", response.StatusCode));
Console.WriteLine(string.Format("ErrorInfo: {0}\n", response.GetErrorInfo()));
Console.WriteLine(response.GetData());
Console.WriteLine(string.Format("ErrorMessage: {0}\n", response.GetErrorMessage()));
}

Related

Microsoft.OpenAPI examples or documentation?

Just tried to use Swashbuckle 5 rc2 + Microsoft OpenAPI implementation but struggling to make sense of how to inject the security requirements using the OpenApiSecurityRequirement via an OperationFilter
I'm converting the OperationFilter from Swashbuckle 4 to Swashbuckle 5 rc2 which makes use of Microsoft's OpenApi. WIthin the Swashbuckle 4 implementation I had OperationFilter (and this allowed me to use both oauth2 implicit flow scopes as well as api_key where I would explicitly set a bearer JTW token in the SwaggerUI:
// Swashbuckle 4 code
operation.Security = new List<Dictionary<String, IEnumerable<String>>>
{
new Dictionary<string, IEnumerable<string>>
{
{ "Bearer", new string[] { } }
},
new Dictionary<string, IEnumerable<string>>
{
{ "oauth2", requiredScopes }
}
};
Not very sure how to use OpenAPI for describing the same security requirements, but in the converted OperationFilter implementation I've basically sought out the endpoints which have the Authorize attribute and read the policy to retrieve the scopes:
if (requiredScopes.Any())
{
operation.Responses.Add("401", new OpenApiResponse { Description = "Unauthorized" });
operation.Responses.Add("403", new OpenApiResponse { Description = "Forbidden" });
OpenApiSecurityRequirement bearerSecurityRequirement = new OpenApiSecurityRequirement();
bearerSecurityRequirement[new OpenApiSecurityScheme()
{
Type = SecuritySchemeType.Http,
Scheme = "Bearer",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Name = "api_key",
}] = new List<String>();
OpenApiSecurityRequirement oauth2SecurityRequirement = new OpenApiSecurityRequirement();
oauth2SecurityRequirement[new OpenApiSecurityScheme()
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows()
{
Implicit = new OpenApiOAuthFlow()
{
AuthorizationUrl = new Uri("<authorization url here>"),
TokenUrl = new Uri("<token url here>"),
Scopes = requiredScopes.ToDictionary(x => x) // TODO: Fix descriptions
}
},
In = ParameterLocation.Header,
Name = "oauth2"
}] = new List<String>(requiredScopes);
operation.Security = new List<OpenApiSecurityRequirement>
{
bearerSecurityRequirement,
oauth2SecurityRequirement
};
}
In the json output of the generated swagger doc/openapi doc for the operation I'm just seeing:
"security" : [
{"api_key": []},
{
"oauth2": [
"",
""
]
}
]
I think I am aiming to generate the following json in terms OpenAPI standard, where api_key and oauth2 are just the names of my security schemes.
"security" : [
{"api_key": []},
{
"oauth2": [
"<scope1>",
"<scope2>"
]
}
]
Are there any docs coming or perhaps some fuller examples that actually demonstrate how to declare protected endpoints for oauth2 and api key approaches?
You need to set the Reference of the OpenApiSecurityScheme.
I ended up with a result just like you, just an empty object.
I tried to add HTTP basic authentication like in the example:
openapi: 3.0.0
...
components:
securitySchemes:
basicAuth: # <-- arbitrary name for the security scheme
type: http
scheme: basic
security:
- basicAuth: [] # <-- use the same name here
And to do that with Microsoft.OpenApi:
var document = new OpenApiDocument
{
Components = new OpenApiComponents
{
SecuritySchemes =
{
{
"basicAuth", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.Http,
Scheme = "basic"
}
}
}
},
SecurityRequirements =
{
new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference()
{
Type = ReferenceType.SecurityScheme,
Id = "basicAuth"
}
},
new List<string>()
}
}
}
};
By looking at the source code of OpenApiSecurityRequirement one can see that it's checking OpenApiSecurityScheme.Reference and not Name:
foreach (var securitySchemeAndScopesValuePair in this)
{
var securityScheme = securitySchemeAndScopesValuePair.Key;
var scopes = securitySchemeAndScopesValuePair.Value;
if (securityScheme.Reference == null)
{
// Reaching this point means the reference to a specific OpenApiSecurityScheme fails.
// We are not able to serialize this SecurityScheme/Scopes key value pair since we do not know what
// string to output.
continue;
}
securityScheme.SerializeAsV3(writer);
writer.WriteStartArray();
foreach (var scope in scopes)
{
writer.WriteValue(scope);
}
writer.WriteEndArray();
}

How to create k8s deployment using kubernetes-client in c#?

I'm getting Microsoft.Rest.HttpOperationException: 'Operation returned an invalid status code 'BadRequest'' on this line.
var result = client.CreateNamespacedDeployment(deployment, namespace);
Kubernetes-client has a small number of good resources and most of them is written in other language such as java and python. So i'm referring to these documentations.
this is my implementation so far.
V1Deployment deployment = new V1Deployment()
{
ApiVersion = "extensions/v1beta1",
Kind = "Deployment",
Metadata = new V1ObjectMeta()
{
Name = "...",
NamespaceProperty = env,
Labels = new Dictionary<string, string>()
{
{ "app", "..." }
}
},
Spec = new V1DeploymentSpec
{
Replicas = 1,
Selector = new V1LabelSelector()
{
MatchLabels = new Dictionary<string, string>
{
{ "app", "..." }
}
},
Template = new V1PodTemplateSpec()
{
Metadata = new V1ObjectMeta()
{
CreationTimestamp = null,
Labels = new Dictionary<string, string>
{
{ "app", "..." }
}
},
Spec = new V1PodSpec
{
Containers = new List<V1Container>()
{
new V1Container()
{
Name = "...",
Image = "...",
ImagePullPolicy = "Always",
Ports = new List<V1ContainerPort> { new V1ContainerPort(80) }
}
}
}
}
},
Status = new V1DeploymentStatus()
{
Replicas = 1
}
};
var result = client.CreateNamespacedDeployment(deployment, namespace);
I want to know the proper way on how to create kubernetes deployment using kubernetes-client, and also i want to know the cause of this issue.
For the full clarity and future visitors, it's worth to mention, what is exactly behind this bad request error (code: 400) returned from API server, when using your code sample:
"the API version in the data (extensions/v1beta1) does not match the expected API version (apps/v1)"
Solution:
ApiVersion = "extensions/v1beta1" -> ApiVersion = "apps/v1"
Full code sample:
private static void Main(string[] args)
{
var k8SClientConfig = new KubernetesClientConfiguration { Host = "http://127.0.0.1:8080" };
IKubernetes client = new Kubernetes(k8SClientConfig);
ListDeployments(client);
V1Deployment deployment = new V1Deployment()
{
ApiVersion = "apps/v1",
Kind = "Deployment",
Metadata = new V1ObjectMeta()
{
Name = "nepomucen",
NamespaceProperty = null,
Labels = new Dictionary<string, string>()
{
{ "app", "nepomucen" }
}
},
Spec = new V1DeploymentSpec
{
Replicas = 1,
Selector = new V1LabelSelector()
{
MatchLabels = new Dictionary<string, string>
{
{ "app", "nepomucen" }
}
},
Template = new V1PodTemplateSpec()
{
Metadata = new V1ObjectMeta()
{
CreationTimestamp = null,
Labels = new Dictionary<string, string>
{
{ "app", "nepomucen" }
}
},
Spec = new V1PodSpec
{
Containers = new List<V1Container>()
{
new V1Container()
{
Name = "nginx",
Image = "nginx:1.7.9",
ImagePullPolicy = "Always",
Ports = new List<V1ContainerPort> { new V1ContainerPort(80) }
}
}
}
}
},
Status = new V1DeploymentStatus()
{
Replicas = 1
}
};
Closing this issue (Resolved)
Reference: https://github.com/Azure/autorest/issues/931
Cause of issue: incorrect version of Kubernetes ApiVersion.
Solution: get and replace ApiVersion from kubernetes api.
Can also handle the exception using:
try
{
var result = client.CreateNamespacedDeployment(deployment, namespace);
}
catch (Microsoft.Rest.HttpOperationException httpOperationException)
{
var phase = httpOperationException.Response.ReasonPhrase;
var content = httpOperationException.Response.Content;
}

How do I indent json data with C#? Json.net [duplicate]

I am using .NET JSON parser and would like to serialize my config file so it is readable. So instead of:
{"blah":"v", "blah2":"v2"}
I would like something nicer like:
{
"blah":"v",
"blah2":"v2"
}
My code is something like this:
using System.Web.Script.Serialization;
var ser = new JavaScriptSerializer();
configSz = ser.Serialize(config);
using (var f = (TextWriter)File.CreateText(configFn))
{
f.WriteLine(configSz);
f.Close();
}
You are going to have a hard time accomplishing this with JavaScriptSerializer.
Try JSON.Net.
With minor modifications from JSON.Net example
using System;
using Newtonsoft.Json;
namespace JsonPrettyPrint
{
internal class Program
{
private static void Main(string[] args)
{
Product product = new Product
{
Name = "Apple",
Expiry = new DateTime(2008, 12, 28),
Price = 3.99M,
Sizes = new[] { "Small", "Medium", "Large" }
};
string json = JsonConvert.SerializeObject(product, Formatting.Indented);
Console.WriteLine(json);
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);
}
}
internal class Product
{
public String[] Sizes { get; set; }
public decimal Price { get; set; }
public DateTime Expiry { get; set; }
public string Name { get; set; }
}
}
Results
{
"Sizes": [
"Small",
"Medium",
"Large"
],
"Price": 3.99,
"Expiry": "\/Date(1230447600000-0700)\/",
"Name": "Apple"
}
Documentation: Serialize an Object
A shorter sample code for Json.Net library
private static string FormatJson(string json)
{
dynamic parsedJson = JsonConvert.DeserializeObject(json);
return JsonConvert.SerializeObject(parsedJson, Formatting.Indented);
}
If you have a JSON string and want to "prettify" it, but don't want to serialise it to and from a known C# type then the following does the trick (using JSON.NET):
using System;
using System.IO;
using Newtonsoft.Json;
class JsonUtil
{
public static string JsonPrettify(string json)
{
using (var stringReader = new StringReader(json))
using (var stringWriter = new StringWriter())
{
var jsonReader = new JsonTextReader(stringReader);
var jsonWriter = new JsonTextWriter(stringWriter) { Formatting = Formatting.Indented };
jsonWriter.WriteToken(jsonReader);
return stringWriter.ToString();
}
}
}
Shortest version to prettify existing JSON: (edit: using JSON.net)
JToken.Parse("mystring").ToString()
Input:
{"menu": { "id": "file", "value": "File", "popup": { "menuitem": [ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"} ] } }}
Output:
{
"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{
"value": "New",
"onclick": "CreateNewDoc()"
},
{
"value": "Open",
"onclick": "OpenDoc()"
},
{
"value": "Close",
"onclick": "CloseDoc()"
}
]
}
}
}
To pretty-print an object:
JToken.FromObject(myObject).ToString()
Oneliner using Newtonsoft.Json.Linq:
string prettyJson = JToken.Parse(uglyJsonString).ToString(Formatting.Indented);
Net Core App
var js = JsonSerializer.Serialize(obj, new JsonSerializerOptions {
WriteIndented = true
});
All this can be done in one simple line:
string jsonString = JsonConvert.SerializeObject(yourObject, Formatting.Indented);
Here is a solution using Microsoft's System.Text.Json library:
static string FormatJsonText(string jsonString)
{
using var doc = JsonDocument.Parse(
jsonString,
new JsonDocumentOptions
{
AllowTrailingCommas = true
}
);
MemoryStream memoryStream = new MemoryStream();
using (
var utf8JsonWriter = new Utf8JsonWriter(
memoryStream,
new JsonWriterOptions
{
Indented = true
}
)
)
{
doc.WriteTo(utf8JsonWriter);
}
return new System.Text.UTF8Encoding()
.GetString(memoryStream.ToArray());
}
You may use following standard method for getting formatted Json
JsonReaderWriterFactory.CreateJsonWriter(Stream stream, Encoding encoding, bool ownsStream, bool indent, string indentChars)
Only set "indent==true"
Try something like this
public readonly DataContractJsonSerializerSettings Settings =
new DataContractJsonSerializerSettings
{ UseSimpleDictionaryFormat = true };
public void Keep<TValue>(TValue item, string path)
{
try
{
using (var stream = File.Open(path, FileMode.Create))
{
//var currentCulture = Thread.CurrentThread.CurrentCulture;
//Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
try
{
using (var writer = JsonReaderWriterFactory.CreateJsonWriter(
stream, Encoding.UTF8, true, true, " "))
{
var serializer = new DataContractJsonSerializer(type, Settings);
serializer.WriteObject(writer, item);
writer.Flush();
}
}
catch (Exception exception)
{
Debug.WriteLine(exception.ToString());
}
finally
{
//Thread.CurrentThread.CurrentCulture = currentCulture;
}
}
}
catch (Exception exception)
{
Debug.WriteLine(exception.ToString());
}
}
Pay your attention to lines
var currentCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
....
Thread.CurrentThread.CurrentCulture = currentCulture;
For some kinds of xml-serializers you should use InvariantCulture to avoid exception during deserialization on the computers with different Regional settings. For example, invalid format of double or DateTime sometimes cause them.
For deserializing
public TValue Revive<TValue>(string path, params object[] constructorArgs)
{
try
{
using (var stream = File.OpenRead(path))
{
//var currentCulture = Thread.CurrentThread.CurrentCulture;
//Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
try
{
var serializer = new DataContractJsonSerializer(type, Settings);
var item = (TValue) serializer.ReadObject(stream);
if (Equals(item, null)) throw new Exception();
return item;
}
catch (Exception exception)
{
Debug.WriteLine(exception.ToString());
return (TValue) Activator.CreateInstance(type, constructorArgs);
}
finally
{
//Thread.CurrentThread.CurrentCulture = currentCulture;
}
}
}
catch
{
return (TValue) Activator.CreateInstance(typeof (TValue), constructorArgs);
}
}
Thanks!
Using System.Text.Json set JsonSerializerOptions.WriteIndented = true:
JsonSerializerOptions options = new JsonSerializerOptions { WriteIndented = true };
string json = JsonSerializer.Serialize<Type>(object, options);
2023 Update
For those who ask how I get formatted JSON in .NET using C# and want to see how to use it right away and one-line lovers. Here are the indented JSON string one-line codes:
There are 2 well-known JSON formatter or parsers to serialize:
Newtonsoft Json.Net version:
using Newtonsoft.Json;
var jsonString = JsonConvert.SerializeObject(yourObj, Formatting.Indented);
.Net 7 version:
using System.Text.Json;
var jsonString = JsonSerializer.Serialize(yourObj, new JsonSerializerOptions { WriteIndented = true });
using System.Text.Json;
...
var parsedJson = JsonSerializer.Deserialize<ExpandoObject>(json);
var options = new JsonSerializerOptions() { WriteIndented = true };
return JsonSerializer.Serialize(parsedJson, options);
First I wanted to add comment under Duncan Smart post, but unfortunately I have not got enough reputation yet to leave comments. So I will try it here.
I just want to warn about side effects.
JsonTextReader internally parses json into typed JTokens and then serialises them back.
For example if your original JSON was
{ "double":0.00002, "date":"\/Date(1198908717056)\/"}
After prettify you get
{
"double":2E-05,
"date": "2007-12-29T06:11:57.056Z"
}
Of course both json string are equivalent and will deserialize to structurally equal objects, but if you need to preserve original string values, you need to take this into concideration
I have something very simple for this. You can put as input really any object to be converted into json with a format:
private static string GetJson<T> (T json)
{
return JsonConvert.SerializeObject(json, Formatting.Indented);
}
This worked for me. In case someone is looking for a VB.NET version.
#imports System
#imports System.IO
#imports Newtonsoft.Json
Public Shared Function JsonPrettify(ByVal json As String) As String
Using stringReader = New StringReader(json)
Using stringWriter = New StringWriter()
Dim jsonReader = New JsonTextReader(stringReader)
Dim jsonWriter = New JsonTextWriter(stringWriter) With {
.Formatting = Formatting.Indented
}
jsonWriter.WriteToken(jsonReader)
Return stringWriter.ToString()
End Using
End Using
End Function
.NET 5 has built in classes for handling JSON parsing, serialization, deserialization under System.Text.Json namespace. Below is an example of a serializer which converts a .NET object to a JSON string,
using System.Text.Json;
using System.Text.Json.Serialization;
private string ConvertJsonString(object obj)
{
JsonSerializerOptions options = new JsonSerializerOptions();
options.WriteIndented = true; //Pretty print using indent, white space, new line, etc.
options.NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals; //Allow NANs
string jsonString = JsonSerializer.Serialize(obj, options);
return jsonString;
}
Below code works for me:
JsonConvert.SerializeObject(JToken.Parse(yourobj.ToString()))
For UTF8 encoded JSON file using .NET Core 3.1, I was finally able to use JsonDocument based upon this information from Microsoft: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to#utf8jsonreader-utf8jsonwriter-and-jsondocument
string allLinesAsOneString = string.Empty;
string [] lines = File.ReadAllLines(filename, Encoding.UTF8);
foreach(var line in lines)
allLinesAsOneString += line;
JsonDocument jd = JsonDocument.Parse(Encoding.UTF8.GetBytes(allLinesAsOneString));
var writer = new Utf8JsonWriter(Console.OpenStandardOutput(), new JsonWriterOptions
{
Indented = true
});
JsonElement root = jd.RootElement;
if( root.ValueKind == JsonValueKind.Object )
{
writer.WriteStartObject();
}
foreach (var jp in root.EnumerateObject())
jp.WriteTo(writer);
writer.WriteEndObject();
writer.Flush();

How to parse a json array json.net

I know it sounds basic but all answers around this question have been stupidly large and bulky code that does not allow the functionality i need. i need to parse this json array.
[
{
"label":"Cow (1)",
"value":3309
},
{
"label":"Cow (1)",
"value":14998
},
{
"label":"Cow (4)",
"value":20969
},
{
"label":"Cow (4)",
"value":20970
},
{
"label":"Cow (4)",
"value":20971
},
{
"label":"Cowardly Bandit",
"value":1886
},
{
"label":"Cow calf (1)",
"value":2310
},
{
"label":"Coward in armour (82)",
"value":5097
},
{
"label":"Coward with bow (105)",
"value":6049
},
{
"label":"Cow calf (1)",
"value":20979
},
{
"label":"Undead cow (4)",
"value":1691
},
{
"label":"Plague cow",
"value":1998
},
{
"label":"Plague cow",
"value":1999
},
{
"label":"Unicow (57)",
"value":5603
},
{
"label":"Zombie cow (1)",
"value":18597
},
{
"label":"Zombie cow (1)",
"value":20928
},
{
"label":"Super Cow (5)",
"value":21497
},
{
"label":"Dairy cow",
"value":22418
},
{
"label":"Armoured cow thing (62)",
"value":5986
},
{
"label":"Armoured cow thing (62)",
"value":6048
}
]
And when i try to access the data point inside the array it returns null, code:
Stream stream = client.OpenRead("http://services.runescape.com/m=itemdb_rs/bestiary/beastSearch.json?term=" + Input);
StreamReader reader = new StreamReader(stream);
jObject = JObject.Parse(reader.ReadToEnd());
stream.Close();
//put items into list view
// i is the number where the json object is in the array
var lvi = new ListViewItem(new string[] { (string)jObject[i]["label"], (string)jObject[i]["value"] });
I do not want to use classes
Found error in your code. Instead:
JObject.Parse(reader.ReadToEnd());
Write (JObject -> JArray):
string text = reader.ReadToEnd();
var jObject = JArray.Parse(text);
Also when write operation in 2 lines, you will see where the error: in reading from the stream or in serialization.
Not wanting to use classes is weird but not impossible.
var json = reader.ReadToEnd();
var objects = JsonConvert.DeserializeObject<dynamic[]>(json);
var lvi = new ListViewItem(new string[] { (string)objects[i].label, (string)objects[i].value });
Try my answer to this question :
public IEnumerable<MeItem> DeserializeListFromJson(string jsonArray)
{
return JsonConverter.DeserializeObject<List<JObject>>(jsonArray).Select( obj => DeserializeFromJson(obj) );
}
public MeItem DeserializeFromJson(string jsonString)
{
return JsonConvert.DeserializeObject<MeItem>(jsonString);
}
You can find necessary detailed informations in my answer for this question and this one
Edit:
If you do not want to use classes then you can just modify DeserializeFromJson() method into something like this :
public KeyValuePair<string, string> DeserializeFromJson(JObject obj)
{
return new KeyValuePair<string, string>(obj.SelectToken("label").Value<string>(), obj.SelectToken("value").Value<string>());
}
Which will require to modify DeserializeListFromJson() method into :
public IEnumerable<KeyValuePair<string,string>> DeserializeListFromJson(string jsonArray)
{
return JsonConverter.DeserializeObject<List<JObject>>(jsonArray).Select( obj => DeserializeFromJson(obj) );
}
Usage with your case :
Stream stream = client.OpenRead("http://services.runescape.com/m=itemdb_rs/bestiary/beastSearch.json?term=" + Input);
ListViewItem item = null;
using (StreamReader reader = new StreamReader(stream))
{
KeyValuePair<string, string> selected = DeserializeListFromJson(reader.ReadToEnd()).ElementAt(i);
item = new ListViewItem(new string[] { selected.Key, selected.Value });
}

Microsoft.Graph.AttendeeType not enumerated correctly

I am attempting to create an event through the Microsoft Graph API inviting users as attendees. The code to set the attendees is as follows:
var attendees = new List<Microsoft.Graph.Attendee>();
foreach (var e in emailAddresses)
{
var userProfile = await AzureGraph.GetOtherUserProfile(e);
if (e != currentUserEmail.First())
{
Microsoft.Graph.EmailAddress email = new Microsoft.Graph.EmailAddress();
email.Name = userProfile.DisplayName;
email.Address = e;
attendees.Add(new Microsoft.Graph.Attendee()
{
EmailAddress = email,
Type = Microsoft.Graph.AttendeeType.Optional
});
}
}
await AzureGraph.AddEvent(new Microsoft.Graph.Event
{
Subject = string.Format("Follow Up: {0}", Id),
Body = new Microsoft.Graph.ItemBody
{
Content = "content"
},
Start = start,
End = start.AddMinutes(30),
Attendees = attendees
});
However, when making the request, i get a Bad Request response. The reason for this is that the 'Type' of an attendee is a n enum of Microsoft.Graph.AttendeeType and this is not being enumerated properly. Therefore it is attempting to send through the numeric value '1' instead of the string value "Optional" causing it to fail.
I was able to confirm this using fiddler, if i manually change the numeric value to the string value then it works no problem.
Has anybody come across this or have any ideas how i can solve this?
Many Thanks for your help in advance :)
I have now managed to solve this. The solution is a bit of a hack but it works. My original call code was as follows:
public static async Task AddEvent(Event e)
{
using (var client = new HttpClient())
{
using (var req = new HttpRequestMessage(HttpMethod.Post, _calendarUrl))
{
var token = await GetToken();
req.Headers.Add("Authorization", string.Format("Bearer {0}", token));
req.Headers.TryAddWithoutValidation("Content-Type", "application/json");
var requestContent = JsonConvert.SerializeObject(new
{
Subject = e.Subject,
Body = new
{
ContentType = "HTML",
Content = e.Body.Content
},
Start = new
{
DateTime = e.Start,
TimeZone = "UTC"
},
End = new
{
DateTime = e.End,
TimeZone = "UTC"
}
});
req.Content = new StringContent(requestContent, Encoding.UTF8, "application/json");
using (var response = await client.SendAsync(req))
{
if (response.IsSuccessStatusCode)
{
return;
}
else
{
throw new HttpRequestException("Event could not be added to calendar");
}
}
}
}
}
I have now changed this to:
public static async Task AddEvent(Event e)
{
using (var client = new HttpClient())
{
using (var req = new HttpRequestMessage(HttpMethod.Post, _calendarUrl))
{
var token = await GetToken();
req.Headers.Add("Authorization", string.Format("Bearer {0}", token));
req.Headers.TryAddWithoutValidation("Content-Type", "application/json");
IList<Attendee> attendees = new List<Attendee>();
foreach(var a in e.Attendees)
{
attendees.Add(new Attendee()
{
EmailAddress = a.EmailAddress,
Type = Enum.GetName(typeof(AttendeeType), AttendeeType.Optional)
});
}
var requestContent = JsonConvert.SerializeObject(new
{
Subject = e.Subject,
Body = new
{
ContentType = "HTML",
Content = e.Body.Content
},
Start = new
{
DateTime = e.Start,
TimeZone = "UTC"
},
End = new
{
DateTime = e.End,
TimeZone = "UTC"
},
Attendees = attendees
});
req.Content = new StringContent(requestContent, Encoding.UTF8, "application/json");
using (var response = await client.SendAsync(req))
{
if (response.IsSuccessStatusCode)
{
return;
}
else
{
throw new HttpRequestException("Event could not be added to calendar");
}
}
}
}
}
As well as adding the following local class:
private class Attendee
{
public EmailAddress EmailAddress { get; set; }
public string Type { get; set; }
}
Essentially, the Graph Attendee expected:
1. an EmailAddress object containing Name (string) and Email (string).
2. a Type object of type AttendeeType which was the enum not being passed correctly.
Therefore, i created my own version of the class Attendee to contain the same EmailAddress object and Type of type string as the API expects it to be.
I then had to change the enum type to the name of the enum rather than the int value. This was done as follows:
attendees.Add(new Attendee()
{
EmailAddress = a.EmailAddress,
Type = Enum.GetName(typeof(AttendeeType), AttendeeType.Optional)
});
This gave me the value "Optional" rather than 1 which made it acceptable to the API.
I hope this helps someone in the future.
It looks like a major oversight on microsoft's part to use an enum in the code and the expect a string rather than an integer in the API and i think this needs addressing.

Categories

Resources