Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoCredentials class for accessing publicly available data #50

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/GoogleCloud.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Google Cloud APIs
module GoogleCloud

export
JSONCredentials, MetadataCredentials, GoogleSession, authorize,
JSONCredentials, MetadataCredentials, NoCredentials, GoogleSession, authorize,
set_session!, get_session
export
iam, storage, compute, container, pubsub, logging, datastore
Expand Down
12 changes: 8 additions & 4 deletions src/api/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,13 @@ function execute(session::GoogleSession, resource::APIResource, method::APIMetho

# obtain and use access token
auth = authorize(session)
headers = Dict{String, String}(
"Authorization" => "$(auth[:token_type]) $(auth[:access_token])"
)
headers = if isnothing(auth)
Dict{String, String}()
else
Dict{String, String}(
"Authorization" => "$(auth[:token_type]) $(auth[:access_token])"
)
end
params = Dict(params)

# check if data provided when not expected
Expand Down Expand Up @@ -273,7 +277,7 @@ function execute(session::GoogleSession, resource::APIResource, method::APIMetho

# merge in default parameters and evaluate any expressions
params = merge!(copy(method.default_params), Dict(params))
extra = Dict(:project_id => session.credentials.project_id)
extra = hasproperty(session.credentials, :project_id) ? Dict(:project_id => session.credentials.project_id) : Dict()
for (key, val) in params
if isa(val, Symbol)
params[key] = extra[val]
Expand Down
6 changes: 5 additions & 1 deletion src/credentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Google Cloud Platform service-account API credentials.
"""
module credentials

export Credentials, JSONCredentials, MetadataCredentials
export Credentials, JSONCredentials, MetadataCredentials, NoCredentials

import Base: show, print
import JSON
Expand Down Expand Up @@ -126,4 +126,8 @@ function print(io::IO, x::Credentials)
end
show(io::IO, x::JSONCredentials) = print(io, x)

struct NoCredentials <: Credentials
end

end

2 changes: 2 additions & 0 deletions src/session.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,6 @@ function authorize(session::GoogleSession; cache::Bool=true)
authorization
end

authorize(session::GoogleSession{T}) where {T <: NoCredentials} = nothing

end
31 changes: 31 additions & 0 deletions test/nocredentials.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Test

using GoogleCloud
using JSON

creds = NoCredentials()

session = GoogleSession(creds, ["devstorage.full_control"])

## Test download of JSON data from Pong dataset
bucketName = "atari-replay-datasets"
prefix="dqn/Pong/1/replay_logs/\$store\$_action"

fileList = GoogleCloud.storage(:Object, :list, bucketName; prefix, session)

parities = map(fileList) do item
reduce(⊻, GoogleCloud.storage(:Object, :get, bucketName, item[:name]; session))
end

EXPECTED_PARITY = 0xf1

## Test download of binary data from sentinel dataset
sentinelbucket = "gcp-public-data-sentinel-2"
sentinelpath = "L2/tiles/32/T/NS/S2A_MSIL2A_20210506T102021_N0300_R065_T32TNS_20210506T132458.SAFE/GRANULE/L2A_T32TNS_A030664_20210506T102022/IMG_DATA/R20m/T32TNS_20210506T102021_B07_20m.jp2"
sentinelimage = GoogleCloud.storage(:Object, :get, sentinelbucket, sentinelpath; session)
EXPECTED_PARITY_SENTINEL = 0x6e

@testset "storage access and NoCredentials" begin
@test reduce(⊻, fetch.(parities)) == EXPECTED_PARITY
@test reduce(⊻, sentinelimage) == EXPECTED_PARITY_SENTINEL
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ using GoogleCloud
using Test

include("api.jl")
include("nocredentials.jl")
#include("storage.jl")