forked from Programming-With-Chris/ShoppingList
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5768790
commit 6d35e7e
Showing
15 changed files
with
507 additions
and
12 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
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
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,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; } | ||
|
||
} |
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,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; } | ||
|
||
} |
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,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; } | ||
} | ||
|
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,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"); | ||
} | ||
|
||
} | ||
|
||
} |
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
Oops, something went wrong.