简体   繁体   中英

Looking for client side Json/WebAPI package to send HttpGet/HttpPost messages

BACKGROUND currently I am in a side project where I am building a Xamarin based mobile App for easy movie and tv show searching and navigation based on user preferences (eg based on movie genre etc).

To do this the mobile app acts as a client to the https://www.themoviedb.org/documentation/api online movie database's Web API service.

PROBLEM I am looking for an easy and well supported package to wrap API Uri paths and query parameters into a web api query. I have looked a bit on RestSharp .

I like the syntax very much with the pattern based query path:

var request = new RestRequest("resource/{id}"); 
request.AddParameter("name", "value");
request.AddUrlSegment("id", "123"); 

but I am not sure about the packages future. Also if there is an alternative from MS, I would take that.

So have also looked Web-API tailored at MS alternatives but I am unsure what is recommended there. I only can find docs and examples for the server side (ASP.NET CORE MVC).

I need to be pointed to a well functioning .NET Standard 2.0 package from Microsoft or from a 3rd party to send Web API request from my Xamarin client.

Where am II have already written a lot of code based on pure HttPClient and AspNetcore.WebUtilituies for assembling a query string. But the gazillions of API path segments are getting out of my hand. I really need something to manage API path templates like RestSharp does

Code sample:

here I declare all of the path segments which I assemble manually ==> ugly AF

public static class WebApiPathConstants
{
public const string BASE_Address = "https://api.themoviedb.org";
public const string BASE_Path = "/3";
public const string CONFIG_Path = "/configuration";
public const string GENRE_LIST_Path = "/genre/movie/list";
...
lot of lines here
....
public const string PAGE_Key = "page";
public const string INCLUDE_Adult_Key = "include_adult";
public const string API_KEY_Key = "api_key";
public const string RECOMMENDATIONS_Path = "/recommendations";
public const string SIMILARS_Path = "/similar";
}

Here I assemble a query and kick of a task to get movie details based on the query from the server: The assembly of the Url path is my main problem. It looks too error prone.

    public async Task<FetchMovieDetailsResult> FetchMovieDetails(int id, string language = null, int retryCount = 0, int delayMilliseconds = 1000)
    {
        string baseUrl = BASE_Address + BASE_Path + MOVIE_DETAILS_Path + "/" + id;

        var query = new Dictionary<string, string>();
        query.Add(API_KEY_Key, apiKeyValue);

        if (!string.IsNullOrEmpty(language))
            query.Add(LANGUAGE_Key, language);

        string requestUri = QueryHelpers.AddQueryString(baseUrl, query);

        FetchMovieDetailsResult result = await GetResponse<FetchMovieDetailsResult>(retryCount, delayMilliseconds, requestUri);

        return result;
    }

The result is a POCO class with the HttpStatusCode and (if successful ) a Json object. The client accesses the Json object only if the StatusCode == 2xx.

Prepared to be shot down in flames here, if this doesn't match your use-case, but it looks like the TheMovieDb site itself has a list of client libraries. It's available here: https://www.themoviedb.org/documentation/api/wrappers-libraries . They're obviously a layer higher than you're asking for here, in that they completely wrap the API, such that you need not even know what you're calling or how you're calling it, but in the interests of getting the job done, they seem like they'd do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM