public IActionResult Update(long applicationId, long id, [FromBody] ApplicationTokenViewModel token) { token.ApplicationTokenId = id; token.ApplicationId = applicationId; return(Ok(_applicationTokenService.Update(token))); }
/// <summary> /// Verify token /// </summary> private void CheckToken() { if (tokenModel == null || string.IsNullOrEmpty(tokenModel.Token)) { tokenModel = LoginApplication().GetAwaiter().GetResult(); } }
public ApplicationToken Create(ApplicationTokenViewModel toCreate) { if (string.IsNullOrEmpty(toCreate.Name)) { throw new EntityValidationException("Token Name cannot be blank!"); } if (!ApplicationExists(toCreate.ApplicationId)) { throw new EntityValidationException("Application does not exist!"); } if (_db.ApplicationTokens.Active() .Any(at => at.ApplicationId == toCreate.ApplicationId && at.Name == toCreate.Name)) { throw new EntityValidationException("A token with this name already exists for this application."); } var token = new ApplicationToken() { Name = toCreate.Name, ApplicationId = toCreate.ApplicationId, Active = true }; _db.ApplicationTokens.Add(token); _db.SaveChanges(); return(Get(token.ApplicationTokenId)); }
public static ApplicationTokenViewModel ToViewModel(this ApplicationToken token) { var vm = new ApplicationTokenViewModel() { ApplicationId = token.ApplicationId, ApplicationTokenId = token.ApplicationTokenId, Name = token.Name }; return(vm); }
public ApplicationToken Update(ApplicationTokenViewModel toUpdate) { if (string.IsNullOrEmpty(toUpdate.Name)) { throw new EntityValidationException("Token Name cannot be blank!"); } var token = GetOrException(toUpdate.ApplicationTokenId); if (token.ApplicationId != toUpdate.ApplicationId) { throw new EntityValidationException("You cannot change the Application of the Token after creation!"); } token.Name = toUpdate.Name; _db.SaveChanges(); return(token); }
public DInServicesApi(ApplicationTokenViewModel tokenModel = null) { this.apiUrlBase = Utils.GetAppSetting("DInApiUrl"); this.httpClient = new HttpClient(); this.httpClient.BaseAddress = new Uri(this.apiUrlBase); this.credentials = new AppAuthViewModel() { ApiKey = Utils.GetAppSetting("ApiKey"), SecretKey = Utils.GetAppSetting("SecretKey"), UseHeadQuarter = false, }; if (tokenModel != null) { this.tokenModel = tokenModel; } else { this.tokenModel = new ApplicationTokenViewModel(); } }
/// <summary> /// Get token for api usage /// </summary> /// <returns>Token</returns> public async Task <ApplicationTokenViewModel> LoginApplication() { this.httpClient.SetHeaders(tokenModel.Token, true); var content = JsonConvert.SerializeObject(this.credentials); var buffer = Encoding.UTF8.GetBytes(content); var byteContent = new ByteArrayContent(buffer); byteContent.Headers.ContentType = new MediaTypeHeaderValue(Constants.HTTP_MEDIA_TYPE); try { var response = httpClient.PostAsync(Constants.API_ROUTE_APP_LOGIN, byteContent).GetAwaiter().GetResult(); Stream stream = await response.Content.ReadAsStreamAsync(); StreamReader reader = new StreamReader(stream); var json = reader.ReadToEnd(); tokenModel = JsonConvert.DeserializeObject <ApplicationTokenViewModel>(json); } catch { tokenModel = new ApplicationTokenViewModel(); } return(tokenModel); }
/// <summary> /// Execute any endpoint from API /// </summary> /// <param name="model">Endpoint execution data</param> /// <returns>ServerResponse from endpoint</returns> public async Task <ServerResponse> ExecuteEndpointTask(EndpointTaskViewModel model) { ServerResponse result; try { CheckToken(); this.httpClient.SetHeaders(tokenModel.Token); var content = model.Body; var buffer = Encoding.UTF8.GetBytes(content); var byteContent = new ByteArrayContent(buffer); byteContent.Headers.ContentType = new MediaTypeHeaderValue(Constants.HTTP_MEDIA_TYPE); HttpResponseMessage response = null; switch (model.HttpMethod) { case Enums.HttpMethods.GET: response = httpClient.GetAsync(model.EndpointSufix).GetAwaiter().GetResult(); break; case Enums.HttpMethods.POST: response = httpClient.PostAsync(model.EndpointSufix, byteContent).GetAwaiter().GetResult(); break; case Enums.HttpMethods.PUT: response = httpClient.PutAsync(model.EndpointSufix, byteContent).GetAwaiter().GetResult(); break; case Enums.HttpMethods.DELETE: response = httpClient.DeleteAsync(model.EndpointSufix).GetAwaiter().GetResult(); break; case Enums.HttpMethods.OPTIONS: default: break; } ; if (response != null && response.StatusCode == HttpStatusCode.Unauthorized) { this.tokenModel = null; this.tokenModel = GetCurrentToken(); if (tryCounter < 3) { tryCounter++; return(await ExecuteEndpointTask(model)); } else { tryCounter = 0; result = UtilsApi.ErrorResponse(Constants.ERROR_MAX_RETRY); } } else { tryCounter = 0; result = await UtilsApi.GetServerResponse(response); } } catch { result = UtilsApi.ErrorResponse(Constants.ERROR_API_UNAVAILABLE); } return(result); }