static void Main(string[] args) { Authentication authentication = new Authentication("test", "12345"); NotifierMobileService.GetAll(authentication, null, null, null); NotifierMobileService.Get(5, authentication); Notification addModel = new Notification(); addModel.Title = "title"; addModel.Message = "message"; addModel.Type = (int) NotificationType.INFO; addModel.CreateDate = DateTime.Now; NotifierMobileService.Add(addModel, authentication); Notification updateModel = new Notification(); updateModel.Message = "update"; updateModel.Title = "update"; updateModel.Type = 2; NotifierMobileService.Update(5, updateModel, authentication); NotifierMobileService.MarkAsRead(5, authentication); NotifierMobileService.Delete(90, authentication); }
public static WebRequest createRequest(RequestType type, Authentication authentication, int? id, Notification model) { HttpRequestAttr requestAttrs = (HttpRequestAttr) type.GetAttr(); WebRequest request; string url = requestAttrs.URL; if (id.HasValue) { url += ("/" + id); } url += "?username="******"&secretKey=" + authentication.SecretKey; request = WebRequest.Create(url); request.Method = requestAttrs.Method; if (model != null) { request.ContentType = "application/json"; using (var streamWriter = new StreamWriter(request.GetRequestStream())) { string json = model.GenerateJsonString(); streamWriter.Write(json); } } else { request.ContentLength = 0; } request.Timeout = CONNECTION_TIMEOUT; return request; }
public static WebRequest createGetRequest(RequestType requestType, Authentication authentication, int? id, int? type, bool? unread, int? fromId) { HttpRequestAttr requestAttrs = (HttpRequestAttr)requestType.GetAttr(); WebRequest request; string url = requestAttrs.URL; if (id.HasValue) { url += ("/" + id); } url += "?username="******"&secretKey=" + authentication.SecretKey; if (type.HasValue) { url += "&type=" + type.Value; } if (unread.HasValue) { url += "&unread=" + unread.Value; } if (fromId.HasValue) { url += "&fromId=" + fromId.Value; } request = WebRequest.Create(url); request.Method = requestAttrs.Method; request.Timeout = CONNECTION_TIMEOUT; return request; }
/// <summary> /// Delete a notification by id /// </summary> /// <param name="id"></param> /// <param name="model"></param> public static void Delete(int id, Authentication authentication) { try { WebRequest request = HttpHelper.createRequest(RequestType.DELETE, authentication, id, null); HttpHelper.getResponse(request); } catch (WebException ex) { throw createNotificationException(ex); } catch (Exception ex) { throw createNotificationException(ex); } }
/// <summary> /// Add a new notification /// </summary> /// <param name="model"></param> public static void Add(Notification model, Authentication authentication) { try { WebRequest request = HttpHelper.createRequest(RequestType.ADD, authentication, null, model); HttpHelper.getResponse(request); } catch (WebException ex) { throw createNotificationException(ex); } catch (Exception ex) { throw createNotificationException(ex); } }
/// <summary> /// Get notification by id /// </summary> /// <param name="id"></param> /// <param name="model"></param> /// <returns></returns> public static Notification Get(int id, Authentication authentication) { List<Notification> notifications = new List<Notification>(); try { WebRequest request = HttpHelper.createGetRequest(RequestType.GET, authentication, id, null, null, null); notifications = HttpHelper.getResponse(request); } catch (WebException ex) { throw createNotificationException(ex); } catch (Exception ex) { throw createNotificationException(ex); } return notifications[0]; }
/// <summary> /// Get all notifications /// </summary> /// <param name="authentication"></param> /// <returns></returns> public static List<Notification> GetAll(Authentication authentication) { return GetAll(authentication, null, null, null); }
/// <summary> /// Update a notification by id /// </summary> /// <param name="id"></param> /// <param name="model"></param> public static void Update(int id, Notification model, Authentication authentication) { try { WebRequest request = HttpHelper.createRequest(RequestType.UPDATE, authentication, id, model); HttpHelper.getResponse(request); } catch (WebException ex) { throw createNotificationException(ex); } catch (Exception ex) { throw createNotificationException(ex); } }
/// <summary> /// Mark a notification as read /// </summary> /// <param name="id"></param> /// <param name="model"></param> public static void MarkAsRead(int id, Authentication authentication) { try { WebRequest request = HttpHelper.createRequest(RequestType.MARK_AS_READ, authentication, id, null); HttpHelper.getResponse(request); } catch (WebException ex) { throw createNotificationException(ex); } catch (Exception ex) { throw createNotificationException(ex); } }
/// <summary> /// Get notifications by type and unread and fromId /// </summary> /// <param name="authentication"></param> /// <param name="type"></param> /// <param name="unread"></param> /// <param name="fromId"></param> /// <returns></returns> public static List<Notification> GetAll(Authentication authentication, int? type, bool? unread, int? fromId) { List<Notification> notifications = new List<Notification>(); try { WebRequest request = HttpHelper.createGetRequest(RequestType.GET_ALL, authentication, null, type, unread, fromId); notifications = HttpHelper.getResponse(request); } catch (WebException ex) { throw createNotificationException(ex); } catch (Exception ex) { throw createNotificationException(ex); } return notifications; }
/// <summary> /// Get notifications by type and unread /// </summary> /// <param name="authentication"></param> /// <param name="type"></param> /// <param name="unread"></param> /// <returns></returns> public static List<Notification> GetAll(Authentication authentication, int type, bool unread) { return GetAll(authentication, type, unread, null); }