public virtual T Create() { BillomatResourceAttribute resource = GetResource(); if (resource.Flags.HasFlag(BillomatResourceFlags.NoCreate)) { throw new BillomatException(string.Format("Creating new objects is not allowed for {0}!", GetType().Name), new NotSupportedException()); } var xml = CreateXml(resource.XmlSingleName, this); var req = new BillomatRequest { Verb = "POST", Resource = resource.ResourceName, Body = xml.ToString(SaveOptions.DisableFormatting) }; T result = CreateFromXml(req.GetXmlResponse()); // save the returned values (especially id) ApplyFrom(result); return(result); }
public virtual bool Delete() { BillomatResourceAttribute resource = GetResource(); if (resource.Flags.HasFlag(BillomatResourceFlags.NoDelete)) { throw new BillomatException(string.Format("Deleting an object is not allowed for {0}!", GetType().Name), new NotSupportedException()); } var req = new BillomatRequest { Verb = "DELETE", Resource = resource.ResourceName, Id = Id }; try { req.GetXmlResponse(); return(true); } catch (BillomatException) { return(false); } }
/// <summary> /// Returns the user that is currently using the API /// </summary> /// <returns></returns> public static BillomatUser FindMyself() { var req = new BillomatRequest { Resource = "users", Method = "myself" }; return(CreateFromXml(req.GetXmlResponse())); }
public static T Find(int id) { var req = new BillomatRequest { Verb = "GET", Resource = GetResource().ResourceName, Id = id }; return(CreateFromXml(req.GetXmlResponse())); }
public void Cancel() { var req = new BillomatRequest { Verb = "PUT", Resource = GetResource().ResourceName, Id = Id, Method = "cancel" }; req.GetResponse(); }
/// <summary> /// Returns image date containing a thumbnail of the first page of the template /// </summary> /// <param name="type">Type of the image to be returned</param> /// <returns>Byte array containing image data of the thumbnail</returns> public byte[] Thumb(BillomatTemplateThumbType type = BillomatTemplateThumbType.Png) { var req = new BillomatRequest { Resource = "templates", Id = Id, Method = "thumb" }; req.Params.Add("type", type.ToString().ToLower()); return(req.GetResponse()); }
public Stream GetPdfDocument() { var req = new BillomatRequest { Resource = GetResource().ResourceName, Id = Id, Method = "pdf" }; req.Params.Add("format", "pdf"); return(new MemoryStream(req.GetResponse())); }
public void SendMail(string toEMail, string fromEMail = null, string ccEMail = null, string bccEMail = null, string subject = null, string body = null, string filename = null) { string[] tos = (toEMail ?? "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); string[] ccs = (ccEMail ?? "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); string[] bccs = (bccEMail ?? "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (tos.Length == 0) { throw new BillomatException("No recipient specified!", new ArgumentException("toEMail must not be null or empty", "toEMail")); } var req = new BillomatRequest { Verb = "POST", Resource = GetResource().ResourceName, Id = Id, Method = "email" }; var elRecipients = new XElement("recipients"); elRecipients.Add(tos.Select(to => new XElement("to", to)).ToArray()); elRecipients.Add(ccs.Select(cc => new XElement("cc", cc)).ToArray()); elRecipients.Add(bccs.Select(bcc => new XElement("bcc", bcc)).ToArray()); var xml = new XElement("email", elRecipients); if (!string.IsNullOrEmpty(fromEMail)) { xml.Add(new XElement("from", fromEMail)); } if (!string.IsNullOrEmpty(subject)) { xml.Add(new XElement("subject", subject)); } if (!string.IsNullOrEmpty(body)) { xml.Add(new XElement("body", body)); } if (!string.IsNullOrEmpty(filename)) { xml.Add(new XElement("filename", filename)); } req.Body = xml.ToString(SaveOptions.DisableFormatting); req.GetResponse(); }
/// <summary> /// Retrieves the Billomat settings for this account /// </summary> /// <returns></returns> public static BillomatSettings Load() { var req = new BillomatRequest { Verb = "GET", Resource = GetResource().ResourceName }; BillomatSettings result = CreateFromXml(req.GetXmlResponse()); result.Created = DateTime.Now; return(result); }
/// <summary> /// Uploads a signed PDF file for this invoice /// </summary> /// <param name="file"></param> public void UploadSignature(Stream file) { string base64File = BillomatHelper.Base64File(file); var req = new BillomatRequest { Verb = "PUT", Resource = GetResource().ResourceName, Id = Id, Method = "upload-signature" }; var xml = new XElement("signature", new XElement("base64file", base64File)); req.Body = xml.ToString(SaveOptions.DisableFormatting); req.GetResponse(); }
/// <summary> /// Finds all Billomat objects with the specified parameters. Sends multiple requests if /// necessary (due to page size). /// </summary> /// <param name="parameters"></param> /// <returns></returns> protected static List <T> FindAll(NameValueCollection parameters = null) { BillomatResourceAttribute resource = GetResource(); var req = new BillomatRequest { Verb = "GET", Resource = resource.ResourceName }; if (parameters != null) { req.Params.Add(parameters); } req.Params["per_page"] = Billomat.PageSize.ToString(CultureInfo.InvariantCulture); int page = 0; int total = 0; T[] result = null; do { page++; req.Params["page"] = page.ToString(CultureInfo.InvariantCulture); XElement xml = req.GetXmlResponse(); if (result == null) { total = (int)xml.Attribute("total"); result = new T[total]; } int i = 0; foreach (var obj in xml.XPathSelectElements(string.Format("/{0}/{1}", resource.XmlMultiName, resource.XmlSingleName))) { result[(page - 1) * Billomat.PageSize + i] = CreateFromXml(obj); i++; } } while (page * Billomat.PageSize < total); return(result.ToList()); }
/// <summary> /// Downloads the PDF document of this invoice /// </summary> /// <param name="signed">Specify true to retrieve a signed PDF</param> /// <returns></returns> public MemoryStream GetPdfDocument(bool signed) { var req = new BillomatRequest { Resource = GetResource().ResourceName, Id = Id, Method = "pdf" }; req.Params.Add("format", "pdf"); if (signed) { req.Params.Add("type", "signed"); } return(new MemoryStream(req.GetResponse())); }
public void Complete(int?templateId = null) { var req = new BillomatRequest { Verb = "PUT", Resource = GetResource().ResourceName, Id = Id, Method = "complete" }; var xml = new XElement("complete"); if (templateId.HasValue) { xml.Add(new XElement("template_id", templateId.Value.ToString(CultureInfo.InvariantCulture))); } req.Body = xml.ToString(SaveOptions.DisableFormatting); req.GetResponse(); }
public virtual void Update() { BillomatResourceAttribute resource = GetResource(); if (resource.Flags.HasFlag(BillomatResourceFlags.NoUpdate)) { throw new BillomatException(string.Format("Updating an object is not allowed for {0}!", GetType().Name), new NotSupportedException()); } var xml = CreateXml(resource.XmlSingleName, this); var req = new BillomatRequest { Verb = "PUT", Resource = resource.ResourceName, Id = Id, Body = xml.ToString(SaveOptions.DisableFormatting) }; req.GetXmlResponse(); }