Bladeren bron

configurable base url and subresource url

master
Flo Smilari 4 jaren geleden
bovenliggende
commit
5f85e89656

+ 5
- 0
Pages/IndexPage.razor Bestand weergeven

@@ -1,12 +1,16 @@
@page "/"
@using cwebplusApp.Shared.Services;
@using System.Threading;
@using Microsoft.Extensions.Configuration;
@inject NavigationManager NavigationManager;
@inject AppState AppState;
@inject IStringLocalizer<Resources> i18n
@inject PageHistoryManager PageHistoryManager
@inject MasterDataService MasterDataService;
@inject Toaster Toaster;
@inject IConfiguration Configuration;
@inject ILFBicycleRest iLFBicycleRest;
<div class="row h-100 justify-content-center">
@@ -47,6 +51,7 @@
showProgressCircle = true;
StateHasChanged();
try {
iLFBicycleRest.Initialize(Configuration);
await MasterDataService.SynchronizeMasterdata();
} catch (Exception) {
Toaster.ShowWarning(i18n.GetString("Warning.Masterdata.Title"), i18n.GetString("Warning.Masterdata.Msg"));

+ 3
- 0
Shared/Services/ILFBicycleRest.cs Bestand weergeven

@@ -1,10 +1,13 @@
using cwebplusApp.Shared.Models;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace cwebplusApp.Shared.Services {
public interface ILFBicycleRest {
void Initialize(IConfiguration configuration);
Task<List<ColorItem>> GetColors();
Task<List<BicycleType>> GetBicycleTypes();

+ 46
- 18
Shared/Services/LFBicycleRest.cs Bestand weergeven

@@ -1,5 +1,7 @@
using cwebplusApp.Shared.Models;
using Json.Net;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Globalization;
@@ -13,40 +15,66 @@ namespace cwebplusApp.Shared.Services {
private static readonly string VERSION = "v1";
private readonly HttpClient httpClient;
private HttpClient httpClient;
[Inject]
public IConfiguration Configuration { get; set; }
public LFBicycleRest() {
this.httpClient = new HttpClient { BaseAddress = new Uri("https://integrate.dynalias.net:9443/Fundvelo/") };
public void Initialize(IConfiguration configuration) {
this.Configuration = configuration;
string hostBaseUrl = Configuration.GetValue<string>("host_base_url");
Console.WriteLine("host_base_url: " + hostBaseUrl);
if (!String.IsNullOrEmpty(hostBaseUrl)) {
this.httpClient = new HttpClient { BaseAddress = new Uri(hostBaseUrl) };
}
}
public async Task<List<ColorItem>> GetColors() {
HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format("api/{0}/{1}/fundvelo/colors", VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
if (httpClient != null) {
string subResourceUrl = Configuration.GetValue<string>("subresource_url_colors");
if (!String.IsNullOrEmpty(subResourceUrl)) {
HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
ColorItem[] colors = JsonNet.Deserialize<ColorItem[]>(await httpResult.Content.ReadAsStringAsync());
return new List<ColorItem>(colors);
if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
ColorItem[] colors = JsonNet.Deserialize<ColorItem[]>(await httpResult.Content.ReadAsStringAsync());
return new List<ColorItem>(colors);
}
throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
}
}
throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
throw new HttpRequestException("HTTP client not initialized!");
}
public async Task<List<BicycleType>> GetBicycleTypes() {
HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format("api/{0}/{1}/fundvelo/types", VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
if (httpClient != null) {
string subResourceUrl = Configuration.GetValue<string>("subresource_url_types");
if (!String.IsNullOrEmpty(subResourceUrl)) {
HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
BicycleType[] bicycleTypes = JsonNet.Deserialize<BicycleType[]>(await httpResult.Content.ReadAsStringAsync());
return new List<BicycleType>(bicycleTypes);
if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
BicycleType[] bicycleTypes = JsonNet.Deserialize<BicycleType[]>(await httpResult.Content.ReadAsStringAsync());
return new List<BicycleType>(bicycleTypes);
}
throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
}
}
throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
throw new HttpRequestException("HTTP client not initialized!");
}
public async Task<List<Brand>> GetBrands() {
HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format("api/{0}/{1}/fundvelo/brands", VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
if (httpClient != null) {
string subResourceUrl = Configuration.GetValue<string>("subresource_url_brands");
if (!String.IsNullOrEmpty(subResourceUrl)) {
HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format(subResourceUrl, VERSION, CultureInfo.CurrentCulture.TwoLetterISOLanguageName));
if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
Brand[] brands = JsonNet.Deserialize<Brand[]>(await httpResult.Content.ReadAsStringAsync());
return new List<Brand>(brands);
if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
Brand[] brands = JsonNet.Deserialize<Brand[]>(await httpResult.Content.ReadAsStringAsync());
return new List<Brand>(brands);
}
throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
}
}
throw new HttpRequestException("HTTP error " + httpResult.StatusCode);
throw new HttpRequestException("HTTP client not initialized!");
}
}

+ 0
- 1
Shared/Services/MasterDataService.cs Bestand weergeven

@@ -48,7 +48,6 @@ namespace cwebplusApp.Shared.Services {
_brands = Defaults.GetBrandDefaults(_i18n).ToArray();
_colors = Defaults.GetColorDefaults(_i18n).ToArray();
_bicycleTypes = Defaults.GetBicycleTypeDefaults(_i18n).ToArray();
Console.WriteLine("MasterDataService constructor / colors: " + _colors.Length);
}
public async Task SynchronizeMasterdata() {

+ 1
- 0
cwebplusApp.csproj Bestand weergeven

@@ -21,6 +21,7 @@
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.7" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Localization" Version="5.0.7" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
<PackageReference Include="System.Net.Http.Json" Version="5.0.0" />
</ItemGroup>

+ 0
- 1
cwebplusApp.csproj.user Bestand weergeven

@@ -5,6 +5,5 @@
</PropertyGroup>
<PropertyGroup>
<ActiveDebugProfile>CaritasPWA</ActiveDebugProfile>
<NameOfLastUsedPublishProfile>D:\Work\Caritas\CaritasPWA\Properties\PublishProfiles\FolderProfile.pubxml</NameOfLastUsedPublishProfile>
</PropertyGroup>
</Project>

+ 6
- 0
wwwroot/appsettings.json Bestand weergeven

@@ -0,0 +1,6 @@
{
"host_base_url": "https://integrate.dynalias.net:9443/Fundvelo/",
"subresource_url_colors": "api/{0}/{1}/fundvelo/colors",
"subresource_url_brands": "api/{0}/{1}/fundvelo/brands",
"subresource_url_types=": "api/{0}/{1}/fundvelo/types"
}

+ 0
- 1
wwwroot/css/app.css Bestand weergeven

@@ -204,7 +204,6 @@ div.outlined .mdc-text-field.mdc-text-field--fullwidth .mdc-floating-label.mdc-f
.fv-osm-tile {
border: 1px solid rgb(0 0 0 /.38);
height: 100%;
border-radius: 20px;
}

+ 22
- 11
wwwroot/service-worker.js Bestand weergeven

@@ -3,6 +3,7 @@
// be reflected on the first load after each change).
const staticCacheName = 'site-static-v1'; // IMPORTANT: CHANGE THE VERSION IN THIS NAME EVERY TIME THE APP IS DEPLOYED ON SERVER WITH CHANGES!!!
const appsettings_url = 'appsettings.json';
const assets = [
'./',
'/index.html',
@@ -49,13 +50,14 @@ const assets = [
'https://fonts.gstatic.com/s/roboto/v27/KFOlCnqEu92Fr1MmEU9fBBc4AMP6lQ.woff2',
'https://fonts.gstatic.com/s/roboto/v27/KFOmCnqEu92Fr1Mu4mxKKTU1Kg.woff2',
'https://fonts.gstatic.com/s/materialicons/v85/flUhRq6tzZclQEJ-Vdg-IuiaDsNcIhQ8tQ.woff2',
'appsettings.json',
'manifest.json'
];
// install event
self.addEventListener('install', evt => {
evt.waitUntil(
self.addEventListener('install', event => {
event.waitUntil(
caches.open(staticCacheName).then(cache => {
console.log('caching app assets');
cache.addAll(assets);
@@ -80,12 +82,21 @@ self.addEventListener('activate', event => {
);
});
self.addEventListener('fetch', evt => {
evt.respondWith(
caches.match(evt.request).then(cacheRes => {
return cacheRes || fetch(evt.request);
})
);
});
// fetch events (appsettings are always first fetched from network)
self.addEventListener('fetch', event => {
if (event.request.url.endsWith(appsettings_url)) {
fetch(event.request).then(function (response) {
return caches.open(staticCacheName).then(function (cache) {
console.log('update cache');
cache.put(event.request, response.clone());
return response;
});
});
} else {
event.respondWith(
caches.match(event.request).then(cacheRes => {
return cacheRes || fetch(event.request);
})
);
}
});

Laden…
Annuleren
Opslaan