|
| 1 | +require 'json' |
| 2 | +require 'net/http' |
| 3 | +require 'uri' |
| 4 | +require 'fileutils' |
| 5 | +require 'contentstack/error' |
| 6 | + |
| 7 | +module Contentstack |
| 8 | + # Centralised endpoint resolver. Reads region metadata from the local |
| 9 | + # regions.json (downloaded from https://artifacts.contentstack.com/regions.json |
| 10 | + # at gem install time) and falls back to a live fetch when the file is absent. |
| 11 | + # |
| 12 | + # Delegates to ContentstackUtils.get_contentstack_endpoint when that gem |
| 13 | + # ships the method (contentstack-utils-ruby PR #41). |
| 14 | + class Endpoint |
| 15 | + REGISTRY_URL = 'https://artifacts.contentstack.com/regions.json' |
| 16 | + DATA_FILE_PATH = File.join(File.dirname(File.dirname(__FILE__)), 'data', 'regions.json') |
| 17 | + |
| 18 | + # Maps the SDK's short service keys to the camelCase keys used in regions.json, |
| 19 | + # preserving backward compatibility for callers using Service::CDA / Service::CMA. |
| 20 | + SERVICE_MAP = { |
| 21 | + 'cda' => 'contentDelivery', |
| 22 | + 'cma' => 'contentManagement', |
| 23 | + 'preview' => 'preview' |
| 24 | + }.freeze |
| 25 | + |
| 26 | + DEFAULT_SERVICE = 'contentDelivery' |
| 27 | + |
| 28 | + # Resolve a Contentstack service URL for the given region and service. |
| 29 | + # |
| 30 | + # Contentstack::Endpoint.get_contentstack_endpoint('eu') |
| 31 | + # # => "https://eu-cdn.contentstack.com" |
| 32 | + # |
| 33 | + # Contentstack::Endpoint.get_contentstack_endpoint('us', 'contentManagement') |
| 34 | + # # => "https://api.contentstack.io" |
| 35 | + # |
| 36 | + # Contentstack::Endpoint.get_contentstack_endpoint('eu', 'cda') # short alias |
| 37 | + # # => "https://eu-cdn.contentstack.com" |
| 38 | + # |
| 39 | + # When +custom_host+ is supplied the region CDN prefix is derived from |
| 40 | + # regions.json and prepended to the custom domain. |
| 41 | + def self.get_contentstack_endpoint(region, service = DEFAULT_SERVICE, custom_host = nil) |
| 42 | + region_key = region.to_s.downcase |
| 43 | + service_key = SERVICE_MAP.fetch(service.to_s, service.to_s) |
| 44 | + |
| 45 | + if custom_host.nil? || custom_host.to_s.empty? |
| 46 | + if defined?(ContentstackUtils) && ContentstackUtils.respond_to?(:get_contentstack_endpoint) |
| 47 | + return ContentstackUtils.get_contentstack_endpoint(region_key, service_key) |
| 48 | + end |
| 49 | + resolve_standard(region_key, service_key) |
| 50 | + else |
| 51 | + resolve_custom_host(region_key, service_key, custom_host) |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + # Download the latest regions.json from https://artifacts.contentstack.com/regions.json |
| 56 | + # and persist it locally. Called automatically by ext/download_regions/extconf.rb |
| 57 | + # during bundle install / bundle update, and by `bundle exec rake refresh_regions`. |
| 58 | + def self.refresh_regions |
| 59 | + data = fetch_from_registry |
| 60 | + FileUtils.mkdir_p(File.dirname(DATA_FILE_PATH)) |
| 61 | + File.write(DATA_FILE_PATH, JSON.pretty_generate(data)) |
| 62 | + data |
| 63 | + end |
| 64 | + |
| 65 | + private |
| 66 | + |
| 67 | + def self.resolve_standard(region_key, service_key) |
| 68 | + region_data = find_region(region_key) |
| 69 | + unless region_data |
| 70 | + raise Contentstack::Error.new( |
| 71 | + Contentstack::ErrorMessages.region_invalid(region_key, all_region_ids) |
| 72 | + ) |
| 73 | + end |
| 74 | + unless region_data['endpoints'].key?(service_key) |
| 75 | + raise Contentstack::Error.new( |
| 76 | + Contentstack::ErrorMessages.service_invalid(service_key, region_data['endpoints'].keys) |
| 77 | + ) |
| 78 | + end |
| 79 | + region_data['endpoints'][service_key] |
| 80 | + end |
| 81 | + |
| 82 | + def self.resolve_custom_host(region_key, service_key, custom_host) |
| 83 | + region_data = find_region(region_key) |
| 84 | + if region_data && region_data['endpoints'].key?(service_key) |
| 85 | + standard_url = region_data['endpoints'][service_key] |
| 86 | + prefix = URI.parse(standard_url).host.split('.').first |
| 87 | + "https://#{prefix}.#{custom_host}" |
| 88 | + else |
| 89 | + "https://cdn.#{custom_host}" |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + # Find a region by its canonical id or any of its declared aliases. |
| 94 | + def self.find_region(region_key) |
| 95 | + load_regions['regions'].find do |r| |
| 96 | + r['id'] == region_key || |
| 97 | + r['alias'].any? { |a| a.downcase == region_key } |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + def self.all_region_ids |
| 102 | + load_regions['regions'].map { |r| r['id'] } |
| 103 | + end |
| 104 | + |
| 105 | + def self.load_regions |
| 106 | + if File.exist?(DATA_FILE_PATH) |
| 107 | + JSON.parse(File.read(DATA_FILE_PATH)) |
| 108 | + else |
| 109 | + warn '[Contentstack] regions.json not found locally — fetching from registry...' |
| 110 | + data = fetch_from_registry |
| 111 | + begin |
| 112 | + FileUtils.mkdir_p(File.dirname(DATA_FILE_PATH)) |
| 113 | + File.write(DATA_FILE_PATH, JSON.pretty_generate(data)) |
| 114 | + rescue => e |
| 115 | + warn "[Contentstack] Could not cache regions.json: #{e.message}" |
| 116 | + end |
| 117 | + data |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + def self.fetch_from_registry |
| 122 | + uri = URI.parse(REGISTRY_URL) |
| 123 | + response = Net::HTTP.get_response(uri) |
| 124 | + unless response.is_a?(Net::HTTPSuccess) |
| 125 | + raise Contentstack::Error.new( |
| 126 | + "Failed to fetch region metadata from registry (HTTP #{response.code}). " \ |
| 127 | + 'Ensure network access and try again.' |
| 128 | + ) |
| 129 | + end |
| 130 | + JSON.parse(response.body) |
| 131 | + end |
| 132 | + end |
| 133 | +end |
0 commit comments