Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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