Module: RGeoServer::RestApiClient

Includes:
ActiveSupport::Benchmarkable, GeoServerUrlHelpers
Included in:
Catalog
Defined in:
lib/rgeoserver/rest_api_client.rb

Constant Summary

Constants included from GeoServerUrlHelpers

GeoServerUrlHelpers::URI_FORMATS, GeoServerUrlHelpers::URI_REGEX, GeoServerUrlHelpers::URI_SEQUENCES

Instance Method Summary collapse

Methods included from GeoServerUrlHelpers

#url_for

Instance Method Details

#add(what, message, method, options = {}) ⇒ Object

Add resource to the catalog

Parameters:

  • what (String)
  • message (String)
  • method (Symbol)
  • options (Hash) (defaults to: {})


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rgeoserver/rest_api_client.rb', line 93

def add what, message, method, options = {}
  h = options.delete(:headers) || headers(:xml)
  request = client[url_for(what, options)]
  request.options[:headers] = h
  $logger.debug "Adding: \n #{message}"
  begin
    ap({:add_request => request, :add_message => Nokogiri::XML(message)}) if $DEBUG
    return request.send method, message
  rescue RestClient::InternalServerError => e
    $logger.error e.response
    $logger.flush if $logger.respond_to? :flush
    raise GeoServerInvalidRequest, "Error adding #{what.inspect}. See logger for details"
  end

end

#client(config = {}) ⇒ RestClient

Returns cached or new client.

Returns:

  • (RestClient)

    cached or new client



28
29
30
# File 'lib/rgeoserver/rest_api_client.rb', line 28

def client config = {}
  @client ||= rest_client(config.merge(self.config[:restclient]).merge(self.config).with_indifferent_access)
end

#do_url(sub_url, method = :get, data = nil, options = {}, client = @client) ⇒ Object

Do an action on an arbitrary URL path within the catalog Default method is GET

Parameters:

  • sub_url (String)
  • method (String) (defaults to: :get)
  • data (String) (defaults to: nil)

    payload

  • options (Hash) (defaults to: {})

    for request



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rgeoserver/rest_api_client.rb', line 74

def do_url sub_url, method = :get, data = nil, options = {}, client = @client
  sub_url.slice! client.url
  fetcher = client[sub_url]
  fetcher.options.merge(options)
  begin
    return fetcher.get if method == :get
    fetcher.send method, data
  rescue RestClient::InternalServerError => e
    $logger.error e.response
    $logger.flush if $logger.respond_to? :flush
    raise GeoServerInvalidRequest, "Error fetching URL: #{sub_url}. See $logger for details"
  end
end

#gwc_client(config = {}) ⇒ RestClient

Returns cached or new client.

Returns:

  • (RestClient)

    cached or new client



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rgeoserver/rest_api_client.rb', line 33

def gwc_client config = {}
  unless @gwc_client.is_a? RestClient::Resource
    c = config.merge(self.config[:restclient]).merge(self.config)
    if c[:geowebcache_url].nil? or c[:geowebcache_url] == 'builtin'
      c[:url] = c[:url].gsub(%r{/rest$}, '/gwc/rest') # switch to built-in GeoServer GWC
    else
      c[:url] = c[:geowebcache_url]
    end
    @gwc_client = rest_client(c)
  end
  @gwc_client
end

#headers(format) ⇒ Object



46
47
48
49
# File 'lib/rgeoserver/rest_api_client.rb', line 46

def headers format
  sym = :xml || format.to_sym
  {:accept => sym, :content_type=> sym}
end

#modify(what, message, method, options = {}) ⇒ Object

Modify resource in the catalog

Parameters:

  • what (String)
  • message (String)
  • method (Symbol)
  • options (Hash) (defaults to: {})


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rgeoserver/rest_api_client.rb', line 114

def modify what, message, method, options = {}
  h = options.delete(:headers) || headers(:xml)
  request = client[url_for(what, options)]
  request.options[:headers] = h
  $logger.debug "Modifying: \n #{message}"
  begin
    ap({:modify_request => request, :modify_message => Nokogiri::XML(message)}) if $DEBUG
    return request.send method, message
  rescue RestClient::InternalServerError => e
    $logger.error e.response
    $logger.flush if $logger.respond_to? :flush
    raise GeoServerInvalidRequest, "Error modifying #{what.inspect}. See $logger for details"
  end

end

#purge(what, options) ⇒ Object

Purge resource from the catalog. Options can include recurse=true or false

Parameters:

  • what (OrderedHash)
  • options (Hash)


133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rgeoserver/rest_api_client.rb', line 133

def purge what, options
  request = client[url_for(what, options)]
  $logger.debug "Purge: \n #{request}"
  begin
    ap({:purge_request => request}) if $DEBUG
    return request.delete
  rescue RestClient::InternalServerError => e
    $logger.error e.response
    $logger.flush if $logger.respond_to? :flush
    raise GeoServerInvalidRequest, "Error deleting #{what.inspect}. See $logger for details"
  end
end

#rest_client(c) ⇒ Object

Instantiates a rest client with passed configuration return [RestClient::Resource]

Parameters:

  • c (Hash)

    configuration

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
# File 'lib/rgeoserver/rest_api_client.rb', line 16

def rest_client c
  ap({:rest_client => c}) if $DEBUG
  raise ArgumentError, "#rest_client requires :url" if c[:url].nil?
  RestClient::Resource.new(c[:url],
      :user => c[:user],
      :password => c[:password],
      :headers => c[:headers],
      :timeout => (c[:timeout] || 300).to_i,
      :open_timeout => (c[:open_timeout] || 60).to_i)
end

#search(what, options = {}) ⇒ Object

Search a resource in the catalog

Parameters:

  • what (OrderedHash)
  • options (Hash) (defaults to: {})


54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rgeoserver/rest_api_client.rb', line 54

def search what, options = {}
  h = options.delete(:headers) || headers(:xml)
  resources = client[url_for(what, options)]
  resources.options[:headers] = h
  begin
    ap({ :func => { :search => what }, :request => resources }) if $DEBUG
    return resources.get
  rescue RestClient::InternalServerError => e
    $logger.error e.response
    $logger.flush if $logger.respond_to? :flush
    raise GeoServerInvalidRequest, "Error listing #{what.inspect}. See $logger for details"
  end
end