-
Notifications
You must be signed in to change notification settings - Fork 0
/
GpxUpload.cs
58 lines (45 loc) · 1.89 KB
/
GpxUpload.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
// using System.Diagnostics;
using System.IO;
// using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
public class GpxUpload
{
static HttpClient client = new HttpClient();
public static string OsmAuth() {
string osmPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".rtg", "osm.txt");
if (File.Exists(osmPath)) return File.ReadAllText(osmPath);
Console.WriteLine("Type OSM username:password");
var userpass = Console.ReadLine() ?? throw new ArgumentNullException();
File.WriteAllText(osmPath, userpass);
return userpass;
}
public static async Task<int> Run(string path, DateTime start)
{
// https://wiki.openstreetmap.org/wiki/API_v0.6#Create:_POST_.2Fapi.2F0.6.2Fgpx.2Fcreate
using var multiContent = new MultipartFormDataContent();
using var gxp = File.OpenRead(path);
using var file = new StreamContent(gxp);
multiContent.Add(file, "file", Path.GetFileName(path));
using var description = new StringContent($"Run {start:yyyy-MM-dd} looking for paths");
multiContent.Add(description, "description");
using var tags = new StringContent("");
multiContent.Add(tags, "tags");
using var visibility = new StringContent("trackable");
multiContent.Add(visibility, "visibility");
using var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
Content = multiContent,
RequestUri = new Uri("https://api.openstreetmap.org/api/0.6/gpx/create"),
};
string base64 = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(OsmAuth()));
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", base64);
using var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
var id = await response.Content.ReadAsStringAsync();
return int.Parse(id);
}
}