Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Install dependencies
run: shards install
- name: Type-check
run: crystal build src/right.cr -o bin/rightdocuments --no-codegen --error-on-warnings
run: crystal build src/rightdocuments.cr -o bin/rightdocuments --no-codegen --error-on-warnings
- name: Run specs
run: crystal spec --error-on-warnings
if: hashFiles('spec/**/*_spec.cr') != ''
2 changes: 1 addition & 1 deletion .github/workflows/sdk-updated.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
run: shards update rightdocuments-client
- name: Build + test
run: |
crystal build src/right.cr -o bin/rightdocuments --no-codegen --error-on-warnings
crystal build src/rightdocuments.cr -o bin/rightdocuments --no-codegen --error-on-warnings
if compgen -G "spec/**/*_spec.cr" > /dev/null; then
crystal spec --error-on-warnings
fi
Expand Down
6 changes: 3 additions & 3 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: right
version: 0.3.5
name: rightdocuments
version: 0.3.6

authors:
- usiegj00 <248302+usiegj00@users.noreply.github.com>

targets:
rightdocuments:
main: src/right.cr
main: src/rightdocuments.cr

crystal: ">= 1.10"

Expand Down
3 changes: 0 additions & 3 deletions src/right.cr

This file was deleted.

3 changes: 0 additions & 3 deletions src/right/version.cr

This file was deleted.

3 changes: 3 additions & 0 deletions src/rightdocuments.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require "./rightdocuments/cli"

RightDocuments::CLI.run(ARGV)
59 changes: 44 additions & 15 deletions src/right/cli.cr → src/rightdocuments/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require "oauth-device-flow"
require "rightdocuments-client"
require "./version"

module Right
module RightDocuments
CLIENT_ID = ENV["RIGHTDOCUMENTS_CLIENT_ID"]? || "d4803e11ec443f87854a6caec69ab50b"
BASE_URL = ENV["RIGHTDOCUMENTS_URL"]? || "https://app.rightdocuments.com"

Expand Down Expand Up @@ -72,14 +72,15 @@ module Right

module CLI
def self.run(argv : Array(String)) : Nil
app = ACON::Application.new("rightdocuments", VERSION)
app = ACON::Application.new("rightdocuments", CLI_VERSION)
app.add WhoamiCommand.new
app.add LoginCommand.new
app.add LogoutCommand.new
app.add EntitiesCommand.new
app.add EntitiesCreateCommand.new
app.add EntitiesInfoCommand.new
app.add DocumentsCommand.new
app.add DocumentsDeleteCommand.new
app.add ImportCommand.new
app.add CatalogCommand.new
app.add SkillsCommand.new
Expand All @@ -100,7 +101,7 @@ module Right
@[ACONA::AsCommand("login", description: "Authenticate via OAuth device flow")]
class LoginCommand < ACON::Command
protected def execute(input : ACON::Input::Interface, output : ACON::Output::Interface) : ACON::Command::Status
Right.oauth.authenticate(scope: "documents:read documents:write")
RightDocuments.oauth.authenticate(scope: "documents:read documents:write")
output.puts "Logged in."
ACON::Command::Status::SUCCESS
rescue ex
Expand All @@ -112,7 +113,7 @@ module Right
@[ACONA::AsCommand("logout", description: "Clear stored credentials")]
class LogoutCommand < ACON::Command
protected def execute(input : ACON::Input::Interface, output : ACON::Output::Interface) : ACON::Command::Status
Right.oauth.logout
RightDocuments.oauth.logout
output.puts "Logged out."
ACON::Command::Status::SUCCESS
end
Expand All @@ -127,7 +128,7 @@ module Right
end

protected def execute(input : ACON::Input::Interface, output : ACON::Output::Interface) : ACON::Command::Status
Right.sdk_config
RightDocuments.sdk_config
result = RightDocuments::MeApi.new.api_v1_me_get
if json?(input)
output.puts result.to_pretty_json
Expand All @@ -153,7 +154,7 @@ module Right
end

protected def execute(input : ACON::Input::Interface, output : ACON::Output::Interface) : ACON::Command::Status
Right.sdk_config
RightDocuments.sdk_config
result = RightDocuments::EntitiesApi.new.api_v1_entities_get
if json?(input)
output.puts result.to_pretty_json
Expand All @@ -179,7 +180,7 @@ module Right
end

protected def execute(input : ACON::Input::Interface, output : ACON::Output::Interface) : ACON::Command::Status
Right.sdk_config
RightDocuments.sdk_config
result = RightDocuments::EntitiesApi.new.api_v1_entities_id_get(input.argument("entity_id").to_s)
if json?(input)
output.puts result.to_pretty_json
Expand Down Expand Up @@ -244,7 +245,7 @@ module Right
end

protected def execute(input : ACON::Input::Interface, output : ACON::Output::Interface) : ACON::Command::Status
Right.sdk_config
RightDocuments.sdk_config
result = RightDocuments::CatalogApi.new.api_v1_catalog_get
if json?(input)
output.puts result.to_pretty_json
Expand Down Expand Up @@ -310,15 +311,15 @@ module Right
)
body = RightDocuments::ApiV1EntitiesPostRequest.new(entity: entity).to_json

uri = URI.parse("#{Right::BASE_URL}/api/v1/entities")
uri = URI.parse("#{RightDocuments::BASE_URL}/api/v1/entities")
headers = HTTP::Headers{
"Authorization" => "Bearer #{Right.oauth.access_token}",
"Authorization" => "Bearer #{RightDocuments.oauth.access_token}",
"Content-Type" => "application/json",
}
response = HTTP::Client.post(uri, headers: headers, body: body)
unless response.status.success?
output.puts "entities:create failed: HTTP #{response.status.code} — #{response.body}"
if hint = Right.enum_error_hint(response.body)
if hint = RightDocuments.enum_error_hint(response.body)
output.puts hint
end
return ACON::Command::Status::FAILURE
Expand Down Expand Up @@ -354,8 +355,8 @@ module Right
protected def execute(input : ACON::Input::Interface, output : ACON::Output::Interface) : ACON::Command::Status
entity_id = input.argument("entity_id").to_s
# Swagger doesn't describe the response schema, so call HTTP directly.
uri = URI.parse("#{Right::BASE_URL}/api/v1/entities/#{URI.encode_path(entity_id)}/documents")
headers = HTTP::Headers{"Authorization" => "Bearer #{Right.oauth.access_token}"}
uri = URI.parse("#{RightDocuments::BASE_URL}/api/v1/entities/#{URI.encode_path(entity_id)}/documents")
headers = HTTP::Headers{"Authorization" => "Bearer #{RightDocuments.oauth.access_token}"}
response = HTTP::Client.get(uri, headers: headers)
unless response.status.success?
output.puts "documents failed: HTTP #{response.status.code} — #{response.body}"
Expand All @@ -378,6 +379,34 @@ module Right
end
end

@[ACONA::AsCommand("documents:delete", description: "Delete a document by ID")]
class DocumentsDeleteCommand < ACON::Command
protected def configure : Nil
self.argument("id", :required, "document ID to delete")
end

protected def execute(input : ACON::Input::Interface, output : ACON::Output::Interface) : ACON::Command::Status
id = input.argument("id").to_s
if id.empty?
output.puts "error: document ID is required"
return ACON::Command::Status::FAILURE
end

RightDocuments.sdk_config
_, status, _ = RightDocuments::DocumentsApi.new.api_v1_documents_id_delete_with_http_info(id)
if status == 204
output.puts "deleted #{id}"
ACON::Command::Status::SUCCESS
else
output.puts "documents:delete failed: HTTP #{status}"
ACON::Command::Status::FAILURE
end
rescue ex
output.puts "documents:delete failed: #{ex.message}"
ACON::Command::Status::FAILURE
end
end

@[ACONA::AsCommand("import", description: "Import a PDF as an executed document")]
class ImportCommand < ACON::Command
include JSONOption
Expand Down Expand Up @@ -405,15 +434,15 @@ module Right
# The swagger doesn't yet describe the multipart body for import, so the
# SDK's import method takes only entity_id. Drop to direct HTTP until
# the swagger is fleshed out.
uri = URI.parse("#{Right::BASE_URL}/api/v1/entities/#{URI.encode_path(entity_id)}/documents/import")
uri = URI.parse("#{RightDocuments::BASE_URL}/api/v1/entities/#{URI.encode_path(entity_id)}/documents/import")
io = IO::Memory.new
builder = HTTP::FormData::Builder.new(io)
File.open(path) do |file|
builder.file("file", file, HTTP::FormData::FileMetadata.new(filename: File.basename(path)))
end
builder.finish
headers = HTTP::Headers{
"Authorization" => "Bearer #{Right.oauth.access_token}",
"Authorization" => "Bearer #{RightDocuments.oauth.access_token}",
"Content-Type" => builder.content_type,
}
response = HTTP::Client.post(uri, headers: headers, body: io.to_s)
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions src/rightdocuments/version.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module RightDocuments
CLI_VERSION = "0.3.6"
end