gotmike gotmike - 4 months ago 19
JSON Question

How can I make a subroutine out of this C# code?

I have the following C# code and I have re-used it about 20x in my code. I can't figure out how to make it into a subroutine which would allow me to re-use it b/c of the type of object is based on a different class each time.

The purpose of the code is to send an HTTP request, gather the JSON response, then serialize JSON (using a DataContract) into a class for use in other areas.

Here's what I've got:

public static ResponseAttachmentIds MakeRequestAttachmentId(string requestUrl, string strToken)
{
try
{
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
request.Headers["Authorization"] = "OAuth " + strToken;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
throw new Exception(String.Format(
"Server error (HTTP {0}: {1}).",
response.StatusCode,
response.StatusDescription));
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(ResponseAttachmentIds));
object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
ResponseAttachmentIds jsonResponse = objResponse as ResponseAttachmentIds;
response.Close();
return jsonResponse;
}
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
return null;
}

}


The DataContract classes for this particular use are:

public class ResponseAttachmentIds
{
[DataMember(Name = "done")]
public bool attachmentIds_done;
[DataMember(Name = "records")]
public List<ResponseAttachmentId> attachmentIds_records;
}
[DataContract]
public class ResponseAttachmentId
{
[DataMember(Name = "Id")]
public string attachmentId_strId { get; set; }
[DataMember(Name = "Image_Attachment_Id__c")]
public string attachmentId_strAttachmentId { get; set; }
}


The problem is that I've also got this same setup for about 20 different classes like
ResponseImages
,
ResponseProperties
,
ResponseProperty
, etc.

Any help?

Answer

I believe what you want is a generic method as such:

public static T MakeRequestAttachmentId<T>(string requestUrl, string strToken) where T : class
    {
        try
        {
            HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
            request.Headers["Authorization"] = "OAuth " + strToken;
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                if (response.StatusCode != HttpStatusCode.OK)
                    throw new Exception(String.Format(
                    "Server error (HTTP {0}: {1}).",
                    response.StatusCode,
                    response.StatusDescription));
                DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(T));
                object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
                var jsonResponse = (T)objResponse;
                response.Close();
                return jsonResponse;
            }
        }
        catch (Exception e)
        {
            System.Windows.Forms.MessageBox.Show(e.Message);
            return default(T);
        }

    }

And you can call it as such:

MakeRequestAttachmentId<ResponseAttachmentIds>("", "");