diff --git a/fixtures/vcr_cassettes/labels/with_name_param_as_array.yml b/fixtures/vcr_cassettes/labels/with_name_param_as_array.yml new file mode 100644 index 0000000..8966867 --- /dev/null +++ b/fixtures/vcr_cassettes/labels/with_name_param_as_array.yml @@ -0,0 +1,51 @@ +--- +http_interactions: +- request: + method: get + uri: https://inbox.azconfig.io/labels?api-version=1.0&name=prod,test + body: + encoding: US-ASCII + string: '' + headers: + X-Ms-Date: + - Tue, 09 Jan 2024 18:07:42 GMT + X-Ms-Content-Sha256: + - 47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU= + Authorization: + - HMAC-SHA256 Credential=TjvF&SignedHeaders=x-ms-date;host;x-ms-content-sha256&Signature=BT7vCF9rfrZpCsRXR7m2QyQxWhq0q92nlIJgujpTuPM= + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + Host: + - inbox.azconfig.io + response: + status: + code: 200 + message: OK + headers: + Date: + - Tue, 09 Jan 2024 18:07:42 GMT + Content-Type: + - application/vnd.microsoft.appconfig.labelset+json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Sync-Token: + - zAJw6V16=NjoxMyM1NDI0NTQ2;sn=5424546 + X-Ms-Request-Id: + - 78a45bb3-131a-4128-882d-64afbd2a0da6 + X-Ms-Correlation-Request-Id: + - 78a45bb3-131a-4128-882d-64afbd2a0da6 + Strict-Transport-Security: + - max-age=31536000; includeSubDomains + Access-Control-Allow-Origin: + - "*" + body: + encoding: UTF-8 + string: '{"items":[{"name":"prod"},{"name":"test"}]}' + recorded_at: Tue, 09 Jan 2024 18:07:42 GMT +recorded_with: VCR 6.2.0 diff --git a/fixtures/vcr_cassettes/labels/with_name_param_as_string.yml b/fixtures/vcr_cassettes/labels/with_name_param_as_string.yml new file mode 100644 index 0000000..6ee105c --- /dev/null +++ b/fixtures/vcr_cassettes/labels/with_name_param_as_string.yml @@ -0,0 +1,51 @@ +--- +http_interactions: +- request: + method: get + uri: https://inbox.azconfig.io/labels?api-version=1.0&name=p* + body: + encoding: US-ASCII + string: '' + headers: + X-Ms-Date: + - Tue, 09 Jan 2024 18:07:17 GMT + X-Ms-Content-Sha256: + - 47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU= + Authorization: + - HMAC-SHA256 Credential=TjvF&SignedHeaders=x-ms-date;host;x-ms-content-sha256&Signature=tZLdY/9jghuYIThY9JyEc5mQuPfI2pSiEfyjtAdhrr4= + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + Host: + - inbox.azconfig.io + response: + status: + code: 200 + message: OK + headers: + Date: + - Tue, 09 Jan 2024 18:07:17 GMT + Content-Type: + - application/vnd.microsoft.appconfig.labelset+json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Sync-Token: + - zAJw6V16=NjoxMyM1NDI0NTQ2;sn=5424546 + X-Ms-Request-Id: + - 17520fc2-db00-4a52-8e1e-1ca89c30ce32 + X-Ms-Correlation-Request-Id: + - 17520fc2-db00-4a52-8e1e-1ca89c30ce32 + Strict-Transport-Security: + - max-age=31536000; includeSubDomains + Access-Control-Allow-Origin: + - "*" + body: + encoding: UTF-8 + string: '{"items":[{"name":"prod"}]}' + recorded_at: Tue, 09 Jan 2024 18:07:17 GMT +recorded_with: VCR 6.2.0 diff --git a/fixtures/vcr_cassettes/labels/without_name_param.yml b/fixtures/vcr_cassettes/labels/without_name_param.yml new file mode 100644 index 0000000..4605350 --- /dev/null +++ b/fixtures/vcr_cassettes/labels/without_name_param.yml @@ -0,0 +1,51 @@ +--- +http_interactions: +- request: + method: get + uri: https://inbox.azconfig.io/labels?api-version=1.0&name=* + body: + encoding: US-ASCII + string: '' + headers: + X-Ms-Date: + - Tue, 09 Jan 2024 18:03:48 GMT + X-Ms-Content-Sha256: + - 47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU= + Authorization: + - HMAC-SHA256 Credential=TjvF&SignedHeaders=x-ms-date;host;x-ms-content-sha256&Signature=N9CYXeOBBH17GWPlFtWGDYvMSocSlHH6uMTVZZ+CbnE= + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + Host: + - inbox.azconfig.io + response: + status: + code: 200 + message: OK + headers: + Date: + - Tue, 09 Jan 2024 18:03:49 GMT + Content-Type: + - application/vnd.microsoft.appconfig.labelset+json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Sync-Token: + - zAJw6V16=NjoxMyM1NDI0NTQ2;sn=5424546 + X-Ms-Request-Id: + - 59a9f83f-cd5d-48d9-9e55-d8740c3fc7c0 + X-Ms-Correlation-Request-Id: + - 59a9f83f-cd5d-48d9-9e55-d8740c3fc7c0 + Strict-Transport-Security: + - max-age=31536000; includeSubDomains + Access-Control-Allow-Origin: + - "*" + body: + encoding: UTF-8 + string: '{"items":[{"name":null},{"name":"prod"},{"name":"test"}]}' + recorded_at: Tue, 09 Jan 2024 18:03:49 GMT +recorded_with: VCR 6.2.0 diff --git a/lib/azure_app_config/base.rb b/lib/azure_app_config/base.rb index 277a760..e828464 100644 --- a/lib/azure_app_config/base.rb +++ b/lib/azure_app_config/base.rb @@ -37,6 +37,14 @@ def fetch(name, label: nil) parse_fetch_response response end + def labels(name: nil) + name, _ = normalise_parameters(name: name, label: nil) + path = "/labels" + + query_params = URI.encode_www_form(name: name.join(",")) + parse_labels_response client.get(path, query_params) + end + def keys(name: nil) name, = normalise_parameters(name: name, label: nil) path = "/keys" @@ -120,5 +128,9 @@ def parse_keys_response(response) data = JSON.parse(response.body) data["items"].map { |item| item["name"].gsub(key_prefix, "") } end + + def parse_labels_response(response) + JSON.parse(response.body)["items"].map { |item| item["name"] } + end end end diff --git a/spec/azure_app_config/base_spec.rb b/spec/azure_app_config/base_spec.rb index 68e7630..7dcefa4 100644 --- a/spec/azure_app_config/base_spec.rb +++ b/spec/azure_app_config/base_spec.rb @@ -311,6 +311,51 @@ end end + describe "#labels" do + subject(:result) { described_class.instance.labels(name: name) } + + describe "without name param" do + let(:name) { nil } + + it "returns all labels" do + VCR.use_cassette("labels/without_name_param") do + expect(result).to be_a Array + + expect(result.size).to be > 0 + expect(result).to match_array([nil, "prod", "test"]) + end + end + end + + describe "with name param" do + describe "as string" do + let(:name) { "p*" } + + it "returns matching labels" do + VCR.use_cassette("labels/with_name_param_as_string") do + expect(result).to be_a Array + + expect(result.size).to eq 1 + expect(result).to(be_all { |k| k.start_with?(name.chomp("*")) }) + end + end + end + + describe "as array" do + let(:name) { ["prod", "test"] } + + it "returns matching labels" do + VCR.use_cassette("labels/with_name_param_as_array") do + expect(result).to be_a Array + + expect(result.size).to eq 2 + expect(result).to(be_all { |k| name.include?(k) }) + end + end + end + end + end + describe "#method_missing" do it "testing if all the above methods work as expected" do expect(described_class).to respond_to(:all) @@ -318,6 +363,7 @@ expect(described_class).to respond_to(:disabled?) expect(described_class).to respond_to(:enabled?) expect(described_class).to respond_to(:keys) + expect(described_class).to respond_to(:labels) expect(described_class).not_to respond_to(:some_wrong_method) end