All Projects → chsakell → Multi Client Api

chsakell / Multi Client Api

Licence: mit
Building multi-client APIs in ASP.NET

Building multi-client APIs in ASP.NET

multi-client-api-06
The project introduces a method that processes JTokens in order to return only the properties requested by the client.
  • Usage
Assuming the result of the resource uri /api/tracks/1 is the following:
{
  "TrackId": 1,
  "AlbumId": 1,
  "Bytes": 11170334,
  "Composer": "Angus Young, Malcolm Young, Brian Johnson",
  "GenreId": 1,
  "MediaTypeId": 1,
  "Milliseconds": 343719,
  "Name": "For Those About To Rock (We Salute You)",
  "UnitPrice": 0.99
}

You can request only specific properties of that resource by making the request /api/tracks/1?props=bytes,milliseconds,name

{
  "Bytes": 11170334,
  "Milliseconds": 343719,
  "Name": "For Those About To Rock (We Salute You)"
}

The algorithm supports nested navigation properties as well. If /api/albums/1 returns..

{
  "AlbumId": 1,
  "ArtistName": "AC/DC",
  "Title": "For Those About To Rock We Salute You",
  "Track": [
    {
      "TrackId": 1,
      "AlbumId": 1,
      "Bytes": 11170334,
      "Composer": "Angus Young, Malcolm Young, Brian Johnson",
      "GenreId": 1,
      "MediaTypeId": 1,
      "Milliseconds": 343719,
      "Name": "For Those About To Rock (We Salute You)",
      "UnitPrice": 0.99
    },
    {
      "TrackId": 6,
      "AlbumId": 1,
      "Bytes": 6713451,
      "Composer": "Angus Young, Malcolm Young, Brian Johnson",
      "GenreId": 1,
      "MediaTypeId": 1,
      "Milliseconds": 205662,
      "Name": "Put The Finger On You",
      "UnitPrice": 0.99
    }
  ]
}

Then /api/albums/1?props=artistname,title,track(composer;name) should return the following:

{
  "ArtistName": "AC/DC",
  "Title": "For Those About To Rock We Salute You",
  "Track": [
    {
      "Composer": "Angus Young, Malcolm Young, Brian Johnson",
      "Name": "For Those About To Rock (We Salute You)"
    },
    {
      "Composer": "Angus Young, Malcolm Young, Brian Johnson",
      "Name": "Put The Finger On You"
    }
  ]
}

Properties in navigations should be semicolon (;) separated inside parethensis.

  • Example in API Controller
var _tracks = _trackRepository.GetAll(includeProperties).Skip(page).Take(pageSize);

var _tracksVM = Mapper.Map<IEnumerable<Track>, IEnumerable<TrackViewModel>>(_tracks);

string _serializedTracks = JsonConvert.SerializeObject(_tracksVM, Formatting.None,
    new JsonSerializerSettings()
    {
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
    });

JToken _jtoken = JToken.Parse(_serializedTracks);
if (!string.IsNullOrEmpty(props))
    Utils.FilterProperties(_jtoken, props.ToLower().Split(',').ToList());

return Ok(_jtoken);

The project is built in Visual Studio 2015 and ASP.NET Core but the technique and the method can be easily integrated in any version of ASP.NET API. In case you want to run the ShapingAPI application:

  1. Download the source code and open the solution in Visual Studio 2015
  2. Restore Nuget and Bower packages
  3. Install the Chinook database in your SQL Server by running the script inside the SQL folder.
  4. Alter the appsettings.json file to reflect your database environment.
  5. Run the application

Donations

For being part of open source projects and documenting my work here and on chsakell's blog I really do not charge anything. I try to avoid any type of ads also.

If you think that any information you obtained here is worth of some money and are willing to pay for it, feel free to send any amount through paypal.

Paypal
Buy me a beer

Follow chsakell's Blog

Facebook Twitter
Microsoft Web Application Development
facebook twitter-small

License

Code released under the MIT license.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].