Esempio n. 1
0
        /// <summary>
        /// Initiates and suspends the job.
        /// </summary>
        /// <returns>A DTO containing information about the state of the
        /// suspended job. This DTO is used to complete the job</returns>
        /// <param name="client">Client.</param>
        public async Task <InfoRequestJobDTO> InitiateJob(Client client)
        {
            await Setup(client);

            InfoRequestJobDTO dto = new InfoRequestJobDTO()
            {
                ClientID   = client.Id,
                EncodedSyn = _infoRequester.CreateEncodedSyn(),
                SessionID  = _infoRequester.SuspendSession()
            };

            return(dto);
        }
        public async Task <IActionResult> Post([FromBody] ReadRequestDTO value)
        {
            //TODO: Temporary seeding of the database, for demonstration purposes
            if (GetClientByApiKey("1234") == null)
            {
                Client newClient = new Client()
                {
                    APIKey     = "1234",
                    IDUID      = 1,
                    PrivateKey = "9e6a6bf412ce4e3a91a33c7c0f6d94b3127b8d4f5ed336210a672fe595bf1769"
                };

                _clientContext.Add(newClient);
                _clientContext.SaveChanges();
            }

            //Get the client by the supplied API key
            Client client;

            if ((client = GetClientByApiKey(value.APIKey)) == null)
            {
                return(Unauthorized());
            }

            //Setup the Info Request Job
            InfoRequestWorker worker = new InfoRequestWorker(_clientContext, _idMapper, _web3, _serviceProvider);
            InfoRequestJobDTO dto    = await worker.InitiateJob(client);

            BackgroundJob.Enqueue <InfoRequestWorker>(w => w.CompleteJob(dto, value.CallbackEndpoint));

            //Prepare the SYN for output
            StringToQRSVGValueConverter valConv = new StringToQRSVGValueConverter();
            string        qrEncodedSyn          = valConv.Convert(dto.EncodedSyn).Content;
            ContentResult content = Content(qrEncodedSyn);

            content.ContentType = "image/svg+xml";

            return(content);
        }
Esempio n. 3
0
        /// <summary>
        /// Completes the job. To be call in Hangfire
        /// </summary>
        /// <returns>The job.</returns>
        /// <param name="dto">Suspended Job DTO, used to restore and
        /// complete the job.</param>
        /// <param name="callbackEndpoint">Callback endpoint (where the
        /// result of the job should be sent).</param>
        public async Task CompleteJob(InfoRequestJobDTO dto, string callbackEndpoint)
        {
            await Setup(dto.ClientID);

            ManualResetEvent waitHandle = new ManualResetEvent(false);
            ID id = null;

            _infoRequester.HandshakeComplete += (sender, e) =>
            {
                id = e.ReceivedID;
                waitHandle.Set();
            };
            _infoRequester.ResumeSession(dto.SessionID);

            if (waitHandle.WaitOne(100000))
            {
                await new HttpClient().PostAsync(callbackEndpoint, new System.Net.Http.StringContent(JsonConvert.SerializeObject(id)));
            }
            else
            {
                await new HttpClient().PostAsync(callbackEndpoint, new System.Net.Http.StringContent("error"));
            }
        }