mirror of
https://github.com/mistergibson/gxg-framework.git
synced 2026-08-15 03:46:00 -07:00
Added GxGApi client
This commit is contained in:
parent
6fa51485dd
commit
247d7c8607
3 changed files with 142 additions and 4 deletions
|
|
@ -1,6 +1,6 @@
|
|||
Gem::Specification.new do |s|
|
||||
s.name = 'gxg-framework'
|
||||
s.version = '0.0.52'
|
||||
s.version = '0.0.53'
|
||||
s.licenses = ['UNLICENSED']
|
||||
s.summary = "GxG Framework for JRuby"
|
||||
s.description = "GxG Framework for JRuby"
|
||||
|
|
|
|||
|
|
@ -1770,10 +1770,7 @@ module GxG
|
|||
the_record[:permissions] = entry[:permissions]
|
||||
if self.role_exist?(entry[:credential])
|
||||
role = self.role_fetch({:uuid => entry[:credential].to_s})
|
||||
# group = self.group_fetch({:uuid => role[:group_uuid].to_s})
|
||||
the_record[:details][:role_title] = role[:title]
|
||||
# the_record[:details][:group] = group[:uuid].to_s.to_sym
|
||||
# the_record[:details][:group_title] = group[:title]
|
||||
else
|
||||
if self.user_exist?(entry[:credential])
|
||||
user = self.user_fetch({:uuid => entry[:credential].to_s})
|
||||
|
|
|
|||
|
|
@ -3691,6 +3691,147 @@ module GxG
|
|||
end
|
||||
::GxG::Networking::DISPATCHER.register_client("https",::GxG::Networking::HttpsClient)
|
||||
#
|
||||
class GxGApi
|
||||
private
|
||||
#
|
||||
def pull(details={})
|
||||
result = nil
|
||||
begin
|
||||
response = @connector.get(::URI::parse("#{@scheme}://#{@hostname}#{@endpoint}?details=#{details.gxg_export.to_json.encrypt(@csrf).encode64}"))
|
||||
response.body.rewind
|
||||
if response.code.to_i == 200
|
||||
raw_result = ::Hash::gxg_import(JSON::parse(response.body.read().decode64.decrypt(@csrf), :symbolize_names => true))
|
||||
if raw_result.is_a?(::Hash)
|
||||
raw_result.search do |the_value, the_selector, the_container|
|
||||
if the_value.is_a?(::Hash)
|
||||
if the_value[:formats].is_a?(::Hash) && the_value[:records].is_a?(::Array)
|
||||
imported_list = ::GxG::Database::Database::detached_package_import(the_value)
|
||||
if imported_list.size > 1
|
||||
the_container[(the_selector)] = imported_list
|
||||
else
|
||||
the_container[(the_selector)] = imported_list[0]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
result = raw_result[:result]
|
||||
end
|
||||
else
|
||||
raise Exception.new(response.body.read())
|
||||
end
|
||||
rescue Exception => the_error
|
||||
log_error({:error => the_error})
|
||||
end
|
||||
result
|
||||
end
|
||||
#
|
||||
def push(data={})
|
||||
result = nil
|
||||
begin
|
||||
response = @connector.put(::URI::parse("#{@scheme}://#{@hostname}#{@endpoint}", data.gxg_export.to_json.encrypt(@csrf).encode64))
|
||||
response.body.rewind
|
||||
if response.code.to_i == 200
|
||||
raw_result = ::Hash::gxg_import(JSON::parse(response.body.read().decode64.decrypt(@csrf), :symbolize_names => true))
|
||||
if raw_result.is_a?(::Hash)
|
||||
raw_result.search do |the_value, the_selector, the_container|
|
||||
if the_value.is_a?(::Hash)
|
||||
if the_value[:formats].is_a?(::Hash) && the_value[:records].is_a?(::Array)
|
||||
imported_list = ::GxG::Database::Database::detached_package_import(the_value)
|
||||
if imported_list.size > 1
|
||||
the_container[(the_selector)] = imported_list
|
||||
else
|
||||
the_container[(the_selector)] = imported_list[0]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
result = raw_result[:result]
|
||||
end
|
||||
else
|
||||
raise Exception.new(response.body.read())
|
||||
end
|
||||
rescue Exception => the_error
|
||||
log_error({:error => the_error})
|
||||
end
|
||||
result
|
||||
end
|
||||
#
|
||||
public
|
||||
#
|
||||
def initialize(the_url=nil, options={})
|
||||
the_uri = ::URI::parse(the_url.to_s)
|
||||
response = nil
|
||||
@scheme = "https"
|
||||
@hostname = the_uri.hostname
|
||||
@endpoint = the_uri.path
|
||||
@connector = nil
|
||||
@csrf = nil
|
||||
@interface = {}
|
||||
begin
|
||||
the_uri = ::URI::parse("https://#{@hostname}#{@endpoint}?details=eyJpbnRyb2R1Y3Rpb24iOnRydWV9")
|
||||
the_connector = ::GxG::Networking::HttpsClient.new(the_uri)
|
||||
response = the_connector.get(the_uri)
|
||||
rescue Exception => the_error
|
||||
begin
|
||||
@scheme = "http"
|
||||
the_uri = ::URI::parse("http://#{@hostname}#{@endpoint}?details=eyJpbnRyb2R1Y3Rpb24iOnRydWV9")
|
||||
the_connector = ::GxG::Networking::HttpClient.new(the_uri)
|
||||
response = the_connector.get(the_uri)
|
||||
rescue Exception => the_error
|
||||
raise Exception.new("Failed to establish session link at #{the_url.to_s}.")
|
||||
end
|
||||
end
|
||||
if response
|
||||
if response.code.to_i == 200
|
||||
@connector = the_connector
|
||||
response.body.rewind()
|
||||
the_csrf = ::Hash::gxg_import(JSON::parse(response.body.read().decode64, :symbolize_names => true))
|
||||
@csrf = the_csrf[:csrf]
|
||||
end
|
||||
end
|
||||
self
|
||||
end
|
||||
#
|
||||
def interface()
|
||||
@interface
|
||||
end
|
||||
#
|
||||
def login(username=nil, password=nil)
|
||||
if push({:upgrade_credential => {:username => username.to_s, :password => password.to_s}})
|
||||
@interface = pull({:interface => nil})
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
#
|
||||
def logout()
|
||||
pull({:downgrade_credential => nil})
|
||||
@interface = pull({:interface => nil})
|
||||
true
|
||||
end
|
||||
#
|
||||
def get(details={})
|
||||
if details.is_a?(::Hash)
|
||||
pull(details)
|
||||
else
|
||||
log_error("You MUST provide a Hash")
|
||||
nil
|
||||
end
|
||||
end
|
||||
#
|
||||
def put(data={})
|
||||
if data.is_a?(::Hash)
|
||||
push(data)
|
||||
else
|
||||
log_error("You MUST provide a Hash")
|
||||
nil
|
||||
end
|
||||
end
|
||||
#
|
||||
end
|
||||
::GxG::Networking::DISPATCHER.register_client("gxg",::GxG::Networking::GxGApi)
|
||||
#
|
||||
class MatrixID
|
||||
def initialize(the_id=nil)
|
||||
if the_id.is_a?(::MatrixSdk::MXID)
|
||||
|
|
|
|||
Loading…
Reference in a new issue