Say I want to create a C# implementation of an API for some 3rd party service, and I want to have an option to get the json posts and responses out somehow, what would be an elegant way of doing that?
Example of a method:
public class MyApiImplementation{
public PaymentRespose CreatePayment(decimal amount, string orderId){
//some code here
}
}
When I call that method (compiled), I want to get the json form the underlying rest response, so I can debug it or log it.
I don't want to do something like this...:
public class MyApiImplementation{
public PaymentRespose CreatePayment(decimal amount, string orderId, out string json){
//some code here
}
}
When I call the api, I want to be able to get the json out somehow.
public api = new MyApiImplementation();
PaymentResponse pr = api.CreatePayment(100M, "ORDER1234");
string json = ???
I could include the json in the PaymentResponse, but... It seems like it does not belong there.
I am thinking an optional logger of some sort in a contructor overload.
Any good suggestions on how to implement this? What could it look like?
Edit:
Followup
I don't think I was clear enough in my description, so let me try to elaborate and provide a bit more code.
Purpose
I am creating a library class, which is a C# wrapper around a REST interface for a payment provider. I will provide this library to developers via Nuget.
This is what I do today
public class AwesomePaymentClient{
public Payment CreatePayment(string orderId, string currency, out string json)
{
var request = CreatePostRequest("payments");
request.AddParameter("currency", currency);
request.AddParameter("order_id", orderId);
var response = Client.Execute<Payment>(request);
json = response.Content;
VerifyResponse(response);
return response.Data;
}
//all other methods omitted from example
}
I don't particularly like the
out string jsonbit, but I would like developers to have a way to get the actual json response for their own debugging/logging purposes.
I could create an overload that does not have the out parameter, but I feel it makes the API kind of dirty - and that it is a separate concern.
I could also include a json property on Payment-class, but again I would like to keep it separate.
So I am thinking, maybe I make a contructor overload, that takes in some kind of logger or... Something, that I can't quite figure out what is.
Middleware etc is out of the question, as I am just providing a library.
Hope this helps in making my intentions clearer. :)
//some code herebit that you've omitted - that is where any such logging would go :)