Dealing with Users and User-Owned Data in an API

I have a question about the way I am architecting an API.

Structure

My API structure so far is like so (there is about 10 classes total, but many are similar, so I’ve reduced to these classes, and removed fields that are not relevant to the question):

Profile

  • id (unique identifier for this Profile)
  • Allergies
  • X-Rays

Allergies

  • id (unique ID for this allergy)
  • Profile (this points back to the Profile that owns these allergies)
  • name (what they are allergic to)

X-Rays

  • id (unique ID for this X-Ray)
  • Profile (points back to the Profile that owns these X-Rays)
  • thumbnail (a compressed version of the X-Ray image)
  • fullImage (the full, uncompressed X-Ray image)

Use Cases

  • Profiles are sent down the wire in full, except that in the X-Rays class, the thumbnail is sent instead of the full image. When the user wishes to see the full X-Ray image, then they make another call to download the full image (this is done to save bandwidth, it’s possible that a user has 100 X-Rays at 10MB each!).

  • A user can make modifications to Allergies or X-Rays, and these changes would have to be sent back to the server. It’d be wasteful to send the entire Profile object back, so I’d like to send just the individual X-Ray or Allergies object back to the server.

Concerns

What should my API return (the response format will be JSON) when a user downloads a full profile?

{
  "Profile": {
    "id": "1234",
    "Allergies": { 
      "id": "5678",
      "Profile": {
        "What goes here? This is a circular reference!
         I need to include a reference back to the profile
         so I can edit this Allergy client side and push the changes.
         Maybe it should be the User ID of the profile,
         but I'm not so sure that is a good idea"
      },
      "name": "peanuts"
    },
    "X-Rays": {
      "id": "3456",
      "Profile"{
          ...
      },
      "thumbnail": "Base 64 encode of the thumbnail image"
    }
  }
}

1

It looks like you are confusing your underlying model objects with your API resources, they are not necessarily mutually exclusive. If you are simply serializing your classes as JSON, you going to duplicate content (and get the profile appearing several times) which is not needed and a smell.

You could:

Create specific resource models that represent your resources better (kind of like View Models in a MVC Framework). So you could create model objects such as PatientResource, XRayResource and a AllergyResource (you don’t have to suffix the name Resource here, they could just sit somewhere separately to your other models). that better represent what your clients need to consume instead of every property of the domain model.

c# here:

public class PatientResource
{
    public int Id { get; set; }
    public IEnumerable<AllergyResource> Allergies { get; set; }
    public IEnumerable<XrayResource> XRays { get; set; }
}

public class AllergyResource
{ 
    public int Id { get; set; }
    public string Name { get; set; }
    public int ProfileId { get; set; }
}

public class XrayResource
{ 
    public int Id { get; set; }
    public string Name { get; set; }
    public int ProfileId { get; set; }
}

All very basic, the allergy and xray resource do not hold profile objects, they hold a reference id for a profile, even that would look slightly out of place, so you may need to restrict some properties depending on where your data is consumed, for example, do you really need to show an profileId if you are representing an AllergyResource in the AllergyResource collection of a PatientResource, probably not, so for that serialization you would omit that property. However, if you were in a standalone AllergyResource you would probably like to show the reference.


I like to use links in my API’s

I don’t really know what flavour of API you are creating (RESTFul, RPC, HATEOS etc) so this may not apply. I mainly deal with REST style (some may not be REST in the truest sense of the word) but when i am building an API i like to be a succinct as possible with my response objects but give the clients the ability to discover linked resources via links. This can be as complicated or simple as you like, you can use JSON or a more specific media type like HAL+JSON but the point is, you send your resources down the wire, and you enable the discover-ability of related resources via standard HTTP links.

BasecampX as an example: where they have a person, but the todo list is available at another resource endpoint which is specified in response. The objects are simplified but it does make calling the API more verbose in that multiple requests are required. It really depends on how your clients will consume the data and work with it.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật