2014-02-28 17:31:49 +00:00
|
|
|
/**
|
|
|
|
* @namespace Typertext
|
|
|
|
* @module Json
|
|
|
|
*/
|
2014-02-26 19:12:37 +00:00
|
|
|
module Typertext.Json {
|
|
|
|
import HttpRequest = Typertext.Http.HttpRequest;
|
|
|
|
import HttpResponse = Typertext.Http.HttpResponse;
|
|
|
|
import HttpResponseStatus = Typertext.Http.HttpResponseStatus;
|
|
|
|
import HttpUrl = Typertext.Http.HttpUrl;
|
|
|
|
import HttpPostData = Typertext.Http.HttpPostData;
|
|
|
|
import HttpMethod = Typertext.Http.HttpMethod;
|
2014-04-16 15:40:03 +00:00
|
|
|
import TransportConstructor = Typertext.Transport.TransportConstructor;
|
2014-02-26 19:12:37 +00:00
|
|
|
|
|
|
|
export class JsonRequest implements Typertext.GenericRequest<JsonResponseHandler> {
|
|
|
|
private jsonType:string;
|
|
|
|
private request:HttpRequest;
|
|
|
|
|
2014-02-28 17:31:49 +00:00
|
|
|
/**
|
|
|
|
* A class to simplify passing both the status and data of a completed proxy request
|
|
|
|
*
|
|
|
|
* @interface GenericRequest
|
|
|
|
* @uses Typertext.Http.HttpRequest;
|
|
|
|
* @uses Typertext.Http.HttpResponse;
|
|
|
|
* @uses Typertext.Http.HttpResponseStatus;
|
|
|
|
* @uses Typertext.Http.HttpUrl;
|
|
|
|
* @uses Typertext.Http.HttpPostData;
|
|
|
|
* @uses Typertext.Http.HttpMethod;
|
|
|
|
*
|
|
|
|
* @author Kegan Myers <kegan@keganmyers.com>
|
|
|
|
* @version 0.3.0
|
2014-03-12 18:56:11 +00:00
|
|
|
* @constructor
|
2014-02-28 17:31:49 +00:00
|
|
|
*/
|
2014-03-12 18:56:11 +00:00
|
|
|
constructor(jsonContentType:string = "application/json") {
|
2014-02-26 19:12:37 +00:00
|
|
|
this.request = new HttpRequest();
|
|
|
|
this.jsonType = jsonContentType;
|
|
|
|
}
|
|
|
|
|
2014-02-28 17:31:49 +00:00
|
|
|
/**
|
|
|
|
* A convenience method for simply calling a GET
|
|
|
|
*
|
|
|
|
* @param {HttpUrl} request
|
|
|
|
* @param {JsonResponseHandler} callback
|
|
|
|
*/
|
2014-02-26 19:12:37 +00:00
|
|
|
public Get(request:HttpUrl, callback:JsonResponseHandler):void {
|
|
|
|
this.RawRequest(HttpMethod.GET, request, {}, callback);
|
|
|
|
}
|
|
|
|
|
2014-02-28 17:31:49 +00:00
|
|
|
/**
|
|
|
|
* A convenience method for simply calling a POST
|
|
|
|
*
|
|
|
|
* @param {HttpUrl} request
|
|
|
|
* @param {HttpPostData} postData
|
|
|
|
* @param {JsonResponseHandler} callback
|
|
|
|
*/
|
2014-02-26 19:12:37 +00:00
|
|
|
public Post(request:HttpUrl, postData:HttpPostData, callback:JsonResponseHandler):void {
|
2014-03-04 01:04:49 +00:00
|
|
|
this.RawRequest(HttpMethod.POST, request, postData, callback);
|
2014-02-26 19:12:37 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 17:31:49 +00:00
|
|
|
/**
|
|
|
|
* A layer to automatically decode a response into a JSON object
|
|
|
|
*
|
|
|
|
* @param {HttpMethod} method
|
|
|
|
* @param {HttpUrl} request
|
|
|
|
* @param {HttpPostData} postData
|
|
|
|
* @param {JsonResponseHandler} callback
|
2014-04-16 15:40:03 +00:00
|
|
|
* @param {TransportConstructor} transport
|
2014-02-28 17:31:49 +00:00
|
|
|
*/
|
2014-04-16 15:40:03 +00:00
|
|
|
public RawRequest(method:HttpMethod, request:HttpUrl, postData:Typertext.Http.HttpPostData = {}, callback?:JsonResponseHandler, transport?:TransportConstructor) {
|
2014-03-12 18:56:11 +00:00
|
|
|
//Ensure we have an executable callback
|
|
|
|
if (typeof callback != "function") {
|
|
|
|
//Make a request and ignore the response, throwing exceptions in async code can be weird
|
|
|
|
this.request.RawRequest(method, request, postData, ()=>{});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Make a full request and handle the response
|
2014-02-26 19:12:37 +00:00
|
|
|
this.request.RawRequest(method, request, postData, (response:HttpResponse)=> {
|
|
|
|
//Make sure that we got the Json content type we are expecting
|
|
|
|
if (response.GetContentType() != this.jsonType) {
|
2014-03-12 18:56:11 +00:00
|
|
|
//If not it is an invalid server response
|
|
|
|
callback(JsonResponse.fromInvalidHttpResponse(response));
|
|
|
|
return;
|
2014-02-26 19:12:37 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 18:56:11 +00:00
|
|
|
//If it is then we can just pass it straight through to the JSON response
|
|
|
|
callback(JsonResponse.fromHttpResponse(response));
|
2014-04-16 15:40:03 +00:00
|
|
|
}, transport);
|
2014-02-26 19:12:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|