Gravatar C# API

Copenhagen based cloud developer. Love to code and to make complex solutions simple!
Search for a command to run...

Copenhagen based cloud developer. Love to code and to make complex solutions simple!
No comments yet. Be the first to comment.
And examples for .NET projects also

...and why Windows is not going to sleep when left idle

TL;DR Keep trying the firmware update - after around 30 attempts doing the same thing, it just worked 🤯🎉🪅 Awesome Ultrawide Monitor, not only for gaming For once I'm going to write an article which is not directly related to programming, but if yo...

When using GitHub actions it's pretty neat that you can use the hosted GitHub runners. However, this can get costly if you use a lot of runners and they are not always that fast at running the actions. Luckily you can also host your own runners and w...

...and some SonarCloud code analysis!

Several years ago, around 2015, I needed a way to get some basic profile information for users in an app I did. I decided to use Gravatar because they let you get some basic user information if you just pass them an email address.
The library is pretty simple, it just uses a HttpClient to call Gravatar and translate the response into a C# object.
Back then I (and many other developers) mistakenly thought that HttpClient was supposed to be disposed after each usage mainly because it exposes the IDisposable interface and as a good .net developer you have learned to always dispose your resources when you no longer need them, more on that in this Visual Studio Magazine article.
So, now it's 2021 and I wanted to use the API in a new Azure Function (a post on that coming up shortly) and of course wanted to update the API to use HttpClient correctly. Thus I made an update to it.
I have put the code in this public Github repository. I have also published the library as a Nuget for you to include in your projects:
Install-Package GravatarSharp.Core -Version 0.9.0.2
There are 2 features in the library; getting the user profiel and getting the profile image.
Here is how to get the image url for a given email:
/// <summary>
/// Gets the Gravatar image url for the given user/email
/// </summary>
/// <param name="email">The email that will be used to request the user image url</param>
/// <param name="width">The width in pixels. Default is 128</param>
/// <returns>The image url corresponding to the provided email address</returns>
public static string GetImageUrl(string email, int width = 128)
{
return $"http://www.gravatar.com/avatar/{Hashing.CalculateMd5Hash(email)}?s={width}&d=identicon&r=PG";
}
As you might have noticed, this is hashing the email, because that is what Gravatar accepts, this is done like so:
/// <summary>
/// Calculate the hash based on this MSDN post:
/// http://blogs.msdn.com/b/csharpfaq/archive/2006/10/09/how-do-i-calculate-a-md5-hash-from-a-string_3f00_.aspx
/// </summary>
/// <param name="input">The input to hash</param>
/// <returns>The hashed and lowered string</returns>
public static string CalculateMd5Hash(string input)
{
// step 1, calculate MD5 hash from input
var md5 = MD5.Create();
var inputBytes = Encoding.ASCII.GetBytes(input);
var hash = md5.ComputeHash(inputBytes);
// step 2, convert byte array to hex string
var sb = new StringBuilder();
foreach (var t in hash) sb.Append(t.ToString("X2"));
return sb.ToString().ToLower();
}
The second feature is to get the user info that Gravatar has on a given email:
/// <summary>
/// Gets the Gravatar profile for the given user/email
/// </summary>
/// <param name="email">The email that will be used to request the user profile</param>
/// <returns>The user profile corresponding to the provided email address</returns>
public async Task<GetProfileResult> GetProfile(string email)
{
var json = await GetStringResponse($"https://en.gravatar.com/{Hashing.CalculateMd5Hash(email)}.json");
if (string.IsNullOrEmpty(json.ErrorMessage))
return new GetProfileResult
{
Profile = new GravatarProfile(JsonConvert.DeserializeObject<Profile>(json.Result), json.Result)
};
return new GetProfileResult
{
ErrorMessage = json.ErrorMessage
};
}
Here we just append .json to the request url and then Gravatar will return a JSON file which the library then translate into a C# object for you to use.
Remember, you can see the full source code of this library:
Thanks for reading, please let me know if you find this useful or if you have any other feedback. I would love to get your comments!
Also, stay tunes, I will soon post an article on how to use this library in an Azure Function especially how to use dependency injection to inject the HttpClient.