Skip to content

Commit 59ed945

Browse files
refactor code and update README
1 parent 65f7882 commit 59ed945

File tree

3 files changed

+31
-52
lines changed

3 files changed

+31
-52
lines changed

APIHandler.cs

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,77 +6,55 @@ namespace AoC.API;
66
public class Session
77
{
88
private const string DOMAIN = "https://adventofcode.com";
9-
private readonly string _sessionCookie;
10-
private readonly int _year;
11-
private readonly int _day;
9+
10+
public string SessionCookie { get; private set; }
11+
public int Year { get; private set; }
12+
public int Day { get; private set; }
1213

1314
public Session(string sessionCookie, int year, int day)
1415
{
15-
_sessionCookie = sessionCookie;
16-
_year = year;
17-
_day = day;
16+
SessionCookie = sessionCookie;
17+
Year = year;
18+
Day = day;
1819
}
1920

2021
public Session(string sessionCookie, string input, Regex pattern)
2122
{
22-
_sessionCookie = sessionCookie;
23+
SessionCookie = sessionCookie;
2324

2425
var match = pattern.Match(input);
2526
if (match.Success)
2627
{
27-
_year = int.Parse(match.Groups["year"].Value);
28-
_day = int.Parse(match.Groups["day"].Value);
28+
Year = int.Parse(match.Groups["year"].Value);
29+
Day = int.Parse(match.Groups["day"].Value);
2930
}
3031
else { throw new Exception("no regex match found"); }
3132
}
3233

33-
private async Task<string> Get(string uri)
34-
{
35-
var client = new HttpClient();
36-
var request = new HttpRequestMessage
37-
{
38-
Method = HttpMethod.Get,
39-
RequestUri = new Uri(uri),
40-
Headers =
41-
{
42-
{ "Cookie", $"session={_sessionCookie}" },
43-
}
44-
};
45-
46-
using (var response = await client.SendAsync(request))
47-
{
48-
response.EnsureSuccessStatusCode();
49-
return await response.Content.ReadAsStringAsync();
50-
}
51-
}
52-
private async Task<string> Post(string uri, HttpContent? content = null)
34+
private async Task<string> SendRequest(HttpMethod method, string uri, HttpContent? content = null)
5335
{
5436
var client = new HttpClient();
5537
var request = new HttpRequestMessage
5638
{
57-
Method = HttpMethod.Post,
39+
Method = method,
5840
RequestUri = new Uri(uri),
59-
Headers =
60-
{
61-
{ "Cookie", $"session={_sessionCookie}" },
62-
}
41+
Headers = { { "Cookie", $"session={SessionCookie}" } }
6342
};
64-
6543
if (content != null) { request.Content = content; }
6644

67-
6845
using (var response = await client.SendAsync(request))
6946
{
7047
response.EnsureSuccessStatusCode();
7148
return await response.Content.ReadAsStringAsync();
7249
}
7350
}
7451

75-
public async Task<string> GetInputText() => (await Get($"{DOMAIN}/{_year}/day/{_day}/input")).TrimEnd('\n');
52+
public async Task<string> GetInputText() => (await SendRequest(HttpMethod.Get, $"{DOMAIN}/{Year}/day/{Day}/input")).TrimEnd('\n');
7653
public async Task<string[]> GetInputLines() => (await GetInputText()).Split('\n');
54+
7755
public async Task<Dictionary<int, int>> GetAllStars()
7856
{
79-
var response = (await Get($"{DOMAIN}/events"))
57+
var response = (await SendRequest(HttpMethod.Get, $"{DOMAIN}/events"))
8058
.Split('\n')
8159
.Where(line => line.StartsWith("<div class=\"eventlist-event\">"))
8260
.ToArray();
@@ -92,18 +70,15 @@ public async Task<Dictionary<int, int>> GetAllStars()
9270
return new { Year = year, Stars = stars };
9371
}).ToDictionary(x => x.Year, x => x.Stars);
9472
}
95-
public async Task<int> GetThisYearStars() => (await GetAllStars())[_year];
73+
9674
public async Task<bool> SubmitAnswer(int level, object answer)
9775
{
9876
var content = new StringContent($"level={level}&answer={answer}")
9977
{
100-
Headers =
101-
{
102-
ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded")
103-
}
78+
Headers = { ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded") }
10479
};
10580

106-
var response = await Post($"{DOMAIN}/{_year}/day/{_day}/answer", content);
81+
var response = await SendRequest(HttpMethod.Post, $"{DOMAIN}/{Year}/day/{Day}/answer", content);
10782
return response.Contains("That's the right answer!");
10883
}
10984
}

AoC.API.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
<Description>Handle personal AoC data directly from your .NET project</Description>
1010
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1111
<PackageTags>AoCAPI, Advent-of-Code-API</PackageTags>
12-
<RepositoryUrl>https://github.com/hckmtrx/AoC.API</RepositoryUrl>
12+
<RepositoryUrl>https://github.com/antonio-subasic/AoC.API</RepositoryUrl>
1313
<PackageReadmeFile>README.md</PackageReadmeFile>
14-
<PackageVersion>2.0.2</PackageVersion>
14+
<PackageVersion>2.0.3</PackageVersion>
1515
</PropertyGroup>
1616

1717
<ItemGroup>

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
A simple [NuGet](https://nuget.org) package to handle personal [AoC](https://adventofcode.com) data directly from your .NET project
55

66
## Documentation
7+
78
- [Add to your project](#add-to-your-project)
89
- [Initialization](#initialization)
910
- [Features](#features)
@@ -14,6 +15,7 @@ A simple [NuGet](https://nuget.org) package to handle personal [AoC](https://adv
1415
<br><br>
1516

1617
# Add to your project
18+
1719
```bash
1820
dotnet add package AoCAPI
1921
```
@@ -25,12 +27,15 @@ using AoC.API;
2527
<br>
2628

2729
# Initialization
30+
2831
```csharp
2932
var client = new Session("session cookie", int year, int day);
3033
```
34+
3135
```csharp
3236
var client = new Session("session cookie", string input, Regex pattern);
3337
```
38+
3439
> <picture>
3540
> <source media="(prefers-color-scheme: dark)" srcset="https://github.com/Mqxx/GitHub-Markdown/blob/main/blockquotes/badge/dark-theme/info.svg">
3641
> <img alt="Info" src="https://github.com/Mqxx/GitHub-Markdown/blob/main/blockquotes/badge/dark-theme/Info">
@@ -46,6 +51,7 @@ var client = new Session("session cookie", string input, Regex pattern);
4651
# Features
4752

4853
## Get input file
54+
4955
```csharp
5056
string inputText = client.GetInputText(); // input file (raw text)
5157
string inputLines = client.GetInputLines(); // input file (lines)
@@ -54,21 +60,19 @@ string inputLines = client.GetInputLines(); // input file (lines)
5460
<br>
5561

5662
## Get achieved stars
63+
5764
```csharp
58-
Dictionary<int, int> allStars = client.GetAllStars(); // all user's achieved stars
59-
int thisYearStars = client.GetThisYearStars(); // user's achieved stars from initialized year
65+
Dictionary<int, int> allStars = client.GetAllStars(); // all user's achieved stars (key = year, value = stars)
6066
```
6167

6268
<br>
6369

6470
## Submit answer
71+
6572
```csharp
6673
bool succeeded = client.SubmitAnswer(int level, object answer); // submits answer to initialized year and day, returns true if answer is correct
6774
```
6875

6976
<br><br>
7077

71-
> credits to:<br>
72-
> [Max](https://github.com/Mqxx) - markdown info icons<br>
73-
> [Monday Morning Haskell](https://mmhaskell.com/) - documentation on how to obtaining session cookie<br>
74-
> [Developer.Mozilla](https://developer.mozilla.org) - documentation on how to name Regex groups
78+
> credits to:<br> > [Max](https://github.com/Mqxx) - markdown info icons<br> > [Monday Morning Haskell](https://mmhaskell.com/) - documentation on how to obtaining session cookie<br> > [Developer.Mozilla](https://developer.mozilla.org) - documentation on how to name Regex groups

0 commit comments

Comments
 (0)