Request for API to set a card cover easy
Hello!
In my project I need to update a cover on a Trello card. There is no such intuitive ability in API provided by the Manatee.Trello project currently. I'd like such ability to present. Something like card.Cover = new Cover(card.Attachments[0]);
In order to workaround and achieve this functionality currently I do
if (attachmentToUseAsCover != null)
{
JObject rootJObject = new JObject();
JObject coverJObject = new JObject();
rootJObject["cover"] = coverJObject;
coverJObject["idAttachment"] = attachmentToUseAsCover;
string json = rootJObject.ToString(Formatting.None);
HttpClient trelloHttpClient = IWalkBy.Trello.HttpClientFactory.Invoke();
using (StringContent jsonContent = new StringContent(json, Encoding.UTF8, "application/json"))
{
Uri cardUri = new Uri("https://trello.com/1/cards/" + card.Id);
Task<HttpResponseMessage> putTask = trelloHttpClient.PutAsync(cardUri, jsonContent);
putTask.Wait();
}
}
Where attachmentToUseAsCover is id of an attachment of the card, JObject is Newtonsoft.Json.Linq.JObject and IWalkBy.Trello.HttpClientFactory.Invoke is
public static HttpClient Invoke()
{
HttpClient res = TrelloConfiguration.HttpClientFactory.Invoke();
string authorizationHeader = String.Format(CultureInfo.InvariantCulture,
"OAuth oauth_consumer_key=\"{0}\", oauth_token=\"{1}\"",
Uri.EscapeDataString(TrelloAuthorization.Default.AppKey),
Uri.EscapeDataString(TrelloAuthorization.Default.UserToken));
res.DefaultRequestHeaders.Add("Authorization", authorizationHeader);
return res;
}
It's important to specify application/json content type. Otherwise the response code from Trello server is 200, but nothing happens. To read about other possibilities what can be set for card cover you may look here https://community.developer.atlassian.com/t/trello-rest-api-card-colors-covers/38865/9
Thank you.