| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- using LotteryWebApp.Common;
- using Microsoft.Extensions.Configuration;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using JsonSerializer = System.Text.Json.JsonSerializer;
- namespace LotteryWebApp.Service
- {
- public class APIFunctions
- {
- private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
- public RegisterResponse UserLoginApi(IConfiguration configuration, RegisterRequest registerRequest)
- {
- String url = configuration.GetSection("user_register").Value;
- registerRequest.Users = registerRequest.Msisdn;
- registerRequest.Command = ApiConstants.LOGIN;
- registerRequest.Channel = configuration.GetSection("channel").Value;
- registerRequest.serviceId = ApiConstants.SERVICE_ID;
- registerRequest.channel = configuration.GetSection("channel").Value;
- string jsonData = SendPost(registerRequest, ApiConstants.SERVICE_ID, url);
- RegisterResponse deptObj = JsonSerializer.Deserialize<RegisterResponse>(jsonData);
- return deptObj;
- }
- public RegisterResponse UserForgotPasswordApi(IConfiguration configuration, RegisterRequest registerRequest)
- {
- String url = configuration.GetSection("user_register").Value;
- registerRequest.Users = registerRequest.Msisdn;
- registerRequest.Command = ApiConstants.RESETPASS;
- registerRequest.Channel = configuration.GetSection("channel").Value;
- registerRequest.serviceId = ApiConstants.SERVICE_ID;
- registerRequest.channel = configuration.GetSection("channel").Value;
- string jsonData = SendPost(registerRequest, ApiConstants.SERVICE_ID, url);
- RegisterResponse deptObj = JsonSerializer.Deserialize<RegisterResponse>(jsonData);
- return deptObj;
- }
- public RegisterResponse UserChangePasswordApi(IConfiguration configuration, RegisterRequest registerRequest)
- {
- registerRequest.Command = ApiConstants.CHANGEPASS;
- registerRequest.Channel = configuration.GetSection("channel").Value;
- registerRequest.Users = registerRequest.Msisdn;
- registerRequest.serviceId = ApiConstants.SERVICE_ID;
- registerRequest.channel = configuration.GetSection("channel").Value;
- String url = configuration.GetSection("user_register").Value;
- String jsonData = SendPost(registerRequest, ApiConstants.SERVICE_ID, url);
- RegisterResponse deptObj = JsonSerializer.Deserialize<RegisterResponse>(jsonData);
- return deptObj;
- }
- public RegisterResponse UserRegisterApi(IConfiguration configuration, RegisterRequest registerRequest)
- {
- registerRequest.Command = ApiConstants.REGIST;
- registerRequest.Channel = configuration.GetSection("channel").Value;
- registerRequest.Users = registerRequest.Msisdn;
- registerRequest.serviceId = ApiConstants.SERVICE_ID;
- registerRequest.channel = configuration.GetSection("channel").Value;
- String url = configuration.GetSection("user_register").Value;
- String jsonData = SendPost(registerRequest, ApiConstants.SERVICE_ID, url);
- RegisterResponse deptObj = JsonSerializer.Deserialize<RegisterResponse>(jsonData);
- return deptObj;
- }
- public Profile UserLoadProfileApi(IConfiguration configuration, UserGetProfileRequest userGetProfileRequest)
- {
- userGetProfileRequest.usersQuery = userGetProfileRequest.users;
- userGetProfileRequest.serviceId = ApiConstants.SERVICE_ID;
- userGetProfileRequest.channel = configuration.GetSection("channel").Value;
- String url = configuration.GetSection("user_get_profile").Value;
- String jsonData = SendPost(userGetProfileRequest, ApiConstants.SERVICE_ID, url);
- Profile deptObj = JsonSerializer.Deserialize<Profile>(jsonData);
- deptObj.realPicture = deptObj.picture;
- deptObj.picture = Constants.PATH + deptObj.users + "/" + deptObj.picture;
- return deptObj;
- }
- public UserUpdateProfileResponse UserUpdateProfileApi(IConfiguration configuration, UserUpdateProfileRequest userUpdateProfileRequest)
- {
- userUpdateProfileRequest.serviceId = ApiConstants.SERVICE_ID;
- userUpdateProfileRequest.channel = configuration.GetSection("channel").Value;
- String url = configuration.GetSection("user_update_profile").Value;
- String jsonData = SendPost(userUpdateProfileRequest, ApiConstants.SERVICE_ID, url);
- UserUpdateProfileResponse deptObj = JsonSerializer.Deserialize<UserUpdateProfileResponse>(jsonData);
- return deptObj;
- }
- public UserPicUploadResponse UserPicUploadApi(IConfiguration configuration, UserPicUploadRequest userPicUploadRequest)
- {
- userPicUploadRequest.serviceId = ApiConstants.SERVICE_ID;
- userPicUploadRequest.channel = configuration.GetSection("channel").Value;
- String url = configuration.GetSection("user_pic_upload").Value;
- String jsonData = SendPost(userPicUploadRequest, ApiConstants.SERVICE_ID, url);
- UserPicUploadResponse deptObj = JsonSerializer.Deserialize<UserPicUploadResponse>(jsonData);
- return deptObj;
- }
- public UserStatus GetUserStatusApi(IConfiguration configuration, UserStatusRequest userStatusRequest)
- {
- userStatusRequest.serviceId = ApiConstants.SERVICE_ID;
- userStatusRequest.channel = configuration.GetSection("channel").Value;
- String url = configuration.GetSection("get_user_status").Value;
- String jsonData = SendPost(userStatusRequest, ApiConstants.SERVICE_ID, url);
- UserStatus deptObj = JsonSerializer.Deserialize<UserStatus>(jsonData);
- return deptObj;
- }
- public ResultOfTermResponse GetResultOfTermApi(IConfiguration configuration, ResultOfTermRequest resultOfTermRequest)
- {
- resultOfTermRequest.serviceId = ApiConstants.SERVICE_ID;
- resultOfTermRequest.channel = configuration.GetSection("channel").Value;
- String url = configuration.GetSection("get_result_of_term").Value;
- String jsonData = SendPost(resultOfTermRequest, ApiConstants.SERVICE_ID, url);
- ResultOfTermResponse deptObj = JsonSerializer.Deserialize<ResultOfTermResponse>(jsonData);
- return deptObj;
- }
- public TransferMoneyResponse TransferMoneyApi(IConfiguration configuration, TransferMoneyRequest transferMoneyRequest)
- {
- transferMoneyRequest.serviceId = ApiConstants.SERVICE_ID;
- transferMoneyRequest.channel = configuration.GetSection("channel").Value;
- String url = configuration.GetSection("transfer_money").Value;
- String jsonData = SendPost(transferMoneyRequest, ApiConstants.SERVICE_ID, url);
- TransferMoneyResponse deptObj = JsonSerializer.Deserialize<TransferMoneyResponse>(jsonData);
- return deptObj;
- }
- public SendOTPResponse SendOTPApi(IConfiguration configuration, SendOTPRequest sendOTPRequest)
- {
- sendOTPRequest.serviceId = ApiConstants.SERVICE_ID;
- sendOTPRequest.channel = configuration.GetSection("channel").Value;
- String url = configuration.GetSection("send_otp").Value;
- String jsonData = SendPost(sendOTPRequest, ApiConstants.SERVICE_ID, url);
- SendOTPResponse deptObj = JsonSerializer.Deserialize<SendOTPResponse>(jsonData);
- return deptObj;
- }
- public TransactionInfoResponse GetTransactionInfoApi(IConfiguration configuration, TransactionInfoRequest transactionInfoRequest)
- {
- transactionInfoRequest.serviceId = ApiConstants.SERVICE_ID;
- transactionInfoRequest.channel = configuration.GetSection("channel").Value;
- String url = configuration.GetSection("get_transaction_info").Value;
- String jsonData = SendPost(transactionInfoRequest, ApiConstants.SERVICE_ID, url);
- TransactionInfoResponse deptObj = JsonSerializer.Deserialize<TransactionInfoResponse>(jsonData);
- return deptObj;
- }
- public ConfirmTicketDataResponse ConfirmTicketDataApi(IConfiguration configuration, ConfirmTicketDataRequest confirmTicketDataRequest)
- {
- confirmTicketDataRequest.serviceId = ApiConstants.SERVICE_ID;
- confirmTicketDataRequest.channel = configuration.GetSection("channel").Value;
- String url = configuration.GetSection("confirm_ticket_data").Value;
- String jsonData = SendPost(confirmTicketDataRequest, ApiConstants.SERVICE_ID, url);
- ConfirmTicketDataResponse deptObj = JsonSerializer.Deserialize<ConfirmTicketDataResponse>(jsonData);
- return deptObj;
- }
- public ConfirmOTPResponse ConfirmOTPApi(IConfiguration configuration, ConfirmOTPRequest confirmOTPRequest)
- {
- confirmOTPRequest.serviceId = ApiConstants.SERVICE_ID;
- confirmOTPRequest.channel = configuration.GetSection("channel").Value;
- String url = configuration.GetSection("confirm_otp").Value;
- String jsonData = SendPost(confirmOTPRequest, ApiConstants.SERVICE_ID, url);
- ConfirmOTPResponse deptObj = JsonSerializer.Deserialize<ConfirmOTPResponse>(jsonData);
- return deptObj;
- }
- public ConfirmBuyingTicketResponse ConfirmBuyingTicketApi(IConfiguration configuration, ConfirmBuyingTicketRequest confirmBuyingTicketRequest)
- {
- confirmBuyingTicketRequest.serviceId = ApiConstants.SERVICE_ID;
- confirmBuyingTicketRequest.channel = configuration.GetSection("channel").Value;
- String url = configuration.GetSection("confirm_buying_ticket").Value;
- String jsonData = SendPost(confirmBuyingTicketRequest, ApiConstants.SERVICE_ID, url);
- ConfirmBuyingTicketResponse deptObj = JsonSerializer.Deserialize<ConfirmBuyingTicketResponse>(jsonData);
- return deptObj;
- }
- public UserTicketResponse GetUserTicketApi(IConfiguration configuration, UserTicketRequest userTicketRequest)
- {
- userTicketRequest.serviceId = ApiConstants.SERVICE_ID;
- userTicketRequest.channel = configuration.GetSection("channel").Value;
- String url = configuration.GetSection("get_user_ticket").Value;
- String jsonData = SendPost(userTicketRequest, ApiConstants.SERVICE_ID, url);
- UserTicketResponse deptObj = JsonSerializer.Deserialize<UserTicketResponse>(jsonData);
- return deptObj;
- }
- public GetTopWininerResponse GetTopWinnerApi(IConfiguration configuration, GetTopWinnerRequest userTicketRequest)
- {
- userTicketRequest.serviceId = ApiConstants.SERVICE_ID;
- String url = configuration.GetSection("get_top_winner").Value;
- String jsonData = SendPost(userTicketRequest, ApiConstants.SERVICE_ID, url);
- GetTopWininerResponse deptObj = JsonSerializer.Deserialize<GetTopWininerResponse>(jsonData);
- return deptObj;
- }
- //public AutoLoginResponse AutoLoginApi(IConfiguration configuration, AutoLoginRequest request)
- //{
- // String url = configuration.GetSection("auto_login").Value;
- // String jsonData = SendPostObj(request, ApiConstants.SERVICE_ID, url);
- // AutoLoginResponse deptObj = JsonSerializer.Deserialize<AutoLoginResponse>(jsonData);
- // return deptObj;
- //}
- public async Task<AutoLoginResponse> AutoLoginApiAsync(IConfiguration configuration, AutoLoginRequest request)
- {
- String url = configuration.GetSection("auto_login").Value;
- String jsonData = await SendFormDataPostAsync(request.token, url);
- //String jsonData = "{\"code\":200,\"message\":\"Success\",\"data\":{\"tokenGame\":\"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ1c2VySW5mb3IiLCJleHAiOjE3MDU1NTIzODgsInV1aWQiOiI2ZmQ1MWE0YzcyNzAxMTBmMzViNTFjODUyNTAwZjZmOSJ9.1cg95nEMdFTZRaccKDw7cVWArfXXq2JkSY6WrOos7Tg\",\"msisdn\":\"+50942133007\",\"name\":\"Test 007 Julyy May Skuhyu\",\"birthday\":\"1999-02-25\"}}";
- //String jsonData = "{\"code\":200,\"message\":\"Success\",\"data\":{\"tokenGame\":\"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ1c2VySW5mb3IiLCJleHAiOjE3MDU1NTIzODgsInV1aWQiOiI2ZmQ1MWE0YzcyNzAxMTBmMzViNTFjODUyNTAwZjZmOSJ9.1cg95nEMdFTZRaccKDw7cVWArfXXq2JkSY6WrOos7Tg\",\"msisdn\":\"+50955797979\",\"name\":\"Test 007 Julyy May Skuhyu\",\"birthday\":\"1999-02-25\"}}";
- //"{ "code":200,"message":"Success","data":{ "tokenGame":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ1c2VySW5mb3IiLCJleHAiOjE3MTAzODE0NTksInV1aWQiOiI1MGEwYjNlNzk5ZjllZDU3YTE1ZjZjNjljODVmYTM3NSJ9.EgR-P_KzsHJ1XCdgeu25Gy_-qAHOfM9uv9RTZSvr0AA","msisdn":"+50955797979","name":"Morgan Dinh","birthday":"1983-06-23"} }
- AutoLoginResponse deptObj = JsonSerializer.Deserialize<AutoLoginResponse>(jsonData);
- return deptObj;
- }
- public CheckAccountResponse CheckAccountApi(IConfiguration configuration, CheckAccountRequest checkAccountRequest)
- {
- checkAccountRequest.serviceId = ApiConstants.SERVICE_ID;
- String url = configuration.GetSection("check_account").Value;
- String jsonData = SendPost(checkAccountRequest, ApiConstants.SERVICE_ID, url);
- CheckAccountResponse deptObj = JsonSerializer.Deserialize<CheckAccountResponse>(jsonData);
- return deptObj;
- }
- private async Task<string> SendFormDataPostAsync(string token, String url)
- {
- log.Debug(url);
- log.Debug("Request token: " + token);
- var parameters = new Dictionary<string, string>();
- parameters.Add("token", token);
- using (HttpClient client = new HttpClient())
- {
- client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
- HttpResponseMessage response = client.PostAsync(url, new FormUrlEncodedContent(parameters)).Result;
- var responseString = response.Content.ReadAsStringAsync().Result;
- log.Debug("Response: " + responseString);
- return responseString;
- }
- }
- private String SendPost(Posting obj, String serviceId, String url)
- {
- // get configuration
- obj.serviceId = serviceId;
- obj.SV_ID = serviceId;
- obj.service_id = serviceId;
- var json = JsonConvert.SerializeObject(obj);
- var data = new StringContent(json, Encoding.UTF8, "application/json");
- log.Debug(url);
- log.Debug("Request: " + json);
- using (var client = new HttpClient())
- {
- var response = client.PostAsync(url, data).Result;
- if (response.IsSuccessStatusCode)
- {
- var responseContent = response.Content;
- // by calling .Result you are synchronously reading the result
- string responseString = responseContent.ReadAsStringAsync().Result;
- log.Debug("Response: " + responseString);
- return responseString;
- }
- else
- {
- log.Error("Response: " + response.StatusCode.ToString());
- return response.StatusCode.ToString();
- }
- }
- }
- private String SendPostObj(Object obj, String serviceId, String url)
- {
- var json = JsonConvert.SerializeObject(obj);
- var data = new StringContent(json, Encoding.UTF8, "application/json");
- log.Debug(url);
- log.Debug("Request: " + json);
- using (var client = new HttpClient())
- {
- var response = client.PostAsync(url, data).Result;
- if (response.IsSuccessStatusCode)
- {
- var responseContent = response.Content;
- // by calling .Result you are synchronously reading the result
- string responseString = responseContent.ReadAsStringAsync().Result;
- log.Debug("Response: " + responseString);
- return responseString;
- }
- else
- {
- log.Error("Response: " + response.StatusCode.ToString());
- return response.StatusCode.ToString();
- }
- }
- }
- public MoneyConvertRes GetListConvertByUser(IConfiguration configuration, MoneyConvertReq request)
- {
- request.serviceId = ApiConstants.SERVICE_ID;
- request.channel = configuration.GetSection("channel").Value;
- String url = configuration.GetSection("get_list_convert_by_user").Value;
- String jsonData = SendPost(request, ApiConstants.SERVICE_ID, url);
- MoneyConvertRes deptObj = JsonSerializer.Deserialize<MoneyConvertRes>(jsonData);
- return deptObj;
- }
- }
- }
|