/// <summary> /// This example uses the raw JSON string to create a POST /// request for sending one or more SMTP messages through Email-On-Demand. /// /// The JSON request generated by this sample code looks like this: /// ///{ /// "ServerId": "YOUR SERVER ID HERE", /// "ApiKey": "YOUR API KEY HERE", /// "Messages": [{ /// "Subject": "Email subject line for raw JSON example.", /// "TextBody": "The text portion of the message.", /// "HtmlBody": "<h1>The html portion of the message</h1><br/>Test", /// "To": [{ /// "EmailAddress": "*****@*****.**", /// "FriendlyName": "Customer Name" /// }], /// "From": { /// "EmailAddress": "*****@*****.**", /// "FriendlyName": "From Address" /// }, /// }] ///} /// </summary> public static void SimpleInjectionViaStringAsJson( int serverId, string yourApiKey, string apiUrl) { // The client object processes requests to the SocketLabs Injection API. var client = new RestClient(apiUrl); // Construct the string used to generate JSON for the POST request. string stringBody = "{" + "\"ServerId\":\"" + serverId + "\"," + "\"ApiKey\":\"" + yourApiKey + "\"," + "\"Messages\":[{" + "\"Subject\":\"Email subject line for raw JSON example.\"," + "\"TextBody\":\"The text portion of the message.\"," + "\"HtmlBody\":\"<h1>The html portion of the message</h1><br/>Test\"," + "\"To\":[{" + "\"EmailAddress\":\"[email protected]\"," + "\"FriendlyName\":\"Customer Name\"" + "}]," + "\"From\":{" + "\"EmailAddress\":\"[email protected]\"," + "\"FriendlyName\":\"From Address\"" + "}," + "}]" + "}"; try { // Generate a new POST request. var request = new RestRequest(Method.POST) { RequestFormat = DataFormat.Json }; // Store the request data in the request object. request.AddParameter("application/json; charset=utf-8", stringBody, ParameterType.RequestBody); // Make the POST request. var result = client.ExecuteAsPost(request, "POST"); // Store the response result in our custom class. using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(result.Content))) { var serializer = new DataContractJsonSerializer(typeof (PostResponse)); var resp = (PostResponse) serializer.ReadObject(stream); // Display the results. if (resp.ErrorCode.Equals("Success")) { Console.WriteLine("Successful injection!"); } else { Console.WriteLine("Failed injection! Returned JSON is: "); Console.WriteLine(result.Content); } } } catch (Exception ex) { Console.WriteLine("Error, something bad happened: " + ex.Message); } }
public async Task DeleteContainerById(string hostId, string id) { Debug.Assert(id != null); var container = await GetContainerById(hostId, id); var host = await HostActor.GetHostById(container.HostId); // Remove container from Docker var restClient = new RestClient(host.HostUrl); var request = new RestRequest(string.Format("/containers/{0}", id)); var response = restClient.ExecuteAsPost(request, "DELETE"); // TODO - check response for success }
private void QueueLog(LogMessage message) { _loggingEvents.Enqueue(message); var client = new RestClient(postUrl); var request = new RestRequest(); request.Resource = "api/Logger"; request.AddJsonBody(message); client.ExecuteAsPost(request, "POST"); var temp = 1; }
public static void ToAHDC(DTO.LabOrder order, string username, string password) { // Written by Scott Ross // Cornell University 2013 // Using REST Sharp, attach to our webserivces, send message // Serialization and Deserialization using REST Sharp //Instainaite the Rest Client using base url var client = new RestClient("http://localhost:54490/"); //Create the rest request uing the oepration (in this case, we are posting a lab order var request = new RestRequest("labOrder/?format=xml", Method.POST); //Serialize Order RestSharp.Serializers.DotNetXmlSerializer serialize = new RestSharp.Serializers.DotNetXmlSerializer(); String msg = serialize.Serialize(order).ToString(); //Add msg to the parametertype of request body. request.AddParameter("text/xml", msg, ParameterType.RequestBody); request.AddHeader("username", username); request.AddHeader("password", password); //Execute & access "content" //Access the message in response //The REsponse XML looks like: var res = client.ExecuteAsPost(request, "Post"); res.ContentType = "application/xml"; /* RestSharp.Deserializers.XmlDeserializer deserial = new RestSharp.Deserializers.XmlDeserializer(); var response = deserial.Deserialize<Response_DTO.LabOrderResponse>(res); if (response.Ack.Status.Equals("AA")) { Console.WriteLine("Successfull Sending of Order!"); } else { Console.WriteLine(response.Ack.ErrorDetails); } * */ }
/// <summary> /// This example uses the SocketLabs SDK PostBody object to create a POST /// request for sending one or more SMTP messages through Email-On-Demand. /// The SocketLabs SDK library is required, and for this example we also use /// the RestSharp library to ask as our serializer and client. /// /// The JSON request generated by this sample code looks like this: /// ///{ /// "ServerId": "YOUR SERVER ID HERE", /// "ApiKey": "YOUR API KEY HERE", /// "Messages": [{ /// "MailingId": null, /// "MessageId": null, /// "MergeData": null, /// "Subject": "Email subject line for SDK example.", /// "TextBody": "The text portion of the message.", /// "HtmlBody": "<h1>The HTML portion of the message</h1><br/><p>A paragraph.</p>", /// "CustomHeaders": null, /// "To": [{ /// "EmailAddress": "*****@*****.**", /// "FriendlyName": null /// }], /// "Cc": null, /// "Bcc": null, /// "From": { /// "EmailAddress": "*****@*****.**", /// "FriendlyName": "From Address" /// }, /// "ReplyTo": null, /// "Charset": null, /// "Attachments": null /// }] ///} /// </summary> public static void SimpleInjectionViaSdkObjectAsJson( int serverId, string yourApiKey, string apiUrl) { // The client object processes requests to the SocketLabs Injection API. var client = new RestClient(apiUrl); // Construct the object used to generate JSON for the POST request. var postBody = new PostBody { ServerId = serverId, ApiKey = yourApiKey, Messages = new[] { new EmailMessage { Subject = "Email subject line for SDK example.", To = new[] { new Address { EmailAddress = "*****@*****.**", FriendlyName = null } }, From = new Address { EmailAddress = "*****@*****.**", FriendlyName = "From Address" }, TextBody = "The text portion of the message.", HtmlBody = "<h1>The HTML portion of the message</h1><br/><p>A paragraph.</p>", } } }; try { // Generate a new POST request. var request = new RestRequest(Method.POST) { RequestFormat = DataFormat.Json }; // Store the request data in the request object. request.AddBody(postBody); // Make the POST request. var result = client.ExecuteAsPost<PostResponse>(request, "POST"); // Store the response result in our custom class. using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(result.Content))) { var serializer = new DataContractJsonSerializer(typeof (PostResponse)); var resp = (PostResponse) serializer.ReadObject(stream); // Display the results. if (resp.ErrorCode.Equals("Success")) { Console.WriteLine("Successful injection!"); } else { Console.WriteLine("Failed injection! Returned JSON is: "); Console.WriteLine(result.Content); } } } catch (Exception ex) { Console.WriteLine("Error, something bad happened: " + ex.Message); } }
/// <summary> /// This example uses generic JsonObject containers to create a POST /// request for sending one or more SMTP messages through Email-On-Demand. /// /// The JSON request generated by this sample code looks like this: /// ///{ /// "ServerId": "YOUR SERVER ID HERE", /// "ApiKey": "YOUR API KEY HERE", /// "Messages": [{ /// "Subject": "API Template Example", /// "ApiTemplate": "1", /// "To": [{ /// "EmailAddress": "%%DeliveryAddress%%", /// "FriendlyName": "%%FirstName" /// }], /// "From": { /// "EmailAddress": "*****@*****.**", /// "FriendlyName": "The ABC Company" /// }, /// "MergeData": { /// "PerMessage": [ /// [ /// { /// "Field":"DeliveryAddress", /// "Value":"*****@*****.**" /// }, /// { /// "Field": "FirstName", /// "Value": "John" /// } /// ] /// ] /// } /// }] ///} /// </summary> public static void SimpleInjectionApiTemplate( int serverId, string yourApiKey, string apiUrl) { // The client object processes requests to the SocketLabs Injection API. var client = new RestClient(apiUrl); // Construct the objects used to generate JSON for the POST request. var recipient1 = new JsonObject(); recipient1.Add("EmailAddress", "%%DeliveryAddress%%"); recipient1.Add("FriendlyName", "%%FirstName%%"); var toList = new JsonArray(); toList.Add(recipient1); var fromField = new JsonObject(); fromField.Add("EmailAddress", "*****@*****.**"); fromField.Add("FriendlyName", "The ABC Company"); //The DeliveryAddress field is required, all other merge fields are optional. //If your template contains merge fields, you must specify them here. var mergeDeliveryAddress = new JsonObject(); mergeDeliveryAddress.Add("Field", "DeliveryAddress"); mergeDeliveryAddress.Add("Value", "*****@*****.**"); var mergeFirstName = new JsonObject(); mergeFirstName.Add("Field", "FirstName"); mergeFirstName.Add("Value", "John"); var mergeRecipient1 = new JsonArray(); mergeRecipient1.Add(mergeDeliveryAddress); mergeRecipient1.Add(mergeFirstName); var perMessage = new JsonArray(); perMessage.Add(mergeRecipient1); var mergeData = new JsonObject(); mergeData.Add("PerMessage", perMessage); var message1 = new JsonObject(); message1.Add("Subject", "API Template Example"); message1.Add("ApiTemplate", "1"); //Template ID found in the SocketLabs On-Demand Control Panel message1.Add("To", toList); message1.Add("From", fromField); message1.Add("MergeData", mergeData); var messageArray = new JsonObject[1]; messageArray[0] = message1; var body = new JsonObject(); body.Add("ServerId", serverId); body.Add("ApiKey", yourApiKey); body.Add("Messages", messageArray); try { // Generate a new POST request. var request = new RestRequest(Method.POST) { RequestFormat = DataFormat.Json }; // Store the request data in the request object. request.AddBody(body); // Make the POST request. var result = client.ExecuteAsPost(request, "POST"); // Store the response result in our custom class. using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(result.Content))) { var serializer = new DataContractJsonSerializer(typeof(PostResponse)); var resp = (PostResponse)serializer.ReadObject(stream); // Display the results. if (resp.ErrorCode.Equals("Success")) { Console.WriteLine("Successful injection!"); } else { Console.WriteLine("Failed injection! Returned JSON is: "); Console.WriteLine(result.Content); } } } catch (Exception ex) { Console.WriteLine("Error, something bad happened: " + ex.Message); } }
/// <summary> /// This example uses generic JsonObject containers to create a POST /// request for sending one or more SMTP messages through Email-On-Demand. /// /// The JSON request generated by this sample code looks like this: /// ///{ /// "ServerId": "YOUR SERVER ID HERE", /// "ApiKey": "YOUR API KEY HERE", /// "Messages": [{ /// "Subject": "Email subject line for generic object example.", /// "TextBody": "The text portion of the message.", /// "HtmlBody": "<h1>The HTML portion of the message</h1><br/><p>A paragraph.</p>", /// "To": [{ /// "EmailAddress": "*****@*****.**", /// "FriendlyName": "Customer Name" /// }], /// "From": { /// "EmailAddress": "*****@*****.**", /// "FriendlyName": "The ABC Company" /// } /// }] ///} /// </summary> public static void SimpleInjectionViaRestSharpAsJson( int serverId, string yourApiKey, string apiUrl) { // The client object processes requests to the SocketLabs Injection API. var client = new RestClient(apiUrl); // Construct the objects used to generate JSON for the POST request. var recipient1 = new JsonObject(); recipient1.Add("EmailAddress", "*****@*****.**"); recipient1.Add("FriendlyName", "Customer Name"); var toList = new JsonObject[1]; toList[0] = recipient1; var fromField = new JsonObject(); fromField.Add("EmailAddress", "*****@*****.**"); fromField.Add("FriendlyName", "The ABC Company"); var message1 = new JsonObject(); message1.Add("Subject", "Email subject line for generic object example."); message1.Add("TextBody", "The text portion of the message."); message1.Add("HtmlBody", "<h1>The HTML portion of the message</h1><br/><p>A paragraph.</p>"); message1.Add("To", toList); message1.Add("From", fromField); var messageArray = new JsonObject[1]; messageArray[0] = message1; var body = new JsonObject(); body.Add("ServerId", serverId); body.Add("ApiKey", yourApiKey); body.Add("Messages", messageArray); try { // Generate a new POST request. var request = new RestRequest(Method.POST) { RequestFormat = DataFormat.Json }; // Store the request data in the request object. request.AddBody(body); // Make the POST request. var result = client.ExecuteAsPost(request, "POST"); // Store the response result in our custom class. using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(result.Content))) { var serializer = new DataContractJsonSerializer(typeof (PostResponse)); var resp = (PostResponse) serializer.ReadObject(stream); // Display the results. if (resp.ErrorCode.Equals("Success")) { Console.WriteLine("Successful injection!"); } else { Console.WriteLine("Failed injection! Returned JSON is: "); Console.WriteLine(result.Content); } } } catch (Exception ex) { Console.WriteLine("Error, something bad happened: " + ex.Message); } }
/// <summary> /// This example uses the SocketLabs SDK PostBody object to create a POST /// request for sending one or more SMTP messages through Email-On-Demand. /// The SocketLabs SDK library is required, and for this example we also use /// the RestSharp library to ask as our serializer and client. /// /// We use as many fields as possible in this example, in order to show usage /// for most of our options in a single example. /// /// The JSON request generated by this sample code looks like this: /// ///{ /// "ServerId": "YOUR SERVER ID HERE", /// "ApiKey": "YOUR API KEY HERE", /// "Messages": [{ /// "MailingId": "Human Resources 017", /// "MessageId": "0000-9999-0000-9999-0000", /// "MergeData": null, /// "Subject": "Welcome to The ABC Company!", /// "TextBody": "Welcome to The ABC Company, new employees! You are all part of our new department.", /// "HtmlBody": "<h1>Welcome to The ABC Company, new employee!</h1><br/><p>You are all part of our new department.</p>", /// "CustomHeaders": [{ /// "Name": "Internal-Email-Type", /// "Value": "HR/New Employee" /// }], /// "To": [{ /// "EmailAddress": "*****@*****.**", /// "FriendlyName": "Robert Smith" /// }, /// { /// "EmailAddress": "*****@*****.**", /// "FriendlyName": "Alice White" /// }, /// { /// "EmailAddress": "*****@*****.**", /// "FriendlyName": "Christine Johnson" /// }], /// "Cc": [{ /// "EmailAddress": "*****@*****.**", /// "FriendlyName": "HR Department" /// }], /// "Bcc": [{ /// "EmailAddress": "*****@*****.**", /// "FriendlyName": "Email Logging Account" /// }], /// "From": { /// "EmailAddress": "*****@*****.**", /// "FriendlyName": "From Address" /// }, /// "ReplyTo": { /// "EmailAddress": "*****@*****.**", /// "FriendlyName": "HR Department" /// }, /// "Charset": "us-ascii", /// "Attachments": [{ /// "Name": "CompanyContacts.txt", /// "ContentType": "text/plain", /// "Content": "VEhJUyBJUyBUSEUgQVRUQUNITUVOVA==", /// "CustomHeaders": null, /// "ContentId": null /// }] /// }, /// { /// "MailingId": "%%MailingId%%", /// "MessageId": "%%CUSTOM-SERIAL-NUMBER-ID%%", /// "MergeData": { /// "PerMessage": [[{ /// "Field": "DeliveryAddress", /// "Value": "*****@*****.**" /// }, /// { /// "Field": "RecipientName", /// "Value": "Robert Smith" /// }, /// { /// "Field": "SeniorEmployeeName", /// "Value": "Elizabeth Johnson" /// }, /// { /// "Field": "CUSTOM-SERIAL-NUMBER-ID", /// "Value": "0000-0000-0000-8888-0001" /// }], /// [{ /// "Field": "DeliveryAddress", /// "Value": "*****@*****.**" /// }, /// { /// "Field": "RecipientName", /// "Value": "Alice White" /// }, /// { /// "Field": "SeniorEmployeeName", /// "Value": "Elizabeth Johnson" /// }, /// { /// "Field": "CUSTOM-SERIAL-NUMBER-ID", /// "Value": "0000-0000-0000-8888-0002" /// }], /// [{ /// "Field": "DeliveryAddress", /// "Value": "*****@*****.**" /// }, /// { /// "Field": "RecipientName", /// "Value": "Christine Johnson" /// }, /// { /// "Field": "SeniorEmployeeName", /// "Value": "Gary Brown" /// }, /// { /// "Field": "CUSTOM-SERIAL-NUMBER-ID", /// "Value": "0000-0000-0000-8888-0003" /// }]], /// "Global": [{ /// "Field": "MailingId", /// "Value": "Human Resources 018" /// }] /// }, /// "Subject": "Employee mentorship program information.", /// "TextBody": "%%RecipientName%%, you have been paired with %%SeniorEmployeeName%% as part of our mentorship program!", /// "HtmlBody": "<h1>%%RecipientName%%, you have been paired with %%SeniorEmployeeName%% as part of our mentorship program!</h1>", /// "CustomHeaders": null, /// "To": [{ /// "EmailAddress": "%%DeliveryAddress%%", /// "FriendlyName": "%%RecipientName%%" /// }], /// "Cc": null, /// "Bcc": null, /// "From": { /// "EmailAddress": "*****@*****.**", /// "FriendlyName": "HR Department" /// }, /// "ReplyTo": null, /// "Charset": null, /// "Attachments": null /// }] ///} /// </summary> public static void CompleteInjectionViaSdkObjectAsJson( int serverId, string yourApiKey, string apiUrl) { // The client object processes requests to the SocketLabs Injection API. var client = new RestClient(apiUrl); // In this example, we will utilize the majority of options available via // the Injection API. More details are noted inline as we use them. // // Construct the object(s) used to generate JSON for the POST request. // // For this example, we will create two messages in a single injection. var postBody = new PostBody { ServerId = serverId, ApiKey = yourApiKey, Messages = new EmailMessage[2] }; // The first of two messages to be injected; this example showcases // most of the options available with the exception of the inline Merge feature. postBody.Messages[0] = new EmailMessage { Subject = "Welcome to The ABC Company!", To = new[] { new Address { EmailAddress = "*****@*****.**", FriendlyName = "Robert Smith" }, new Address { EmailAddress = "*****@*****.**", FriendlyName = "Alice White" }, new Address { EmailAddress = "*****@*****.**", FriendlyName = "Christine Johnson" } }, Cc = new[] { new Address { EmailAddress = "*****@*****.**", FriendlyName = "HR Department" } }, Bcc = new[] { new Address { EmailAddress = "*****@*****.**", FriendlyName = "Email Logging Account" } }, From = new Address { EmailAddress = "*****@*****.**", FriendlyName = "From Address" }, ReplyTo = new Address { EmailAddress = "*****@*****.**", FriendlyName = "HR Department" }, // Please see the API documentation for information on using the MailingId and MessageId // fields for tracking groups of messages. MailingId = "Human Resources 017", MessageId = "0000-9999-0000-9999-0000", Charset = "us-ascii", TextBody = "Welcome to The ABC Company, new employees! You are all part of our new department.", HtmlBody = "<h1>Welcome to The ABC Company, new employee!</h1><br/><p>You are all part of our new department.</p>", // Custom SMTP headers are defined in this way. CustomHeaders = new[] { new CustomHeader { Name = "Internal-Email-Type", Value = "HR/New Employee" } }, // Multiple attachments can be added to a message. Attachments = new[] { new Attachment { Name = "CompanyContacts.txt", ContentType = "text/plain", Content = Convert.ToBase64String(Encoding.ASCII.GetBytes("THIS IS THE ATTACHMENT")), } }, }; // The second of two messages to be injected; this example shows how // to use the inline Merge feature, which customizes a message for its // recipients. postBody.Messages[1] = new EmailMessage { Subject = "Employee mentorship program information.", To = new[] { // DeliveryAddress is required for all inline merges. Additionally, any other addresses // used in the To, Cc, or Bcc fields will be for header display only, and will not receive // the injected email. Only the DeliveryAddress members will receive the injected message. new Address { EmailAddress = "%%DeliveryAddress%%", FriendlyName = "%%RecipientName%%" } }, From = new Address { EmailAddress = "*****@*****.**", FriendlyName = "HR Department" }, MailingId = "%%MailingId%%", // Merge field variables can go anywhere else in the message. A basic use would be to define // a unique MessageId for each outgoing message. MessageId = "%%CUSTOM-SERIAL-NUMBER-ID%%", // We can even reuse the same variable multiple times, such as %%RecipientName%% being used both // to define the name of the recipient in the header, and for addressing the recipient in the // message body. TextBody = "%%RecipientName%%, you have been paired with %%SeniorEmployeeName%% as part of our mentorship program!", HtmlBody = "<h1>%%RecipientName%%, you have been paired with %%SeniorEmployeeName%% as part of our mentorship program!</h1>", }; // Values for each of the merge data fields need to be added. postBody.Messages[1].MergeData = new MergeData(); // Global variables apply to all recipients in the same way. postBody.Messages[1].MergeData.Global = new MergeRow[1]; postBody.Messages[1].MergeData.Global[0] = new MergeRow("MailingId", "Human Resources 018"); // Per-Message variables are unique to each recipient. postBody.Messages[1].MergeData.PerMessage = new MergeRow[3][]; // Recipient 1 of 3 postBody.Messages[1].MergeData.PerMessage[0] = new MergeRow[4]; postBody.Messages[1].MergeData.PerMessage[0][0] = new MergeRow("DeliveryAddress", "*****@*****.**"); postBody.Messages[1].MergeData.PerMessage[0][1] = new MergeRow("RecipientName", "Robert Smith"); postBody.Messages[1].MergeData.PerMessage[0][2] = new MergeRow("SeniorEmployeeName", "Elizabeth Johnson"); postBody.Messages[1].MergeData.PerMessage[0][3] = new MergeRow("CUSTOM-SERIAL-NUMBER-ID", "0000-0000-0000-8888-0001"); // Recipient 2 of 3 postBody.Messages[1].MergeData.PerMessage[1] = new MergeRow[4]; postBody.Messages[1].MergeData.PerMessage[1][0] = new MergeRow("DeliveryAddress", "*****@*****.**"); postBody.Messages[1].MergeData.PerMessage[1][1] = new MergeRow("RecipientName", "Alice White"); postBody.Messages[1].MergeData.PerMessage[1][2] = new MergeRow("SeniorEmployeeName", "Elizabeth Johnson"); postBody.Messages[1].MergeData.PerMessage[1][3] = new MergeRow("CUSTOM-SERIAL-NUMBER-ID", "0000-0000-0000-8888-0002"); // Recipient 3 of 3 postBody.Messages[1].MergeData.PerMessage[2] = new MergeRow[4]; postBody.Messages[1].MergeData.PerMessage[2][0] = new MergeRow("DeliveryAddress", "*****@*****.**"); postBody.Messages[1].MergeData.PerMessage[2][1] = new MergeRow("RecipientName", "Christine Johnson"); postBody.Messages[1].MergeData.PerMessage[2][2] = new MergeRow("SeniorEmployeeName", "Gary Brown"); postBody.Messages[1].MergeData.PerMessage[2][3] = new MergeRow("CUSTOM-SERIAL-NUMBER-ID", "0000-0000-0000-8888-0003"); try { // Generate a new POST request. var request = new RestRequest(Method.POST) { RequestFormat = DataFormat.Json }; // Store the request data in the request object. request.AddBody(postBody); // Make the POST request. var result = client.ExecuteAsPost(request, "POST"); // Store the response result in our custom class. using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(result.Content))) { var serializer = new DataContractJsonSerializer(typeof (PostResponse)); var resp = (PostResponse) serializer.ReadObject(stream); // Display the results. if (resp.ErrorCode.Equals("Success")) { Console.WriteLine("Successful injection!"); } else { Console.WriteLine("Failed injection! Returned JSON is: "); Console.WriteLine(result.Content); } } } catch (Exception ex) { Console.WriteLine("Error, something bad happened: " + ex.Message); } }