private AuthorizeResponse aux_authorize(int key_type, string id, string key)
{
if ((key_type==APP_ID) && ((id==null) || (id.Length <= 0))) throw new ApiException("argument error: undefined app_id");
if ((key_type==USER_KEY) && ((id==null) || (id.Length <= 0))) throw new ApiException("argument error: undefined user_key");
string URL = hostURI + "/transactions/authorize.xml";
string content = "?provider_key=" + provider_key;
if (key_type==USER_KEY) {content = content + "&user_key=" + id;}
else{content = content + "&app_id=" + id; }
if ((key_type==APP_ID) && (key!=null) ){
if (key.Length <= 0) throw new ApiException("argument error: undefined app_key");
else content = content + "&app_key=" + key;
}
URL = URL + content;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
switch (response.StatusCode)
{
case HttpStatusCode.OK:
string responseBody;
using(Stream responseStream = response.GetResponseStream())
using(StreamReader responseStreamReader = new StreamReader(responseStream, Encoding.UTF8))
{
responseBody = responseStreamReader.ReadToEnd();
}
AuthorizeResponse auth_response = SerializeHelper<AuthorizeResponse>.Ressurect(responseBody);
return auth_response;
}
}
}
catch (WebException webException)
{
if (webException.Response == null) throw webException;
HttpWebResponse response = (HttpWebResponse)webException.Response;
string responseBody;
using(Stream responseStream = response.GetResponseStream())
using(StreamReader responseStreamReader = new StreamReader(responseStream, Encoding.UTF8))
{
responseBody = responseStreamReader.ReadToEnd();
}
ApiError err;
try
{
err = SerializeHelper<ApiError>.Ressurect(responseBody);
}
catch (Exception) {
err = null;
}
if (err != null) throw new ApiException(err.code + " : " + err.message);
switch (response.StatusCode)
{
case HttpStatusCode.Forbidden:
throw new ApiException("Forbidden");
case HttpStatusCode.BadRequest:
throw new ApiException("Bad request");
case HttpStatusCode.InternalServerError:
throw new ApiException("Internal server error");
case HttpStatusCode.NotFound:
throw new ApiException("Request route not found");
case HttpStatusCode.Conflict:
AuthorizeResponse auth_response = SerializeHelper<AuthorizeResponse>.Ressurect(responseBody);
return auth_response;
default:
throw new ApiException("Unknown Exception: " + responseBody);
}
}
return null;
}