Skip to content

Instantly share code, notes, and snippets.

@vansha
Created August 26, 2012 19:00
Show Gist options
  • Save vansha/3482626 to your computer and use it in GitHub Desktop.
Save vansha/3482626 to your computer and use it in GitHub Desktop.
Invoking services via REST endpoints
[RestService("/orders/{id}", "GET")]
[RestService("/orders/by-code/{code}", "GET")]
[RestService("/orders/search", "GET")]
public class GetOrders {
public int? Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public string Customer { get; set; }
}
public class GetOrdersResponse {
public Order[] Orders { get; set; }
public ResponseStatus ResponseStatus { get; set; }
}
public class Order {
public int Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public string Customer { get; set; }
}
[RestService("/orders", "POST")] // to create new order.
[RestService("/orders/{id}", "PUT")] // to create or update existing order with specified Id.
public class SaveOrder {
public int? Id { get; set; }
// Order details.
}
public class SaveOrderResponse {
public int Id { get; set; }
public ResponseStatus ResponseStatus { get; set; }
}
IRestClient client = new JsonServiceClient();
var orderById = client.Get<GetOrdersResponse>("/orders/" + 5).Orders.Single();
var orderByCode = client.Get<GetOrdersResponse>("orders/by-code/" + Uri.EscapeDataString(orderById.Code)).Orders.Single();
var searchUrl = "orders/search?Name={0}&Customer={1}".Fmt(
Uri.EscapeDataString(orderByCode.Name), Uri.EscapeDataString(orderByCode.Customer));
var foundOrders = client.Get<GetOrdersResponse>(searchUrl).Orders;
var createOrderResponse = client.Post<SaveOrderResonse>("/orders", new SaveOrder { /* Order details */});
int orderId = createOrderResponse.Id;
var updateOrderResponse = client.Put<SaveOrderResonse>("/orders/" + orderId, new SaveOrder { /* Order details */ });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment