Skip to content

Commit

Permalink
Starting some API work
Browse files Browse the repository at this point in the history
  • Loading branch information
Programming-With-Chris committed Jul 22, 2022
1 parent 5768790 commit 6d35e7e
Show file tree
Hide file tree
Showing 15 changed files with 507 additions and 12 deletions.
1 change: 1 addition & 0 deletions ShoppingList/AppShell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public AppShell()
Routing.RegisterRoute(nameof(ItemDetail), typeof(ItemDetail));
Routing.RegisterRoute(nameof(MainPage), typeof(MainPage));
Routing.RegisterRoute(nameof(SettingsView), typeof(SettingsView));
Routing.RegisterRoute(nameof(StoreFinder), typeof(StoreFinder));
}
}
3 changes: 2 additions & 1 deletion ShoppingList/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public static MauiApp CreateMauiApp()
builder.Services.AddTransient<ItemDetail>();
builder.Services.AddSingleton<SettingsView>();
builder.Services.AddSingleton<SettingsViewModel>();

builder.Services.AddSingleton<StoreFinder>();
builder.Services.AddSingleton<StoreFinderViewModel>();



Expand Down
15 changes: 15 additions & 0 deletions ShoppingList/Model/Api/AccessTokenRes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShoppingList.Model.Api;

internal class AccessTokenRes
{
public int expires_in{ get; set; }
public string access_token { get; set; }
public string token_type { get; set; }

}
16 changes: 16 additions & 0 deletions ShoppingList/Model/Api/ApiConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShoppingList.Model.Api;

public class ApiConfig
{
public string ClientId { get; set; }
public string ClientSecret { get; set; }
public string RedirectUri { get; set; }
public string KrogerUrl { get; set; }

}
210 changes: 210 additions & 0 deletions ShoppingList/Model/Api/LocationsNearMeResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShoppingList.Model.Api;

public class LocationsNearMeResponse
{
Rootobject _ro;
}


public class Rootobject
{
public Datum[] data { get; set; }
public Meta meta { get; set; }
}

public class Meta
{
public Pagination pagination { get; set; }
}

public class Pagination
{
public int start { get; set; }
public int limit { get; set; }
public int total { get; set; }
}

public class Datum
{
public string locationId { get; set; }
public string chain { get; set; }
public Address address { get; set; }
public Geolocation geolocation { get; set; }
public string name { get; set; }
public Department[] departments { get; set; }
public Hours hours { get; set; }
public string phone { get; set; }
}

public class Address
{
public string addressLine1 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string zipCode { get; set; }
public string county { get; set; }
}

public class Geolocation
{
public float latitude { get; set; }
public float longitude { get; set; }
public string latLng { get; set; }
}

public class Hours
{
public string timezone { get; set; }
public string gmtOffset { get; set; }
public bool open24 { get; set; }
public Monday monday { get; set; }
public Tuesday tuesday { get; set; }
public Wednesday wednesday { get; set; }
public Thursday thursday { get; set; }
public Friday friday { get; set; }
public Saturday saturday { get; set; }
public Sunday sunday { get; set; }
}

public class Monday
{
public string open { get; set; }
public string close { get; set; }
public bool open24 { get; set; }
}

public class Tuesday
{
public string open { get; set; }
public string close { get; set; }
public bool open24 { get; set; }
}

public class Wednesday
{
public string open { get; set; }
public string close { get; set; }
public bool open24 { get; set; }
}

public class Thursday
{
public string open { get; set; }
public string close { get; set; }
public bool open24 { get; set; }
}

public class Friday
{
public string open { get; set; }
public string close { get; set; }
public bool open24 { get; set; }
}

public class Saturday
{
public string open { get; set; }
public string close { get; set; }
public bool open24 { get; set; }
}

public class Sunday
{
public string open { get; set; }
public string close { get; set; }
public bool open24 { get; set; }
}

public class Department
{
public string departmentId { get; set; }
public string name { get; set; }
public string phone { get; set; }
public Hours1 hours { get; set; }
public Address1 address { get; set; }
public Geolocation1 geolocation { get; set; }
public bool offsite { get; set; }
}

public class Hours1
{
public bool open24 { get; set; }
public Monday1 monday { get; set; }
public Tuesday1 tuesday { get; set; }
public Wednesday1 wednesday { get; set; }
public Thursday1 thursday { get; set; }
public Friday1 friday { get; set; }
public Saturday1 saturday { get; set; }
public Sunday1 sunday { get; set; }
}

public class Monday1
{
public string open { get; set; }
public string close { get; set; }
public bool open24 { get; set; }
}

public class Tuesday1
{
public string open { get; set; }
public string close { get; set; }
public bool open24 { get; set; }
}

public class Wednesday1
{
public string open { get; set; }
public string close { get; set; }
public bool open24 { get; set; }
}

public class Thursday1
{
public string open { get; set; }
public string close { get; set; }
public bool open24 { get; set; }
}

public class Friday1
{
public string open { get; set; }
public string close { get; set; }
public bool open24 { get; set; }
}

public class Saturday1
{
public string open { get; set; }
public string close { get; set; }
public bool open24 { get; set; }
}

public class Sunday1
{
public string open { get; set; }
public string close { get; set; }
public bool open24 { get; set; }
}

public class Address1
{
public string addressLine1 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string zipCode { get; set; }
}

public class Geolocation1
{
public float latitude { get; set; }
public float longitude { get; set; }
public string latLng { get; set; }
}

85 changes: 85 additions & 0 deletions ShoppingList/Services/KrogerAPIService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System.Text.Json;
using ShoppingList.Model.Api;

namespace ShoppingList.Services;

public class KrogerAPIService
{
HttpClient _client;
List<string> config;
AccessTokenRes accessToken;

public KrogerAPIService()
{
_client = new HttpClient();

}

public async Task<Dictionary<string, string>> GetLocationNearZip(string zip, ApiConfig apiConfig)
{
Dictionary<string, string> closeKrogerNames = new Dictionary<string, string>();

string zipQuery = $"?filter.zipCode.near={zip}";
Uri uri = new($"{apiConfig.KrogerUrl}locations{zipQuery}");

_client.DefaultRequestHeaders.Authorization = new("Bearer", accessToken.access_token);

HttpResponseMessage res = await _client.GetAsync(uri);

if (res.IsSuccessStatusCode)
{
string content = await res.Content.ReadAsStringAsync();
var jsonRes = JsonSerializer.Deserialize<Rootobject>(content);

foreach (var store in jsonRes.data)
{
if (store.chain.Equals("KROGER"))
{
closeKrogerNames.Add(store.locationId, store.name);
}
}
return closeKrogerNames;
}
else
{
throw new Exception("No Krogers found within a 10 mile radius");
}
}


public async Task<ApiConfig> GetStartupConfig()
{
using var stream = await FileSystem.OpenAppPackageFileAsync("krogerapiconfig.json");
using var reader = new StreamReader(stream);
var json = await reader.ReadToEndAsync();
var output = JsonSerializer.Deserialize<ApiConfig>(json);
return output;
}


public async Task<bool> SetAuthTokens(ApiConfig apiConfig)
{
var authData = $"{apiConfig.ClientId}:{apiConfig.ClientSecret}";
var authHeaderValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(authData));

_client.DefaultRequestHeaders.Authorization = new("Basic", authHeaderValue);


StringContent content = new("grant_type=client_credentials", Encoding.ASCII, "application/x-www-form-urlencoded");

HttpResponseMessage res = await _client.PostAsync(apiConfig.KrogerUrl + "connect/oauth2/token", content);

if (res.IsSuccessStatusCode)
{
var resCont = await res.Content.ReadAsStringAsync();
accessToken = JsonSerializer.Deserialize<AccessTokenRes>(resCont);

return true;
} else
{
throw new Exception("Unable to Authenticate, Sorting Functionality Offline");
}

}

}
18 changes: 15 additions & 3 deletions ShoppingList/ShoppingList.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net6.0-windows10.0.19041.0</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net6.0-tizen</TargetFrameworks> -->
<OutputType>Exe</OutputType>
<RootNamespace>ShoppingList</RootNamespace>

<OutputType>Exe</OutputType>

<RootNamespace>ShoppingList</RootNamespace>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
Expand All @@ -26,7 +28,7 @@

<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.2</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">14.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramlework)')) == 'android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
Expand Down Expand Up @@ -71,6 +73,9 @@
</ItemGroup>

<ItemGroup>
<Compile Update="View\StoreFinder.xaml.cs">
<DependentUpon>StoreFinder.xaml</DependentUpon>
</Compile>
<Compile Update="View\ItemDetail.xaml.cs">
<DependentUpon>ItemDetail.xaml</DependentUpon>
</Compile>
Expand All @@ -89,6 +94,9 @@
</ItemGroup>

<ItemGroup>
<MauiXaml Update="View\StoreFinder.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
<MauiXaml Update="View\ItemDetail.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
Expand All @@ -106,4 +114,8 @@
</MauiXaml>
</ItemGroup>

<ItemGroup>
<Folder Include="Resources\APIs\" />
</ItemGroup>

</Project>
Loading

0 comments on commit 6d35e7e

Please sign in to comment.