Answered Website purchase solutions

raysefo

Well-known member
Joined
Feb 22, 2019
Messages
362
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.
 
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?
 
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.
 
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?
 
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.
 
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;
    }
}
 
For retry purposes, I would like to use Polly. I wonder which one is more logical, using Polly in the console application (like the one in my previous post) or should I write a new controller in my web API and use Polly in there while calling 3rd party web API?
 
I assume you are talking about polly the resilience framework used by people who programme badly and who use it as an auto handler for their faulty code?

If not, explain what you mean when you mention polly.

And if it is the formost, wouldn't you best be able to determine if that's a requirement for your app yourself?

Given your previous posts, and the scale of your complicated project, and use of someone else's API, its rather hard to answer that.

Generally, in previous years of my learning, if I was creating an application that could bring me to a complete grind, I'd bin that project and start over. You're a year on in this project and still struggling with it. Maybe its time to rethink your design build instead?
 
@Sheepings Hi, yes it is that Polly. But I couldn't see the bad programmer part;)
Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner.
 
I think what @Sheepings is trying to say is that you should actually write your own resiliency code because you intimately know the failure conditions, unlike depending on a generic resiliency library where you may have just randomly picked a resiliency strategy instead of carefully choosing one.

Bespoke solutions, specially when you need reliability tend to work better.
 

Latest posts

Back
Top Bottom