Let's take an example of a generic API endpoint that looks as follows:
https://api.imgur.com/3/gallery/{section}/{sort}/{window}/{showViral}/{page} Conditions on this endpoint are as follows:
- Arguments are optional. Default values will be used if not provided.
- All the earlier arguments must be provided in order to use an argument. So in this example, if I were to get posts of page no 2, I must provide section, sort, window and showViral values.
I am trying to create a C# wrapper around such endpoints. My method looks somewhat as follows:
public static async Task<List<Image>> GetGallery(Section? section = null, Sort? sort = null, Window? window = null, bool? showViral = null, int? page = null) { string uri = "gallery"; if (section != null) { uri += "/" + section.ToString().ToLower(); if(sort != null) { uri += "/" + sort.ToString().ToLower(); if(window != null) { uri += "/" + window.ToString().ToLower(); if (showViral != null) { uri += "/" + showViral.ToString(); if (page != null) { uri += "/" + page; } } } } } JObject response = await NetworkHelper.ExecuteRequest(uri); return response["data"].ToObject<List<Image>>(); } I'm not really happy with this implementation. This code looks very ugly. Any thoughts on how I can improve this?