Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding temporary workaround for user agent issue #668
- Loading branch information
1 parent
5340359
commit 6954069
Showing
3 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) BruTile developers team. All rights reserved. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Net.Http; | ||
using BruTile; | ||
using BruTile.Cache; | ||
using BruTile.Web; | ||
|
||
namespace Mapsui.Utilities | ||
{ | ||
[Obsolete("workaround for user agent issue")] | ||
public class CustomHttpTileProvider : ITileProvider, IRequest | ||
{ | ||
private readonly Func<Uri, byte[]> _fetchTile; | ||
private readonly IRequest _request; | ||
private readonly HttpClient _httpClient = new HttpClient(); | ||
|
||
public CustomHttpTileProvider(IRequest request = null, IPersistentCache<byte[]> persistentCache = null, | ||
Func<Uri, byte[]> fetchTile = null) | ||
{ | ||
_request = request; | ||
PersistentCache = persistentCache ?? new NullCache(); | ||
_fetchTile = fetchTile ?? FetchTile; | ||
_httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mapsui"); | ||
} | ||
|
||
private byte[] FetchTile(Uri arg) | ||
{ | ||
return _httpClient.GetByteArrayAsync(arg).ConfigureAwait(false).GetAwaiter().GetResult(); | ||
} | ||
|
||
public IPersistentCache<byte[]> PersistentCache { get; } | ||
|
||
public Uri GetUri(TileInfo tileInfo) | ||
{ | ||
return _request.GetUri(tileInfo); | ||
} | ||
|
||
public byte[] GetTile(TileInfo tileInfo) | ||
{ | ||
var bytes = PersistentCache.Find(tileInfo.Index); | ||
if (bytes != null) return bytes; | ||
bytes = _fetchTile(_request.GetUri(tileInfo)); | ||
if (bytes != null) PersistentCache.Add(tileInfo.Index, bytes); | ||
return bytes; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) BruTile developers team. All rights reserved. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using BruTile; | ||
using BruTile.Cache; | ||
using BruTile.Web; | ||
|
||
namespace Mapsui.Utilities | ||
{ | ||
[Obsolete("workaround for user agent issue")] | ||
public class CustomHttpTileSource : ITileSource, IRequest | ||
{ | ||
private readonly CustomHttpTileProvider _provider; | ||
|
||
public CustomHttpTileSource(ITileSchema tileSchema, string urlFormatter, IEnumerable<string> serverNodes = null, | ||
string apiKey = null, string name = null, IPersistentCache<byte[]> persistentCache = null, | ||
Func<Uri, byte[]> tileFetcher = null, Attribution attribution = null) | ||
: this(tileSchema, new BasicRequest(urlFormatter, serverNodes, apiKey), name, persistentCache, tileFetcher, attribution) | ||
{ | ||
} | ||
|
||
public CustomHttpTileSource(ITileSchema tileSchema, IRequest request, string name = null, | ||
IPersistentCache<byte[]> persistentCache = null, Func<Uri, byte[]> tileFetcher = null, Attribution attibution = null) | ||
{ | ||
_provider = new CustomHttpTileProvider(request, persistentCache, tileFetcher); | ||
Schema = tileSchema; | ||
Name = name ?? string.Empty; | ||
Attribution = attibution ?? new Attribution(); | ||
} | ||
|
||
public IPersistentCache<byte[]> PersistentCache => _provider.PersistentCache; | ||
|
||
public Uri GetUri(TileInfo tileInfo) | ||
{ | ||
return _provider.GetUri(tileInfo); | ||
} | ||
|
||
public ITileSchema Schema { get; } | ||
|
||
public string Name { get; set; } | ||
|
||
public Attribution Attribution { get; set; } | ||
|
||
/// <summary> | ||
/// Gets the actual image content of the tile as byte array | ||
/// </summary> | ||
public virtual byte[] GetTile(TileInfo tileInfo) | ||
{ | ||
return _provider.GetTile(tileInfo); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters