예제 #1
0
 public AccountController(
     JwtConfigurations jwtConfigurations,
     APIConfigurations apiConfigurations)
 {
     _jwtConfigurations = jwtConfigurations;
     _ApiConfigurations = apiConfigurations;
 }
예제 #2
0
        public void ConfigureServices(IServiceCollection services)
        {
            var jwtConfigurations = new JwtConfigurations();

            new ConfigureFromConfigurationOptions <JwtConfigurations>(_configuration.GetSection("Jwt")).Configure(jwtConfigurations);

            var apiConfigurations = new APIConfigurations();

            new ConfigureFromConfigurationOptions <APIConfigurations>(_configuration.GetSection("API")).Configure(apiConfigurations);

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidateAudience         = true,
                    ValidateLifetime         = true,
                    ValidateIssuerSigningKey = true,
                    ValidIssuer      = jwtConfigurations.Issuer,
                    ValidAudience    = jwtConfigurations.Audience,
                    IssuerSigningKey = JwtSecurityKey.Create(jwtConfigurations.Key)
                };
            });

            services.AddMvc();
            services.AddCors();

            services.AddSwaggerGen(s =>
            {
                s.SwaggerDoc("v1", new Info
                {
                    Version        = "v1",
                    Title          = "Where's my bus API",
                    Description    = "API wrapper for SPTrans API",
                    TermsOfService = "N/A",
                    Contact        = new Contact
                    {
                        Name  = "Fabio Gemignani",
                        Email = "",
                        Url   = "https://github.com/fgemig"
                    }
                });
            });

            services.AddSingleton(jwtConfigurations);
            services.AddSingleton(apiConfigurations);

            services.AddScoped <IHttpRequest <HttpRequestHandler>, HttpRequestHandler>();
        }
예제 #3
0
        public string Delete(int id)
        {
            try
            {
                using (HttpClient client = HttpContext.GetHttpClient())
                {
                    using (HttpResponseMessage response = client.DeleteAsync(APIConfigurations.UrlHabilidadeUnico(id)).Result)
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            return("OK");
                        }
                    }
                }

                throw new Exception($"Ocorreu um erro ao excluir a habilidade única com ID {id}.");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #4
0
        public HabilidadeUnico Get(int id)
        {
            try
            {
                using (HttpClient client = HttpContext.GetHttpClient())
                {
                    using (HttpResponseMessage response = client.GetAsync(APIConfigurations.UrlHabilidadeUnico(id)).Result)
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            return(JsonConvert.DeserializeObject <HabilidadeUnico>(response.Content.ReadAsStringAsync().Result));
                        }
                    }
                }

                throw new Exception($"Ocorreu um erro ao buscar a habilidade única com ID {id}.");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public HabilidadePaged Get(int funcionarioId, int pageSize, int page)
        {
            try
            {
                using (HttpClient client = HttpContext.GetHttpClient())
                {
                    using (HttpResponseMessage response = client.GetAsync(APIConfigurations.UrlHabilidades(funcionarioId, pageSize, page)).Result)
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            return(JsonConvert.DeserializeObject <HabilidadePaged>(response.Content.ReadAsStringAsync().Result));
                        }
                    }
                }

                throw new Exception($"Ocorreu um erro ao buscar as habilidades do funcionário com ID {funcionarioId}.");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #6
0
        public IEnumerable <HabilidadeUnico> Get()
        {
            try
            {
                using (HttpClient client = HttpContext.GetHttpClient())
                {
                    using (HttpResponseMessage response = client.GetAsync(APIConfigurations.UrlHabilidadeUnico()).Result)
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            string json = response.Content.ReadAsStringAsync().Result;
                            return(JsonConvert.DeserializeObject <IEnumerable <HabilidadeUnico> >(json));
                        }
                    }
                }

                throw new Exception($"Ocorreu um erro ao buscar as habilidades únicas.");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #7
0
        public HabilidadeUnico Post(HabilidadeUnico habilidade)
        {
            try
            {
                using (HttpClient client = HttpContext.GetHttpClient())
                {
                    string      jsonData = JsonConvert.SerializeObject(habilidade);
                    HttpContent content  = new StringContent(jsonData, Encoding.UTF8, "application/json");
                    using (HttpResponseMessage response = client.PostAsync(APIConfigurations.UrlHabilidadeUnico(), content).Result)
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            return(JsonConvert.DeserializeObject <HabilidadeUnico>(response.Content.ReadAsStringAsync().Result));
                        }
                    }
                }

                throw new Exception($"Ocorreu um erro ao criar a habilidade única.");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public FuncionarioSimples Put(int id, FuncionarioSimples funcionario)
        {
            try
            {
                using (HttpClient client = HttpContext.GetHttpClient())
                {
                    string      jsonData = JsonConvert.SerializeObject(funcionario);
                    HttpContent content  = new StringContent(jsonData, Encoding.UTF8, "application/json");
                    using (HttpResponseMessage response = client.PutAsync(APIConfigurations.UrlFuncionario(id), content).Result)
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            return(JsonConvert.DeserializeObject <FuncionarioSimples>(response.Content.ReadAsStringAsync().Result));
                        }
                    }
                }

                throw new Exception($"Ocorreu um erro ao atualizar o funcionário com ID {id}.");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #9
0
 public HttpRequestHandler(APIConfigurations configuration, IHttpContextAccessor context)
 {
     _configuration = configuration;
     _context       = context;
 }
        public Habilidade Patch(int id, Habilidade habilidade)
        {
            try
            {
                using (HttpClient client = HttpContext.GetHttpClient())
                {
                    string             jsonData = JsonConvert.SerializeObject(habilidade);
                    HttpRequestMessage request  = new HttpRequestMessage(new HttpMethod("PATCH"), APIConfigurations.UrlHabilidade(id))
                    {
                        Content = new StringContent(jsonData, Encoding.UTF8, "application/json")
                    };

                    using (HttpResponseMessage response = client.SendAsync(request).Result)
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            return(JsonConvert.DeserializeObject <Habilidade>(response.Content.ReadAsStringAsync().Result));
                        }
                    }
                }

                throw new Exception($"Ocorreu um erro ao atualizar a habilidade com ID {id}.");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }