PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Passbook.Sample.Web/Controllers/PassRegistrationController.cs

https://github.com/Comezon/dotnet-passbook
C# | 124 lines | 90 code | 22 blank | 12 comment | 1 complexity | 08765dec99f8ae85396c188f8256d123 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Threading.Tasks;
  8. using System.Web;
  9. using System.Web.Http;
  10. using Newtonsoft.Json;
  11. using Newtonsoft.Json.Linq;
  12. using Passbook.Sample.Web.Services;
  13. using Passbook.Generator;
  14. using System.Text;
  15. using System.IO;
  16. using Passbook.Generator.Fields;
  17. using Passbook.Sample.Web.SampleRequests;
  18. namespace Passbook.Sample.Web.Controllers
  19. {
  20. public class PassRegistrationController : ApiController
  21. {
  22. //https://webServiceURL/version/devices/deviceLibraryIdentifier/registrations/passTypeIdentifier/serialNumber
  23. public async Task<HttpResponseMessage> Post(string version, string deviceLibraryIdentifier, string passTypeIdentifier, string serialNumber, HttpRequestMessage message)
  24. {
  25. string authorizationToken = message.Headers.Authorization.Parameter;
  26. string jsonBody = await message.Content.ReadAsStringAsync();
  27. JObject json = JObject.Parse(jsonBody);
  28. string pushToken = json["pushToken"].Value<string>();
  29. PassRegistrationResult result = PassRegistrationResult.SuccessfullyRegistered;
  30. //this.handler.RegisterPass(version, deviceLibraryIdentifier, passTypeIdentifier, serialNumber, pushToken, authorizationToken, out result);
  31. HttpStatusCode resultCode = HttpStatusCode.InternalServerError;
  32. switch (result)
  33. {
  34. case PassRegistrationResult.SuccessfullyRegistered:
  35. resultCode = HttpStatusCode.Created;
  36. break;
  37. case PassRegistrationResult.UnAuthorized:
  38. resultCode = HttpStatusCode.Unauthorized;
  39. break;
  40. case PassRegistrationResult.AlreadyRegistered:
  41. resultCode = HttpStatusCode.OK;
  42. break;
  43. }
  44. return new HttpResponseMessage(resultCode);
  45. }
  46. //https://webServiceURL/version/devices/deviceLibraryIdentifier/registrations/passTypeIdentifier?passesUpdatedSince=tag
  47. public HttpResponseMessage Get(string version, string deviceLibraryIdentifier, string passTypeIdentifier, HttpRequestMessage request)
  48. {
  49. //List<string> updatedSerialNumbers = new List<string>();
  50. //updatedSerialNumbers.Add("121212111");
  51. //Dictionary<string, string> outputDictionary = new Dictionary<string, string>();
  52. //outputDictionary.Add("lastUpdated", "21/07/2012");
  53. //outputDictionary.Add("serialNumbers", JsonConvert.SerializeObject(updatedSerialNumbers));
  54. var response = new HttpResponseMessage(HttpStatusCode.OK);
  55. //string json = JsonConvert.SerializeObject(outputDictionary);
  56. StringBuilder sb = new StringBuilder();
  57. StringWriter sw = new StringWriter(sb);
  58. using (JsonWriter writer = new JsonTextWriter(sw))
  59. {
  60. writer.Formatting = Formatting.Indented;
  61. writer.WriteStartObject();
  62. writer.WritePropertyName("lastUpdated");
  63. writer.WriteValue("21/07/2012");
  64. writer.WritePropertyName("serialNumbers");
  65. writer.WriteStartArray();
  66. writer.WriteValue("121212111");
  67. writer.WriteEndArray();
  68. writer.WriteEndObject();
  69. }
  70. response.Content = new StringContent(sb.ToString(), Encoding.UTF8, "application/json");
  71. return response;
  72. }
  73. //https://webServiceURL/version/passes/passTypeIdentifier/serialNumber
  74. public HttpResponseMessage GetPass(string version, string passTypeIdentifier, string serialNumber)
  75. {
  76. EventPassGeneratorRequest request = new EventPassGeneratorRequest();
  77. request.Identifier = "pass.tomsamcguinness.events";
  78. request.CertThumbprint = ConfigurationManager.AppSettings["PassBookCertificateThumbprint"];
  79. request.CertLocation = System.Security.Cryptography.X509Certificates.StoreLocation.CurrentUser;
  80. request.SerialNumber = "121212111";
  81. request.Description = "My first pass";
  82. request.OrganizationName = "Tomas McGuinness";
  83. request.TeamIdentifier = "Team America";
  84. request.LogoText = "My Pass";
  85. request.BackgroundColor = "#000000";
  86. request.ForegroundColor = "#FFFFFF";
  87. // Specific information
  88. //
  89. request.EventName = "Jeff Wayne's War of the Worlds";
  90. request.SeatingSection = 10;
  91. request.DoorsOpen = new DateTime(2012, 11, 04, 11, 30, 00); // move the date!
  92. request.AddBarCode("01927847623423234234", BarcodeType.PKBarcodeFormatPDF417, "UTF-8", "01927847623423234234");
  93. request.AuthenticationToken = "vxwxd7J8AlNNFPS8k0a0FfUFtq0ewzFdc";
  94. request.WebServiceUrl = "http://192.168.1.3:81/api/";
  95. PassGenerator generator = new PassGenerator();
  96. byte[] generatedPass = generator.Generate(request);
  97. var response = new HttpResponseMessage(HttpStatusCode.OK);
  98. response.Content = new ObjectContent<Byte[]>(generatedPass, new BinaryFormatter());
  99. return response;
  100. }
  101. }
  102. }