Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions BufferAPI/BufferService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using AncoraMVVM.Rest;
using Newtonsoft.Json;
using System;

namespace BufferAPI
{
Expand Down Expand Up @@ -101,6 +102,75 @@ public async Task<HttpResponse<BufferUpdateCreation>> PostUpdate(string text,
{
return await PostUpdate(text, profiles.Select(p => p.Id));
}

/// <summary>
/// Posts an update to the given social media profiles.
/// </summary>
/// <param name="update">BufferUpdateCreate object describing the update to create.</param>
/// <param name="profileIds">List with all the social media profile ids.</param>
/// <returns>HttpResponse with a BufferUpdateCreating object describing the result of the operation.</returns>
public async Task<HttpResponse<BufferUpdateCreation>> PostUpdate(BufferUpdateCreate update, IEnumerable<string> profileIds)
{
ParameterCollection param = new ParameterCollection();

foreach (var id in profileIds)
param.Add("profile_ids[]", id);

param.Add("text", update.Text);
param.Add("shorten", update.Shorten.ToString().ToLower());
param.Add("now", update.Now.ToString().ToLower());
param.Add("top", update.Top.ToString().ToLower());
param.Add("attachment", update.Attachment.ToString().ToLower());


// schedule
if (update.ScheduledAt != null && update.ScheduledAt != DateTime.MinValue )
param.Add("scheduled_at", update.ScheduledAtSeconds);

// media
if (update.Media.Link != null && update.Media.Link.Trim() != "")
param.Add("media[link]", update.Media.Link);

if (update.Media.Description != null && update.Media.Description.Trim() != "")
param.Add("media[description]", update.Media.Description);

if (update.Media.Title != null && update.Media.Title.Trim() != "")
param.Add("media[title]", update.Media.Title);

if (update.Media.Picture != null && update.Media.Picture.Trim() != "")
param.Add("media[picture]", update.Media.Picture);

if (update.Media.Photo != null && update.Media.Photo.Trim() != "")
param.Add("media[photo]", update.Media.Photo);

if (update.Media.Thumbnail != null && update.Media.Thumbnail.Trim() != "")
param.Add("media[thumbnail]", update.Media.Thumbnail);


// retweet
if (update.Retweet.TweetId != null && update.Retweet.TweetId.Trim() != "")
param.Add("retweet[tweet_id]", update.Retweet.TweetId);

if (update.Retweet.Comment != null && update.Retweet.Comment.Trim() != "")
param.Add("retweet[comment]", update.Retweet.Comment);


var req = CreateRequest("updates/create.json", HttpMethod.Post, param);

return await Execute<BufferUpdateCreation>(req);
}

/// <summary>
/// Posts an update to the given social media profiles.
/// </summary>
/// <param name="update">BufferUpdateCreate object describing the update to create.</param>
/// <param name="profiles">List with all the social media profiles.</param>
/// <returns>HttpResponse with a BufferUpdateCreating object describing the result of the operation.</returns>
public async Task<HttpResponse<BufferUpdateCreation>> PostUpdate(BufferUpdateCreate update, IEnumerable<BufferProfile> profiles)
{
return await PostUpdate(update, profiles.Select(p => p.Id));
}

#endregion
}
}
103 changes: 103 additions & 0 deletions BufferAPI/BufferUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,107 @@ public DateTime DueAt
[JsonProperty("via")]
public string Via { get; set; }
}



/// <summary>
/// This class represents the media section of a new update to send to buffer
/// <see href="https://buffer.com/developers/api/updates#updatescreate" />
/// </summary>
[JsonObject(MemberSerialization.OptIn)]
public class BufferUpdateMedia
{
//[JsonProperty("link")]
public string Link { get; set; }

//[JsonProperty("description")]
public string Description { get; set; }

// [JsonProperty("title")]
public string Title { get; set; }

//[JsonProperty("picture")]
public string Picture { get; set; }

//[JsonProperty("photo")]
public string Photo { get; set; }

//[JsonProperty("thumbnail")]
public string Thumbnail { get; set; }

}

/// <summary>
/// This class represents the retweet section of a new update to send to buffer
/// <see href="https://buffer.com/developers/api/updates#updatescreate" />
/// </summary>
[JsonObject(MemberSerialization.OptIn)]
public class BufferUpdateRetweet
{
//[JsonProperty("tweet_id")]
public string TweetId { get; set; }

//[JsonProperty("comment")]
public string Comment { get; set; }

}




/// <summary>
/// This class represents a new update to send to buffer
/// <see href="https://buffer.com/developers/api/updates#updatescreate" />
/// </summary>
[JsonObject(MemberSerialization.OptIn)]
public class BufferUpdateCreate
{
public BufferUpdateCreate()
{
Media = new BufferUpdateMedia();
Retweet = new BufferUpdateRetweet();

Shorten = true;
Attachment = true;
}


// [JsonProperty("text")]
public string Text { get; set; }

// [JsonProperty("shorten")]
public bool Shorten { get; set; }

// [JsonProperty("now")]
public bool Now { get; set; }

// [JsonProperty("top")]
public bool Top { get; set; }

// [JsonProperty("media")]
public BufferUpdateMedia Media { get; set; }

// [JsonProperty("attachment")]
public bool Attachment { get; set; }


public DateTime ScheduledAt { get; set; }

// [JsonProperty("scheduled_at")]
public double ScheduledAtSeconds
{
get
{
return BufferAPI.TimeConversion.DateTimeToUnixTimestamp(ScheduledAt);
}
}

// [JsonProperty("retweet")]
public BufferUpdateRetweet Retweet { get; set; }


}



}
31 changes: 31 additions & 0 deletions BufferAPI/TimeConversion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BufferAPI
{
class TimeConversion
{
public static DateTime UnixTimeStampToDateTime(double? unixTimeStamp)
{
// Unix timestamp is seconds past epoch
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp.Value).ToLocalTime();
return dtDateTime;
}

public static double DateTimeToUnixTimestamp(DateTime dateTime)
{
if (dateTime == null)
{
throw new Exception("Date Time is null, cannot convert.");
}
else
{
return (dateTime - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds;
}
}
}

}