Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Removed the Net::Http based connection interface, eventmachine is now…

… required, added authentication
  • Loading branch information...
commit 4807c9d1297772e5c8f4de08956219868273eeda 1 parent ad7e2af
@dsander authored
View
2  README.markdown 100755 → 100644
@@ -1,5 +1,7 @@
# transmission-client: A Transmission RPC Client
+**Warning - The next release will drop support for the blocking api. Eventmachine will be required by then.**
+
The goal is to support all requests described in the Transmission [RPC Specifications](http://trac.transmissionbt.com/browser/trunk/doc/rpc-spec.txt).
## Installing
View
49 README.rdoc
@@ -1,49 +0,0 @@
-# transmission-client: A Transmission RPC Client
-
-The goal is to support all requests described in the Transmission [RPC Specifications](http://trac.transmissionbt.com/browser/trunk/doc/rpc-spec.txt).
-
-## Installing
-You need to have http://gemcutter.org in you gem sources. To add it you can execute either
-
- sudo gem install gemcutter
- sudo gem tumble
-
-or
-
- sudo gem source -a http://gemcutter.org
-
-To install transmission-client:
-
- sudo gem install transmission-client
-
-If you want to use EventMachine (optional) you need to install the eventmachine gem and igrigorik's em-http-request:
-
- sudo gem install eventmachine
- sudo gem install em-http-request
-
-## Usage
-Get a list of torrents and print its file names:
-
- require 'transmission-client'
- t = Transmission::Client.new('127.0.0.1', 9091)
- t.torrents.each do |torrent|
- puts torrent.name
- end
-
-To use the EventMachine driven interface:
-
- require 'eventmachine'
- require 'transmission-client'
-
- EventMachine.run do
- t = Transmission::Client.new
- EM.add_periodic_timer(1) do
- t.torrents do |torrents|
- torrents.each do |tor|
- puts tor.percentDone
- end
- end
- end
- end
-
-RDoc is still to be written, at the meantime have a look at the code to find out which methods are supported.
View
1  Rakefile 100755 → 100644
@@ -10,6 +10,7 @@ begin
gem.email = "git@dsander.de"
gem.homepage = "http://github.com/dsander/transmission-client"
gem.authors = ["Dominik Sander"]
+ gem.add_dependency "em-http-request"
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
gem.files += Dir['lib/**/*.rb','README.markdown']
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
View
13 lib/transmission-client.rb 100755 → 100644
@@ -5,17 +5,8 @@
$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
require 'transmission-client/client'
-
-if defined? EM
- begin
- require 'em-http'
- require 'transmission-client/em-connection'
- rescue LoadError
- require 'transmission-client/connection'
- end
-else
- require 'transmission-client/connection'
-end
+require 'em-http'
+require 'transmission-client/em-connection'
require 'transmission-client/torrent'
require 'transmission-client/session'
View
65 lib/transmission-client/client.rb 100755 → 100644
@@ -1,71 +1,60 @@
module Transmission
class Client
- def initialize(host='localhost',port=9091)
- Connection.instance.init(host, port)
+ def initialize(host='localhost',port=9091, username = nil, password = nil)
+ Connection.init(host, port, username, password)
end
def start_all &cb
- Connection.instance.send('torrent-start', &cb)
+ Connection.send('torrent-start')
end
- def start(id, &cb)
- Connection.instance.send('torrent-start', {'ids' => id.class == Array ? id : [id]}, &cb)
+ def start(id)
+ Connection.send('torrent-start', {'ids' => id.class == Array ? id : [id]})
end
- def stop(id, &cb)
- Connection.instance.send('torrent-stop', {'ids' => id.class == Array ? id : [id]}, &cb)
+ def stop(id)
+ Connection.send('torrent-stop', {'ids' => id.class == Array ? id : [id]})
end
def stop_all &cb
- Connection.instance.send('torrent-stop', &cb)
+ Connection.send('torrent-stop')
end
- def remove(id, delete_data = false, &cb)
- Connection.instance.send('torrent-remove', {'ids' => id.class == Array ? id : [id], 'delete-local-data' => delete_data }, &cb)
+ def remove(id, delete_data = false)
+ Connection.send('torrent-remove', {'ids' => id.class == Array ? id : [id], 'delete-local-data' => delete_data })
end
- def remove_all(delete_data = false, &cb)
- Connection.instance.send('torrent-remove', {'delete-local-data' => delete_data }, &cb)
+ def remove_all(delete_data = false)
+ Connection.send('torrent-remove', {'delete-local-data' => delete_data })
end
- def add_torrent(a, &cb)
+ def add_torrent(a)
if a['filename'].nil? && a['metainfo'].nil?
raise "You need to provide either a 'filename' or 'metainfo'."
end
- Connection.instance.send('torrent-add', a, &cb)
+ Connection.send('torrent-add', a)
end
- def add_torrent_by_file(filename, &cb)
- add_torrent({'filename' => filename}, &cb)
+ def add_torrent_by_file(filename)
+ add_torrent({'filename' => filename})
end
- def add_torrent_by_data(data, &cb)
- add_torrent({'metainfo' => data}, &cb)
+ def add_torrent_by_data(data)
+ add_torrent({'metainfo' => data})
end
- def session &cb
- if cb
- Connection.instance.request('session-get') { |resp| cb.call Session.new resp }
- else
- Session.new Connection.instance.request('session-get')
- end
+ def session
+ Connection.request('session-get') { |resp| yield Session.new resp }
end
- def torrents(fields = nil, &cb)
+ def torrents(fields = nil)
torrs = []
- if cb
- Connection.instance.request('torrent-get', {'fields' => fields ? fields : Transmission::Torrent::ATTRIBUTES}) { |resp|
- resp['torrents'].each do |t|
- torrs << Torrent.new(t)
- end
- cb.call torrs
- }
- else
- Connection.instance.request('torrent-get', {'fields' => fields ? fields : Transmission::Torrent::ATTRIBUTES})['torrents'].each do |t|
- torrs << Torrent.new(t)
- end
- torrs
- end
+ Connection.request('torrent-get', {'fields' => fields ? fields : Transmission::Torrent::ATTRIBUTES}) { |resp|
+ resp['torrents'].each do |t|
+ torrs << Torrent.new(t)
+ end
+ yield torrs
+ }
end
end
end
View
42 lib/transmission-client/connection.rb
@@ -1,42 +0,0 @@
-module Transmission
- class Connection
- include Singleton
-
- def init(host, port)
- @host = host
- @port = port
- uri = URI.parse("http://#{@host}:#{@port}")
- @conn = Net::HTTP.start(uri.host, uri.port)
- @header = {}
- end
-
- def request(method, attributes={})
- res = @conn.post('/transmission/rpc',build_json(method,attributes),@header)
- if res.class == Net::HTTPConflict && @header['x-transmission-session-id'].nil?
- @header['x-transmission-session-id'] = res['x-transmission-session-id']
- request(method,attributes)
- elsif res.class == Net::HTTPOK
- resp = JSON.parse(res.body)
- if resp["result"] == 'success'
- #pp resp
- resp['arguments']
- else
- resp
- end
- end
- end
-
- def send(method, attributes={})
- request(method, attributes)['result'].nil?
- end
-
- def build_json(method,attributes = {})
- if attributes.length == 0
- {'method' => method}.to_json
- else
- {'method' => method, 'arguments' => attributes }.to_json
- end
- end
-
- end
-end
View
86 lib/transmission-client/em-connection.rb 100755 → 100644
@@ -1,51 +1,53 @@
module Transmission
class Connection
- include Singleton
- #include EM::Deferrable
-
- def init(host, port)
- @host = host
- @port = port
- uri = URI.parse("http://#{@host}:#{@port}/transmission/rpc")
- #@conn = Net::HTTP.start(uri.host, uri.port)
- @conn = EventMachine::HttpRequest.new(uri)
- @header = {} #{"Accept-Encoding" => "deflate"} deflate is broken somewhere
- end
-
- def request(method, attributes={}, &cb)
- req = @conn.post(:body => build_json(method,attributes), :head => @header )
- req.callback {
- if req.response_header.status == 409 #&& @header['x-transmission-session-id'].nil?
- @header['x-transmission-session-id'] = req.response_header['X_TRANSMISSION_SESSION_ID']
- request(method,attributes, &cb)
- elsif req.response_header.status == 200
- resp = JSON.parse(req.response)
- if resp["result"] == 'success'
- cb.call resp['arguments'] if cb
- else
- cb.call resp if cb
+ class <<self
+ def init(host, port, username = nil, password = nil)
+ @host = host
+ @port = port
+ @header = username.nil? ? {} : {'authorization' => [username, password]}
+ uri = URI.parse("http://#{@host}:#{@port}/transmission/rpc")
+ @conn = EventMachine::HttpRequest.new(uri)
+ end
+
+ def request(method, attributes={})
+ req = @conn.post(:body => build_json(method,attributes), :head => @header )
+ req.callback {
+ case req.response_header.status
+ when 401
+ raise SecurityError, 'The client was not able to authenticate, is your username or password wrong?'
+ when 409 #&& @header['x-transmission-session-id'].nil?
+ @header['x-transmission-session-id'] = req.response_header['X_TRANSMISSION_SESSION_ID']
+ request(method,attributes) do |resp|
+ yield resp
+ end
+ when 200
+ resp = JSON.parse(req.response)
+ if resp["result"] == 'success'
+ yield resp['arguments']
+ else
+ yield resp
+ end
end
+ }
+ req.errback {
+ raise "Unknown response."
+ }
+ end
+
+ def send(method, attributes={})
+ request(method, attributes) do |resp|
+ yield resp
end
- }
- req.errback {
- puts 'errback'
- pp req
- }
- end
-
- def send(method, attributes={}, &cb)
- request(method, attributes) do |resp|
- cb.call resp if cb
end
- end
-
- def build_json(method,attributes = {})
- if attributes.length == 0
- {'method' => method}.to_json
- else
- {'method' => method, 'arguments' => attributes }.to_json
+
+ def build_json(method,attributes = {})
+ if attributes.length == 0
+ {'method' => method}.to_json
+ else
+ {'method' => method, 'arguments' => attributes }.to_json
+ end
end
+
end
-
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.