Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
namara / lib / sources.rb
Size: Mime:
require_relative 'rpc/sources/sources_twirp'

class Namara
  # Sources rpc calls
  def create_source(source = {})
    response = rpc_request(source_client) do |client|
      client.create_source(serialize_source(source), rpc_headers)
    end

    OpenStruct.new(response.data.source.to_h)
  end

  def list_sources(organization_id)
    payload = { organization_id: organization_id }

    response = rpc_request(source_client) do |client|
      client.list_sources(payload, rpc_headers)
    end

    OpenStruct.new(sources: ObjectToHash.to_hash_array(response.data.sources.to_a), total_count: response.data.total_count)
  end

  def list_sources_with_connections(organization_id:, limit: nil, offset: nil, portals: [], urls: [], labels: [])
    filter = {}
    filter[:limit] = {value: limit} if limit
    filter[:offset] = {value: offset} if offset
    filter[:portals] = portals if portals && portals.length > 0
    filter[:urls] = urls if urls && urls.length > 0
    filter[:labels] = labels if labels && labels.length > 0

    payload = {
      filter: filter,
      organization_id: organization_id
    }

    response = rpc_request(source_client) do |client|
      client.list_sources_with_connections(payload, rpc_headers)
    end

    OpenStruct.new(sources: ObjectToHash.to_hash_array(response.data.sources.to_a), total_count: response.data.total_count)
  end

  def update_source(source = {})
    response = rpc_request(source_client) do |client|
      client.update_source(serialize_source(source), rpc_headers)
    end

    OpenStruct.new(response.data.source.to_h)
  end

  def get_source(source_id, organization_id)
    payload = {
        id:              source_id,
        organization_id: organization_id
    }

    response = rpc_request(source_client) do |client|
      client.get_source(payload, rpc_headers)
    end

    OpenStruct.new(response.data.source.to_h)
  end

  def get_source_with_connections(source_id:, organization_id: nil)
    payload = {
      id:              source_id,
      organization_id: organization_id
    }

    response = rpc_request(source_client) do |client|
      client.get_source_with_connections(payload, rpc_headers)
    end

    OpenStruct.new(response.data.source.to_h)
  end

  def delete_source(source = {})
    rpc_request(source_client) do |client|
      client.delete_source(serialize_source(source), rpc_headers)
    end
  end

  # Connections rpc calls
  def create_connection(connection_args = {})
    response = rpc_request(source_client) do |client|
      client.create_connection(connection_args, rpc_headers)
    end

    OpenStruct.new(response.data.connection.to_h)
  end

  def list_connections(limit: nil, offset: nil, organization_id: nil, source_ids: [], portals: [])
    filter = {}
    filter[:limit] = { value: limit } if limit
    filter[:offset] = { value: offset } if offset
    filter[:organization_id] = organization if organization_id
    filter[:source_ids] = source_ids if source_ids && source_ids.length > 0
    filter[:portals] = portals if portals && portals.length > 0

    payload = { filter: filter }

    response = rpc_request(source_client) do |client|
      client.list_connections(payload, rpc_headers)
    end
    
    OpenStruct.new(connections: ObjectToHash.to_hash_array(response.data.connections.to_a), total_count: response.data.total_count)
  end

  def get_connection(connection_id)
    payload = { id: connection_id }

    response = rpc_request(source_client) do |client|
      client.get_connection(payload, rpc_headers)
    end

    OpenStruct.new(response.data.connection.to_h)
  end

  def update_connection(connection_args = {})
    response = rpc_request(source_client) do |client|
      client.update_connection(connection_args, rpc_headers)
    end

    OpenStruct.new(response.data.connection.to_h)
  end

  def delete_connection(connection_id)
    payload = { id: connection_id }

    response = rpc_request(source_client) do |client|
      client.delete_connection(payload, rpc_headers)
    end
  end

  # Credentials rpc calls
  def create_credential(credential = {})
    response = rpc_request(source_client) do |client|
      client.create_credential(serialize_credential(credential), rpc_headers)
    end

    OpenStruct.new(response.data.credential.to_h)
  end

  def list_credentials(organization_id)
    payload = { organization_id: organization_id }

    response = rpc_request(source_client) do |client|
      client.list_credentials(payload, rpc_headers)
    end

    OpenStruct.new(credentials: ObjectToHash.to_hash_array(response.data.credentials.to_a), total_count: response.data.total_count)
  end

  def get_credential(credential_id, organization_id)
    payload = {
        id:              credential_id,
        organization_id: organization_id
    }

    response = rpc_request(source_client) do |client|
      client.get_credential(payload, rpc_headers)
    end

    OpenStruct.new(response.data.credential.to_h)
  end

  def update_credential(credential = {})
    response = rpc_request(source_client) do |client|
      client.update_credential(serialize_credential(credential), rpc_headers)
    end

    OpenStruct.new(response.data.credential.to_h)
  end

  def delete_credential(credential = {})
    rpc_request(source_client) do |client|
      client.delete_credential(serialize_credential(credential), rpc_headers)
    end
  end

  private

  def serialize_source(source)
    { source: source.to_h }
  end

  def serialize_credential(credential)
    { credential: credential.to_h }
  end

  def source_client
    @source_client ||= Sources::SourcesServiceClient.new(api_url)
  end
end