Skip to content

Commit

Permalink
Merge pull request #16 from CaptnCodr/feature/weblink
Browse files Browse the repository at this point in the history
Get weblink from shipment
  • Loading branch information
CaptnCodr authored Jan 17, 2024
2 parents 1ba877f + bdf34df commit 4afc10a
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 41 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,7 @@ MigrationBackup/

# Ionide (cross platform F# VS Code tools) working folder
.ionide/
.fake
.fake

# Rider's working folder.
.idea/
2 changes: 2 additions & 0 deletions Arguments.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Arguments =
| [<CliPrefix(CliPrefix.None); AltCommandLine("-n")>] Number of ParseResults<TrackingNumbersArgs>
| [<CliPrefix(CliPrefix.None); AltCommandLine("-d")>] Detail of number: string
| [<CliPrefix(CliPrefix.None); AltCommandLine("-p")>] Package of number: string
| [<CliPrefix(CliPrefix.None); AltCommandLine("-w")>] Weblink of number: string
| [<CliPrefix(CliPrefix.None); AltCommandLine("-u")>] Update

| [<CliPrefix(CliPrefix.None); AltCommandLine("-s")>] SetKey of key: string
Expand All @@ -33,6 +34,7 @@ module Arguments =
| Number _ -> Arguments_Number.ResourceString
| Detail _ -> Arguments_Detail.ResourceString
| Package _ -> Arguments_Package.ResourceString
| Weblink _ -> Arguments_Weblink.ResourceString
| Update -> Arguments_Update.ResourceString
| SetKey _ -> Arguments_SetKey.ResourceString
| GetKey -> Arguments_GetKey.ResourceString
Expand Down
49 changes: 19 additions & 30 deletions Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ module Program =
let printTrackingNumberLines elements =
elements |> Seq.map buildColoredLine |> String.concat Environment.NewLine

let matchTrackingNumberOrIndex (value: string) (execFunc: TrackingNumber -> string) =
match value with
| l when l.Length < 7 ->

match Int32.TryParse(l) with
| true, i ->
Repository.loadTrackingNumbers ()
|> fun n -> n |> Seq.toArray |> Array.tryItem i
|> function
| Some v -> v |> execFunc
| None -> NoTrackingNumber.ResourceString
| _, _ -> IndexNotParsable.ResourceString

| _ -> TrackingNumber(value) |> execFunc

let runCommands (parser: ArgumentParser<CliArguments>) (args: string array) =
match (parser.Parse args).GetAllResults() with
| [ Number n ] ->
Expand All @@ -36,40 +51,14 @@ module Program =

| _ -> parser.PrintUsage()

| [ Detail d ] ->

match d with
| l when l.Length < 7 ->

match Int32.TryParse(l) with
| true, i ->
Repository.loadTrackingNumbers ()
|> fun n -> n |> Seq.toArray |> Array.tryItem i
|> function
| Some v -> v |> loadTrackingNumberDetail |> printTrackingNumberLines
| None -> NoTrackingNumber.ResourceString
| (_, _) -> IndexNotParsable.ResourceString

| _ -> TrackingNumber(d) |> loadTrackingNumberDetail |> printTrackingNumberLines

| [ Package p ] ->

match p with
| l when l.Length < 7 ->
| [ Detail d ] -> matchTrackingNumberOrIndex d (loadTrackingNumberDetail >> printTrackingNumberLines)

match Int32.TryParse(l) with
| true, i ->
Repository.loadTrackingNumbers ()
|> fun n -> n |> Seq.toArray |> Array.tryItem i
|> function
| Some v -> v |> loadTrackingNumberPackageDetails
| None -> NoTrackingNumber.ResourceString
| (_, _) -> IndexNotParsable.ResourceString
| [ Package p ] -> matchTrackingNumberOrIndex p loadTrackingNumberPackageDetails

| _ -> TrackingNumber(p) |> loadTrackingNumberPackageDetails
| [ Weblink w ] -> matchTrackingNumberOrIndex w getWeblink

| [ Update ] ->
let countTransits (elements) : int =
let countTransits elements : int =
elements
|> Seq.filter (fun (x, _) -> x = "pre-transit" || x = "transit")
|> Seq.length
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ Set application's path in system's environment "Path" variables.
[ package | -p ]
+--[ number ]
[ weblink | -w ]
+--[ number ]
[ update | -u ]
[ setkey | -s ]
Expand Down
1 change: 1 addition & 0 deletions Resources/Resources.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Resources =
| Arguments_Number
| Arguments_Detail
| Arguments_Package
| Arguments_Weblink
| Arguments_Update
| Arguments_SetKey
| Arguments_GetKey
Expand Down
3 changes: 3 additions & 0 deletions Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
<data name="Arguments_Update" xml:space="preserve">
<value>Updates all tracking events.</value>
</data>
<data name="Arguments_Weblink" xml:space="preserve">
<value>Gets the weblink to the given tracking number.</value>
</data>
<data name="Arguments_SetKey" xml:space="preserve">
<value>Sets the API key.</value>
</data>
Expand Down
26 changes: 17 additions & 9 deletions ShipmentHandler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ module ShipmentHandler =
|> fun a -> new HttpClient(a, BaseAddress = System.Uri(ApiAddress))
|> DhlSchema.Client

let private checkShipmentDate (timstamp: string) : bool =
(timstamp |> System.DateTime.Parse |> System.DateTime.Now.Subtract |> _.Days) > DaysAfterDelivery
let private checkShipmentDate (timestamp: string) : bool =
(timestamp |> System.DateTime.Parse |> System.DateTime.Now.Subtract |> _.Days) > DaysAfterDelivery

let printShipmentLine (idx: int) (number: string) (shipment: DhlSchema.supermodelIoLogisticsTrackingShipment) =
if
Expand All @@ -46,7 +46,7 @@ module ShipmentHandler =
$"[{idx}] {shipment.Id} @ ({System.DateTime.Parse(shipment.Status.Timestamp.ToString())}): {shipmentLine}")

let printShipmentProblem (exceptionMessage: string) =
let (number, json) =
let number, json =
exceptionMessage
|> fun s -> let i = s.IndexOf(",") in (s.Substring(0, i), s.Substring(i + 1))

Expand All @@ -62,7 +62,7 @@ module ShipmentHandler =
let rec fetchTrackingNumber (retries: int) (idx: int) (TrackingNumber(number)) =
task {
try
let! shipments = getShipments (number)
let! shipments = getShipments number
return shipments |> Seq.map (printShipmentLine idx number)
with ex ->
if retries = 0 then
Expand All @@ -87,7 +87,7 @@ module ShipmentHandler =

let loadTrackingNumberDetail (TrackingNumber(number)) =
task {
let! shipments = getShipments (number)
let! shipments = getShipments number
return shipments |> Seq.head |> (fun s -> s.Events |> Seq.map printShipmentEvent)
}
|> Async.AwaitTask
Expand All @@ -107,15 +107,23 @@ module ShipmentHandler =
let dimensions =
match details.Dimensions with
| null -> ""
| _ as d ->
$"\nWidth: {getDimension (d.Width)}\nHeight: {getDimension (d.Height)}\nLength: {getDimension (d.Length)}"
| d ->
$"\nWidth: {getDimension d.Width}\nHeight: {getDimension d.Height}\nLength: {getDimension d.Length}"

$"Weight: {getDimension (details.Weight)}{dimensions}"
$"Weight: {getDimension details.Weight}{dimensions}"

let loadTrackingNumberPackageDetails (TrackingNumber(number)) =
task {
let! shipments = getShipments (number)
let! shipments = getShipments number
return shipments |> Seq.head |> (fun s -> s.Details |> printPackageDetails)
}
|> Async.AwaitTask
|> Async.RunSynchronously

let getWeblink (TrackingNumber(number)) =
task {
let! shipments = getShipments number
return shipments |> Seq.head |> _.ServiceUrl |> string

Check warning on line 126 in ShipmentHandler.fs

View workflow job for this annotation

GitHub Actions / build

The meaning of _ is ambiguous here. It cannot be used for a discarded variable and a function shorthand in the same scope.

Check warning on line 126 in ShipmentHandler.fs

View workflow job for this annotation

GitHub Actions / build

The meaning of _ is ambiguous here. It cannot be used for a discarded variable and a function shorthand in the same scope.
}
|> Async.AwaitTask
|> Async.RunSynchronously
2 changes: 1 addition & 1 deletion dhl-cli.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PublishSingleFile>true</PublishSingleFile>
<PublishTrimmed>true</PublishTrimmed>
<ProduceReferenceAssembly>false</ProduceReferenceAssembly>
<Version>0.12.1.0</Version>
<Version>0.13.0.0</Version>
<JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault>
</PropertyGroup>
<ItemGroup>
Expand Down

0 comments on commit 4afc10a

Please sign in to comment.