nef
nef

Reputation: 1

How can I return two APIs in one GET?

First of all, I want to say that I'm new to programming. I'm using ASP.net and trying to make an API for a personal project and using JSON for the results.

I can make two pages that look like this (edited for readability).

{
    "user": 
        {"owner_id":1, "name":"Joe", "country":"USA"}, 
        {"owner_id":2, "name":"Bob", "country":"Canada"}
} 

{
    "pet": 
        {"id":1, owner_id:"1", "animal":"dog", "name":"Spot"}, 
        {"id":2, owner_id:"2", "animal":"cat", "name":"Snowball"}, 
        {"id":3, owner_id:"2", "animal":"fish", "name":"Bubble"} 
}

What I want is to combine these two into one "owners" API page that looks kind of like this:

[
    {
        "user": 
        [
            "owner_id":1, "name":"Joe", "country":"USA"
        ], 
        "pet":
        [
            "id":1, owner_id:"1", "animal":"dog", "name": "Spot"
        ]
    }, 

    {
        "user":
        [
            "owner_id":2, "name": "Bob", "country": "Canada"
        ], 
        "pet": 
        [
            "id":2, owner_id:"2", "animal":"cat", "name":"Snowball"
        ], 
        [
            "id":3, owner_id:"2", "animal":"fish", "name":"Bubble"
        ]
    }
]

I've Googled and checked here a lot and a lot of the explanations are going over my head. I tried a ton out and even the nicest looking ones didn't work.

Here's an example of something I tried that didn't work..

using System.Collections.Generic;

namespace TestAPI.Models

public class user
{
    public int id { get; set; }
    public string name { get; set; }
    public string country { get; set; }
}

public class pet
{
    public int id { get; set; }
    public string animal { get; set; }
    public string name { get; set; }
    public int owner_id { get; set; } 
}

public class owner
{
    public int Id { get; set; }
    public List<user> user{ get; set; }
    public List<pet> pet{ get; set; }
}

Here's an example of one of... many different wrong results I've gotten in trying this.

[{"Id":1,"user":null,"pet":null},
{"Id":2,"user":null,"pet":null}]

Any help at all would be appreciated, and any leads. This is my first time working with any of this stuff so I'm a mess but I've been going through lots of tutorials to try to get it down.

Upvotes: 0

Views: 461

Answers (2)

Jamie
Jamie

Reputation: 3372

Look at your JSON above, if that is truly what you want as an output you need to change your model. It looks like you want to search for a user and return owners and pets. So I would change your relationship like so and pluralize your owner and pet properties on user to reflect that it is a list.

public class Owner {
    public int Id {get;set;}
    public string Name {get;set;}
    public string Country {get;set}
}

public class Pet {
    public int Id { get; set; }
    public string Animal { get; set; }
    public string Name { get; set; }
    public int OwnerId { get; set; } 
}

public class User {
    public int Id {get;set;}
    public List<Owner> Owners {get;set;}
    public List<Pet> Pets {get;set}
}

Now when you return the user object from your API call, I am assuming that is what you are doing, you should get what you expect.

Updated based on the null lists. I would add a constructor to the User and set your Pets and Owners to an empty list.

public class User {
    public int Id {get;set;}
    public List<Owner> Owners {get;set;}
    public List<Pet> Pets {get;set;}

    public User() {
        Owners = new List<Owner>():
        Pets = new List<Pet>():
    }
}

Upvotes: 2

Jeff Schreib
Jeff Schreib

Reputation: 119

Create a wrapper api that calls both the pet and user api and merges the results and returns it. So that you will need to make 1 get call to get both datasets merged. After the call to the wrapper api it will call both the apis and after that you can iterate thru the user collection and add each pet to the new merge model (Ex UserWithPet) and as suggested you can use Newtonsoft to easily convert to Json. User class above seems a good way.

Upvotes: 0

Related Questions