Skip to content

Commit

Permalink
Update SDK's handling of errors from API (#3)
Browse files Browse the repository at this point in the history
* Update SDKs handling of errors from API; add 'local' environment

* Update rexml dependency with high severity vulnerabilities to 3.2.5

Co-authored-by: Carl Scheller <[email protected]>
  • Loading branch information
cjscheller and Carl Scheller authored Nov 14, 2021
1 parent 16bd75e commit ab3ee59
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ GEM
multipart-post (2.1.1)
public_suffix (4.0.6)
rake (13.0.6)
rexml (3.2.4)
rexml (3.2.5)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Or install it yourself as:
## Preparation

- Create an account on [DigitalHumani,com](https://my.digitalhumani.com/register) and grab your API key from the **Developer** menu item.
- Note that you'll use a different API key per environment (production vs sandbox)
- Review the [DigitalHumani documentation](https://docs.digitalhumani.com) to familiarize yourself with the environments, requests, projects, etc

## Usage
Expand Down
18 changes: 16 additions & 2 deletions lib/digitalhumani.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def initialize(&block)
if !@api_key or @api_key === ""
raise "Error: @api_key parameter required"
end
if !@environment or !["production","sandbox"].include?(@environment)
if !@environment or !["production","sandbox","local"].include?(@environment)
raise "Error: @environment parameter required and must be one of 'production','sandbox'"
end

Expand All @@ -29,6 +29,8 @@ def initialize(&block)
@url = "https://api.digitalhumani.com/"
when "sandbox"
@url = "https://api.sandbox.digitalhumani.com"
when "local"
@url = "http://localhost:3000"
end
end

Expand Down Expand Up @@ -126,7 +128,19 @@ def client
def request(http_method:, endpoint:, params: {})
params = JSON.generate(params) if !params.empty? and http_method != :get
response = client.public_send(http_method, endpoint, params)
JSON.parse(response.body)

if response.status === 200
return JSON.parse(response.body)
elsif response.status === 401 or response.status === 403
raise response.status.to_s + " " + response.body
else
if response.headers.include?("content-type") and response.headers["content-type"].start_with?("application/json")
json = JSON.parse(response.body)
raise response.status.to_s + " | " + json['message']
else
raise response.status.to_s + " | " + response.body
end
end
end

end
Expand Down
74 changes: 74 additions & 0 deletions spec/digitalhumani_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,78 @@
end
end

describe "DigitalHumani API | Error handling" do
before(:each) do
@dh = DigitalHumani::SDK.new do
@api_key = "$API_KEY"
@environment = "sandbox"
@enterprise_id = "$ENTERPRISE_ID"
end
end

it "handles 400" do
# Stub network request with 400 error
stub_request(:any, /api.sandbox.digitalhumani.com/).
with(headers: {'Content-Type'=>'application/json', 'X-API-KEY'=>'$API_KEY'}).
to_return(status: 400, body: '{ "message": "Test Error" }', headers: {
"content-type": "application/json"
})

expect{
project = @dh.project(project_id: 'invalid')
}.to raise_error(RuntimeError, "400 | Test Error")
end

it "handles 401" do
# Stub network request with 400 error
stub_request(:any, /api.sandbox.digitalhumani.com/).
with(headers: {'Content-Type'=>'application/json', 'X-API-KEY'=>'$API_KEY'}).
to_return(status: 401, body: 'Unauthorized', headers: {
"content-type": "application/json"
})

expect{
project = @dh.project(project_id: 'invalid')
}.to raise_error(RuntimeError, "401 Unauthorized")
end

it "handles 403" do
# Stub network request with 400 error
stub_request(:any, /api.sandbox.digitalhumani.com/).
with(headers: {'Content-Type'=>'application/json', 'X-API-KEY'=>'$API_KEY'}).
to_return(status: 403, body: 'Forbidden', headers: {
"content-type": "application/json"
})

expect{
project = @dh.project(project_id: 'invalid')
}.to raise_error(RuntimeError, "403 Forbidden")
end

it "handles 404" do
# Stub network request with 400 error
stub_request(:any, /api.sandbox.digitalhumani.com/).
with(headers: {'Content-Type'=>'application/json', 'X-API-KEY'=>'$API_KEY'}).
to_return(status: 404, body: '{ "message": "Test Error" }', headers: {
"content-type": "application/json"
})

expect{
project = @dh.project(project_id: 'invalid')
}.to raise_error(RuntimeError, "404 | Test Error")
end

it "handles 500" do
# Stub network request with 400 error
stub_request(:any, /api.sandbox.digitalhumani.com/).
with(headers: {'Content-Type'=>'application/json', 'X-API-KEY'=>'$API_KEY'}).
to_return(status: 500, body: '{ "message": "Test Error" }', headers: {
"content-type": "application/json"
})

expect{
project = @dh.project(project_id: 'invalid')
}.to raise_error(RuntimeError, "500 | Test Error")
end
end
end

0 comments on commit ab3ee59

Please sign in to comment.