-
Notifications
You must be signed in to change notification settings - Fork 2
refactor: replace HTTParty with standard Ruby Net::HTTP #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'json' | ||
| require 'httparty' | ||
| require 'net/http' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switching from HTTParty to standard library Net::HTTP is good for reducing dependencies, but ensure you're handling all error cases properly with the new implementation. |
||
| require 'uri' | ||
| require_relative '../committer/config/accessor' | ||
|
|
||
| module Clients | ||
|
|
@@ -11,6 +12,8 @@ class OverloadError < StandardError; end | |
| class UnknownError < StandardError; end | ||
| class ConfigError < StandardError; end | ||
|
|
||
| API_ENDPOINT = 'https://api.anthropic.com/v1/messages' | ||
|
|
||
| def initialize | ||
| @config = Committer::Config::Accessor.instance | ||
|
|
||
|
|
@@ -38,19 +41,20 @@ def post(message) | |
| private | ||
|
|
||
| def send_request(body) | ||
| options = build_request_options(body) | ||
| HTTParty.post('https://api.anthropic.com/v1/messages', options) | ||
| end | ||
| uri = URI.parse(API_ENDPOINT) | ||
| http = Net::HTTP.new(uri.host, uri.port) | ||
| http.use_ssl = true | ||
|
|
||
| def build_request_options(body) | ||
| { | ||
| headers: { | ||
| 'anthropic-version': '2023-06-01', | ||
| 'content-type': 'application/json', | ||
| 'x-api-key': @config['api_key'] | ||
| }, | ||
| body: body.to_json | ||
| } | ||
| request = Net::HTTP::Post.new(uri.request_uri) | ||
| request['anthropic-version'] = '2023-06-01' | ||
| request['content-type'] = 'application/json' | ||
| request['x-api-key'] = @config['api_key'] | ||
| request.body = body.to_json | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return value has changed. The previous HTTParty.post returned a Response object with various methods, but now you're returning a parsed JSON hash. Ensure all callers of this method can handle the new return type. |
||
| response = http.request(request) | ||
| JSON.parse(response.body) | ||
| rescue JSON::ParserError | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding more specific error handling for different HTTP status codes (like 401, 403, 429, 500) to provide better error messages. |
||
| { 'type' => 'error', 'error' => { 'type' => 'unknown_error' } } | ||
| end | ||
|
|
||
| def handle_error_response(response) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing the httparty dependency here but still using it in the code will cause runtime errors. Make sure to update all usage of httparty or keep the dependency.