Skip to content

Commit f1dafbc

Browse files
authored
feat: relax metadata type for JSON 1.0 (#13)
1 parent fbc507d commit f1dafbc

10 files changed

Lines changed: 47 additions & 57 deletions

File tree

.github/workflows/CI.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ jobs:
2323
fail-fast: false
2424
matrix:
2525
version:
26-
- '1.10'
27-
- '1.11'
26+
- '1'
2827
- 'pre'
2928
os:
3029
- ubuntu-latest

.github/workflows/TagBot.yml

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,8 @@ on:
66
workflow_dispatch:
77
inputs:
88
lookback:
9+
description: "Number of days to look back for new releases"
910
default: "3"
10-
permissions:
11-
actions: read
12-
checks: read
13-
contents: write
14-
deployments: read
15-
issues: read
16-
discussions: read
17-
packages: read
18-
pages: read
19-
pull-requests: read
20-
repository-projects: read
21-
security-events: read
22-
statuses: read
2311
jobs:
2412
TagBot:
2513
if: github.event_name == 'workflow_dispatch' || github.actor == 'JuliaTagBot'

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ CSV = "0.10"
2323
Dates = "1"
2424
DimensionalData = "0.29"
2525
HTTP = "1"
26-
JSON = "0.21"
26+
JSON = "0.21, 1"
2727
SpaceDataModel = "0.1.7, 0.2"
2828
Tables = "1"
29-
Unitful = "1.22.0"
29+
Unitful = "1"
3030
julia = "1.10"

src/HAPIClient.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module HAPIClient
22

33
using HTTP
4-
using JSON
4+
using HTTP: request_uri, URI
5+
import JSON
56
using Dates
6-
using CSV
7+
import CSV
78
using Tables
89
using Unitful
910
using SpaceDataModel: AbstractDataVariable, parse_datetime
@@ -14,6 +15,8 @@ import SpaceDataModel: times
1415
export hapi, get_data, meta, times
1516
export HAPIVariable, HAPIVariables
1617

18+
json_parse(x) = JSON.parse(String(x))
19+
1720
include("server.jl")
1821
include("metadata.jl")
1922
include("data.jl")

src/data.jl

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,23 @@ function get_data(server, dataset, parameters, tmin, tmax; format = format(serve
2222
"time.max" => tmax,
2323
"format" => format
2424
)
25-
uri = HTTP.request_uri(url, query)
25+
uri = request_uri(url, query)
2626
verbose > 0 && @info "Getting data from $uri"
2727
response = HTTP.get(uri; verbose, kw...)
2828

2929
data = if format == "csv"
3030
CSV.File(response.body; header = false, dateformat = DEFAULT_DATE_FORMAT)
3131
elseif format == "json"
32-
JSON.parse(String(response.body))
32+
json_parse(response.body)
3333
elseif format == "binary"
3434
error("Binary format not yet implemented")
3535
else
3636
throw("Unsupported format: $format")
3737
end
3838
meta = get_parameters(server, dataset, parameters)
39-
meta["uri"] = uri
4039
params = meta["parameters"]
41-
n = length(params) - 1
42-
verbose > 0 && @info "Got $n parameters"
43-
return HAPIVariables(data, params, meta, server, dataset)
44-
end
45-
46-
_merge_meta!(x, meta) = begin
47-
merge!(x.meta, meta)
48-
delete!(x.meta, "parameters")
49-
return x
40+
verbose > 0 && @info "Got $(length(params) - 1) parameters"
41+
return HAPIVariables(data, params, meta, server, dataset, uri)
5042
end
5143

5244
"""

src/metadata.jl

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function check_status_code(i)
2222
end
2323
end
2424

25-
check_status_code(response::Dict) = check_status_code(response["status"]["code"])
25+
check_status_code(response::AbstractDict) = check_status_code(response["status"]["code"])
2626

2727
"""
2828
get_catalog(server)
@@ -33,34 +33,33 @@ HAPI info response JSON structure: https://github.com/hapi-server/data-specifica
3333
"""
3434
function get_catalog(server)
3535
response = HTTP.get(url(server, "catalog"))
36-
response_dict = JSON.parse(String(response.body))
36+
response_dict = json_parse(response.body)
3737
if check_status_code(response_dict)
3838
return response_dict["catalog"]
3939
end
4040
end
4141

4242
"""
43-
get_parameters(server, dataset)
43+
get_parameters(server, id)
4444
45-
Get a dictionary containing the HAPI info metadata for all parameters in the `dataset`.
45+
Get a dictionary containing the HAPI info metadata for all parameters in the `id` dataset.
4646
Returns a tuple of (info, parameters) where info is the full response and parameters is
4747
a Vector of Parameter objects.
4848
4949
HAPI info response JSON structure: https://github.com/hapi-server/data-specification/blob/master/hapi-dev/HAPI-data-access-spec-dev.md#36-info
5050
"""
51-
function get_parameters(server, dataset)
52-
query = Dict("id" => dataset)
53-
response = HTTP.get(url(server, "info"); query)
54-
return JSON.parse(String(response.body))
51+
function get_parameters(server, id)
52+
response = HTTP.get(url(server, "info"); query = (; id))
53+
return json_parse(response.body)
5554
end
5655

5756
"""
58-
get_parameters(server, dataset, parameters)
57+
get_parameters(server, id, parameters)
5958
6059
Get a dictionary containing the HAPI info metadata for each parameter in the comma-separated string `parameters`.
6160
"""
62-
function get_parameters(server, dataset, parameters)
63-
query = Dict("id" => dataset, "parameters" => parameters)
61+
function get_parameters(server, id, parameters)
62+
query = (; id, parameters)
6463
response = HTTP.get(url(server, "info"); query)
65-
return JSON.parse(String(response.body))
64+
return json_parse(response.body)
6665
end

src/server.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Get server capabilities.
3939
"""
4040
function get_capabilities(server)
4141
response = HTTP.get(url(server, "capabilities"))
42-
return JSON.parse(String(response.body))
42+
return json_parse(response.body)
4343
end
4444

4545
"""
@@ -51,7 +51,7 @@ function load_servers_from_json(; url=DEFAULT_SERVERS_JSON_URL, register=false)
5151
# Fetch the JSON data: try to load from URL first, fall back to file if HTTP fails
5252
servers_data = try
5353
response = HTTP.get(url)
54-
JSON.parse(String(response.body))
54+
json_parse(response.body)
5555
catch
5656
@warn "HTTP request failed, falling back to local file"
5757
JSON.parsefile(joinpath(@__DIR__, "abouts.json"))

src/specs/parameter.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct Parameter
3939
vectorComponents::Union{String,Vector{String},Nothing}
4040
bins::Union{Vector{Dict{String,Any}},Nothing}
4141

42-
function Parameter(dict::Dict{String,Any})
42+
function Parameter(dict::AbstractDict)
4343
# Validate required fields
4444
name = get(dict, "name") do
4545
throw(ArgumentError("Parameter missing required field 'name'"))
@@ -101,7 +101,7 @@ struct Parameter
101101
end
102102

103103
# Constructor from JSON-like dictionary
104-
Base.convert(::Type{Parameter}, dict::Dict{String,Any}) = Parameter(dict)
104+
Base.convert(::Type{Parameter}, dict::AbstractDict{String,Any}) = Parameter(dict)
105105

106106
"""
107107
is_time_parameter(param::Parameter)

src/types.jl

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
"""
32
An array-like object that represents a HAPI variable.
43
@@ -8,27 +7,35 @@ An array-like object that represents a HAPI variable.
87
- `time`: The time axis (use `times(x)` to access).
98
- `meta`: The metadata (use `meta(x)` to access).
109
"""
11-
struct HAPIVariable{T, N, A <: AbstractArray{T, N}, Tt <: AbstractVector} <: AbstractDataVariable{T, N}
10+
struct HAPIVariable{T, N, A <: AbstractArray{T, N}, Tt <: AbstractVector, M <: AbstractDict} <: AbstractDataVariable{T, N}
1211
data::A
1312
time::Tt
14-
meta::Dict
13+
meta::M
1514
end
1615

1716
"""
1817
A thin wrapper over NamedTuple for HAPI variables that shares the same time axis.
1918
"""
20-
struct HAPIVariables{NT <: NamedTuple, D <: Dict, S, DS}
19+
struct HAPIVariables{NT <: NamedTuple, D <: AbstractDict, S, DS}
2120
nt::NT
2221
meta::D
2322
server::S
2423
dataset::DS
24+
uri::URI
2525
end
2626

2727
@inline Base.parent(x::HAPIVariables) = getfield(x, :nt)
2828
times(x::HAPIVariables) = times(first(parent(x)))
2929
server(x::HAPIVariables) = getfield(x, :server)
3030
dataset(x::HAPIVariables) = getfield(x, :dataset)
31-
id(x::HAPIVariables) = id(server(x)) * "/" * dataset(x)
31+
function id(x::HAPIVariables)
32+
s = server(x)
33+
if s isa Server
34+
return id(s) * "/" * dataset(x)
35+
else
36+
return s * "/info?id=" * dataset(x)
37+
end
38+
end
3239
Base.propertynames(x::HAPIVariables) = propertynames(parent(x))
3340
Base.getproperty(x::HAPIVariables, s::Symbol) = getproperty(parent(x), s)
3441
Base.length(x::HAPIVariables) = length(parent(x))
@@ -38,12 +45,13 @@ Base.getindex(x::HAPIVariables, i) = getindex(parent(x), i)
3845
Base.show(io::IO, x::HAPIVariables) = show(io, parent(x))
3946
function Base.show(io::IO, m::MIME"text/plain", var::HAPIVariables)
4047
print(io, "HAPIVariables: ")
41-
printstyled(io, id(var), color=209)
48+
printstyled(io, id(var), color = 209)
49+
print(io, " (", getfield(var, :uri), ")")
4250
foreach(var) do v
4351
print(io, "\n ")
4452
show(io, v)
4553
end
46-
if (mt = meta(var)) !== nothing
54+
return if (mt = meta(var)) !== nothing
4755
print(io, "\nMetadata - ")
4856
show(io, m, mt)
4957
end
@@ -96,15 +104,15 @@ end
96104
97105
Construct a `HAPIVariable` object from a JSON-parsed Dict `d` (containing parameters) at index `i`.
98106
"""
99-
function HAPIVariable(d::Dict, i::Integer)
107+
function HAPIVariable(d::AbstractDict, i::Integer)
100108
data = d["data"]
101109
param = d["parameters"][i + 1]
102110
time = @. DateTime(getindex(data, 1), DEFAULT_DATE_FORMAT)
103111
values = getindex.(data, i + 1)
104112
return HAPIVariable(values, time, param)
105113
end
106114

107-
HAPIVariable(d::Dict, meta, i::Integer) = HAPIVariable(d, i)
115+
HAPIVariable(d::AbstractDict, meta, i::Integer) = HAPIVariable(d, i)
108116

109117
columns(var::HAPIVariable) = meta(var)["columns"]
110118
colsize(var::HAPIVariable) = colsize(meta(var))

test/data.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ end
2222
tmin = "1970-01-01T00:00:00"
2323
tmax = "1970-01-01T00:00:10"
2424

25-
@test_nowarn hapi(server, dataset, parameters, tmin, tmax)
25+
vars = hapi(server, dataset, parameters, tmin, tmax)
26+
display(vars)
2627
end
2728

2829
@testitem "TestData2.1" begin

0 commit comments

Comments
 (0)