Answered Website purchase solutions

raysefo

Well-known member
Joined
Feb 22, 2019
Messages
192
Programming Experience
10+
Hi guys,

I have a web API as you will remember, that calls 3rd party web APIs in order to get online game codes that work on a market chain. Now, I would like to integrate my web API to this local website which sells lots of stuff including online game codes. In the market chain, my web API is being called from cashiers by using payment terminals. But on the other hand, I have to query the online website web API in order to get the online game code orders that are paid. I am planning to write a job that queries this online website web API for every 10 minutes in order to check if there are paid orders. Then call 3rd web APIs and continue other operations. What are your suggestions/solutions?

Best Regards.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,762
Location
Chesapeake, VA
Programming Experience
10+
How expensive (time, memory, money) is it to query the 3rd party API on demand rather than every 10 minutes? Are you batching because you have a set number of calls to the 3rd party per day?
 

raysefo

Well-known member
Joined
Feb 22, 2019
Messages
192
Programming Experience
10+
I don't know when the online game code is purchased, I have to query the online website API in order to find out if online game codes are purchased from their website. That is why I am planning to write a job.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,762
Location
Chesapeake, VA
Programming Experience
10+
If you are truly integrated with that website, why can't they call your web API the same way the market chain calls your web API? Why do you need to poll the website's web API to determine if you need to do anything? Is your web API so complicated that the web site cannot call you?
 

raysefo

Well-known member
Joined
Feb 22, 2019
Messages
192
Programming Experience
10+
Unfortunately, they don't have such a service. Their functioning is structured in this way.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,762
Location
Chesapeake, VA
Programming Experience
10+
Then it seems like you have no choice other than to poll. But you do have a choice between polling and then calling your web service API normally which in turn will call the 3rd party, or polling and then calling the 3rd party.
 

raysefo

Well-known member
Joined
Feb 22, 2019
Messages
192
Programming Experience
10+
Let's assume I get game orders by calling a local web site's API. (A local web site selling game codes, when a user makes a purchase on the web site, I am getting orders from this web site's API.) I will do this with a .net core console app which will check the web site's API every 10 minutes. Now assume I get 5 game orders from the web site's API. Inside of this .net core console app, I will call my API in order to complete the purchase of these 5 game orders as follows. (I am calling 3rd party Rest APIs in order to get game codes.)
Can you please check my code and give me some feedback\suggestions? Is there any place to cause a problem?

C#:
static async Task Main(string[] args)
{
    const int numberOfProducts = 3;
    try
    {
        for (var i = 0; i < numberOfProducts; i++)
        {
            var product = "000000001585";
            //DEV
            var request =
                        (HttpWebRequest)WebRequest.Create("http://test//api/v2/purchase");

            var svcCredentials =
                        Convert.ToBase64String(
                            Encoding.ASCII.GetBytes("username" + ":" + "password"));

            request.Headers.Add("Authorization", "Basic " + svcCredentials);
            request.Method = "POST";
            request.KeepAlive = false;
            request.ContentType = "application/x-www-form-urlencoded";

            var content = new FormUrlEncodedContent(new[]
            {
                  new KeyValuePair<string, string>("productCode", product),
                  new KeyValuePair<string, string>("quantity", "1"),
                  new KeyValuePair<string, string>("shopNo", "test"),
                  new KeyValuePair<string, string>("safeNo", "test"),
                  new KeyValuePair<string, string>("cashierNo", "test")

            });


            var formUrlEncodedContent = content;
            var urlEncodedString = await formUrlEncodedContent.ReadAsStringAsync();

            using (var streamWriter = new StreamWriter(await request.GetRequestStreamAsync()))
            {
                   await streamWriter.WriteAsync(urlEncodedString);
            }

            var httpResponse = (HttpWebResponse)(await request.GetResponseAsync());

            var response = new HttpResponseMessage
            {
                  StatusCode = httpResponse.StatusCode,
                  Content = new StreamContent(httpResponse.GetResponseStream()),
            };

            //Read response
            var htmlResponse = await response.Content.ReadAsStringAsync();
            var deserializedResult = JObject.Parse(htmlResponse);
                    
            Console.WriteLine((string)deserializedResult["coupons"]?[0]?["Serial"] + ":" + (string)deserializedResult["coupons"]?[0]?["Pin"]);
        }

            
    }
    catch (Exception e)
    {
            
         Console.WriteLine(e.Message);
         throw;
    }
}
 
Top Bottom