0

I am building a web API that will serve as a connector between a 3rd-party application and mine. This application will be running on a server and will be receiving POST requests from the 3rd-party application and sending POST requests of its own as a response.

Before it starts sending these requests, my web API needs to make a POST to the 3rd-party service, so it can be registered and received an authorization token, that it will be used on the requests it sends back, kinda similar to an OAuth token, from what I understand.

Since my code is all inside an HttpPost method, it only gets activated when it receives a call, and that part work as expected. When the service is authenticated and is receiving requests, is fine. The problem is when my service or the 3rd-party is restarted or something, the current token is made invalid or lost and a new one needs to be requested again.

What I wish to do is make that the call to register my service and receive the token is sent when the service starts, automatically.

Currently I am doing a manual call to trigger when my service needs to be registered, but that make it necessary for me to be at my computer to do so, and the connection is not make until I call that request.

Here is a sample of my code:

public class Controller : ApiController
    {

        static string SessionToken = "";

        [HttpPost]
        [Route("connector/webhook")]
        public async Task<HttpStatusCode> Webhook(UpdateContentRequestBody body)
        {
            var NO_ERROR = 0;

            try
            {
                if (string.IsNullOrEmpty(SessionToken))
                {
                    // This registers my service. 
                    var registerConector = ConectorOSCCApi.RegisterConector();

                    if (respostaRegistrarConector.ErrorCode != NO_ERROR)
                    {
                        throw new Exception();
                    }

                    SessionToken = registerConector.SessionToken;
                }

                ConectorApi.KeepAliveRequest(SessionToken);
                RepeatKeepAlive();

                ProccessDataAndSendResponseRequest(body);

                return HttpStatusCode.OK;
            }

            catch (Exception e)
            {
                SessionToken = "";
                return HttpStatusCode.InternalServerError;
            }

I want the method to register the service to run without the need of a call to "connector/webhook", but the rest of the processing and response to only happens when such a call is received. How can I do that?

EDIT: My code is inside a ASP.NET Web Application. I am using .NET Framework 4.5 and hosting my web application on IIS.

Caio Tsubake
  • 101
  • 3
  • 14
  • 1
    Is this full .net framework or .net core? How are you bootstrapping the application? – zaitsman Sep 10 '19 at 12:34
  • I will edit my question with that info – Caio Tsubake Sep 10 '19 at 12:39
  • Add a Boolean into the class the is set to false. The when making request check if boolean is false and only start service when false. After starting service set boolean true so you do not attempt to start service again. – jdweng Sep 10 '19 at 12:40
  • @jdweng. Okay. But won't my code still need to be called by a request for the whole logic to start? – Caio Tsubake Sep 10 '19 at 12:51
  • 1
    Depends on when you want to start the service. The boolean can be put into any class or any class can check the boolen in the Controller if the property is public static. – jdweng Sep 10 '19 at 13:03
  • I want my service to start normally. Basically when the web service starts, from what I understand. – Caio Tsubake Sep 10 '19 at 13:11

2 Answers2

0

This should do it for you :)

public class Controller : ApiController
{

        static string _sessionToken = "";
        static string SessionToken
        {
            get
            {
                if (string.IsNullOrEmpty(_sessionToken))
                {
                    InitToken();
                }
                return _sessionToken
            }
        }       

        void InitToken()
        {
        if (string.IsNullOrEmpty(_sessionToken))
                {
                    // This registers my service. 
                    var registerConector = ConectorOSCCApi.RegisterConector();

                    if (respostaRegistrarConector.ErrorCode != NO_ERROR)
                    {
                        throw new Exception();
                    }

                    _sessionToken = registerConector.SessionToken;
                }
        }

        public Controller() : base()
        {
            InitToken();
            // anything else
        }


        [HttpPost]
        [Route("connector/webhook")]
        public async Task<HttpStatusCode> Webhook(UpdateContentRequestBody body)
        {
            var NO_ERROR = 0;

            try
            {
                ConectorApi.KeepAliveRequest(SessionToken);
                RepeatKeepAlive();

                ProccessDataAndSendResponseRequest(body);

                return HttpStatusCode.OK;
            }

            catch (Exception e)
            {
                SessionToken = "";
                return HttpStatusCode.InternalServerError;
            }
        }
}
0

You don't need to wait for a request to your service to request a token.

Prerequisites : make sure you know what error code you receive from the third party API if your token is no longer correct.

When your API initializes, you will have a method available, ApplicationStart or something else in Startup.cs, depending on version, setup etc. Use that method to request the token from the third party API. Cache the token in the application level cache.

An example of caching can be found here: Caching Data in Web API

When your application receives a request, grab the token from the cache and issue the call to the third part API. If everything works, happy days. If it fails with token issue error code, then re-issue the token request and try again this time with the fresh token. Replace the cached token with the new one.

So basically, keep using a token until it fails, then automatically request a new one and update it. This way you don't need to be there to request the token manually.

You could wrap up this token logic into a service class so you don't have a lot to do in the endpoints.

Andrei Dragotoniu
  • 6,155
  • 3
  • 18
  • 32