Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Latest commit

 

History

History
385 lines (279 loc) · 13.9 KB

transmission.wiki

File metadata and controls

385 lines (279 loc) · 13.9 KB

tags: egg transmission

== transmission

toc:

=== Description

An egg to work with the Transmission (v3.01) RPC protocol (v17). It assumes familiarity with the spec.

This egg may still work with a more recent version of the spec, but it's not guaranteed. An example of a change in the spec that doesn't require any change in the API is this:

<enscript> 17 | 3.01 | yes | torrent-get | new arg "file-count" 17 | 3.01 | yes | torrent-get | new arg "primary-mime-type" </enscript>

This is because the library doesn't check if the fields are valid (this is intentional).

These changes, however:

<enscript> 16 | 3.00 | yes | session-get | new request arg "fields" 16 | 3.00 | yes | torrent-get | new request arg "format" 16 | 3.00 | yes | torrent-set | new arg "labels" </enscript>

Require API changes, because otherwise the and arguments can't be used.

NOTE: This egg uses medea to read and write JSON, which by default reads object keys as symbols. This isn't great, so don't use this library with untrusted Transmission clients if you can avoid it. In the future the reader will be changed to read the keys as strings instead.

=== Author

siiky

=== Repository

https://git.sr.ht/~siiky/transmission.scm

=== Requirements

The following eggs are required for using this egg:

The following eggs are required for testing this egg:

=== API

==== module

===== Parameters

<parameter>*scheme*</parameter> <parameter>*host*</parameter> <parameter>*url*</parameter> <parameter>*port*</parameter> <parameter>*username*</parameter> <parameter>*password*</parameter> <parameter>*session-id*</parameter>

===== High-level RPC API

Every method of the spec is defined, and naming is followed almost directly. In the spec, all methods and most arguments follow the convention. The exceptions are a few arguments in -- these are converted to in this egg: e.g., the key argument for is called . Note, however, that the messages are left untouched: a message to/from the server will still use as the key, NOT .

Almost all required parameters are positional arguments in the library -- the only exception is the argument, which is always a key argument, even for methods with required , because it defaults to no IDs to avoid acting on torrents by accident.

All optional arguments in the spec are key arguments in the library.

<procedure>torrent-source/filename</procedure> <procedure>torrent-source/metainfo</procedure>

Create a torrent source to use with . A torrent can be added from a magnet URL; from a torrent file, given its path, which must be readable by the Transmission daemon; or the contents of the torrent file, encoded in Base64.

is used for magnets and file paths; is used for Base64 encoded torrent files.

====== Results

Every API procedure returns a , which is a SRFI-189 object. The most useful and most commonly used procedures for handling results are renamed and exported. On top of these, some others are defined.

<procedure>(true . _)</procedure> <procedure>(false . _)</procedure>

and return and respectively, independently of the arguments they're called with.

<procedure>(result/error obj ...)</procedure> <procedure>(result/error? obj)</procedure> <procedure>(result/ok obj ...)</procedure> <procedure>(result/ok? obj)</procedure> <procedure>(result? obj)</procedure> <procedure>(result-bind result mproc1 mproc2 ...)</procedure> <procedure>(result-ref result failure [success])</procedure> <procedure>(exception->result pred thunk)</procedure>

Renames of SRFI-189. The related functions are the related functions from SRFI-189; and the related functions are the related functions from SRFI-189.

<procedure>(result/error-ref result #!optional (fail false))</procedure> <procedure>(result/ok-ref result #!optional (fail false))</procedure>

If the object is of the expected alternative ( or ), return the values contained in it; otherwise, call on the values contained in the object, which by default, returns .

<procedure>(result-ref* result)</procedure>

Return the values contained in a result object, whether it's a or .

<procedure>(default-error-proc result/con #!optional tag req resp)</procedure> <procedure>(with-transmission-result result success-proc #!optional (error-proc default-error-proc))</procedure>

Convenient way to handle a single RPC call's result.

is similar to ( from SRFI-189), except that the order of the success and failure procedures is swapped. The failure procedure defaults to , which calls with a generic error message.

should be a procedure of 4 parameters: the and fields of a reply message, the request object, and the response object.

should be a procedure of 4 parameters, 3 of them optional: , , , and . and are the fields of a reply message, is the request object, and is the response object. , , and are either all false or none false. If they're false, is a object thrown during the API call; if they're not false, then is a string.

===== Low-level API

<macro>(make-rpc-call (method (required required-handler) ...) (key default key-handler) ...)</macro> <macro>(make-rpc-call method (key default key-handler) ...)</macro> <macro>(define-rpc-call (method required ...) key ...)</macro> <macro>(define-rpc-call method key ...)</macro> <macro>(export-rpc-call (method required ...) key ...)</macro> <macro>(export-rpc-call method key ...)</macro> <macro>(export-3.1/4.6 method)</macro>

creates a procedure to represent an RPC method.

is like but defines the created procedure.

is like but exports the defined procedure.

exports RPC procedures of sections 3.1 and 4.6 of the spec, which have a single optional argument.

is a method name, which will be used as the method name in messages and, in the case of , , and , the name of the defined procedure.

is the name of a required argument. It will not be used in messages.

is the name of an optional (key) argument. It will be used as the key name in the created procedure, but not in messages.

and are functions that will process a given value, to assure it has the right type/format, and return an entry ready to be inserted into the object of a message, i.e., they must return a pair that will later be converted to JSON.

is the default value of an optional (key) argument.

<procedure>(rpc-call method #!key (arguments #f) (tag #f))</procedure>

Make an RPC call. is the name of the RPC method, and is the object, containing both required and optional arguments.

<procedure>(handle-409 condition request message)</procedure> <procedure>(http-call request message)</procedure> <procedure>(update-request-session-id request #!optional (session-id (*session-id*)))</procedure> <procedure>(make-serialized-message method arguments tag)</procedure> <procedure>(make-message method arguments tag)</procedure> <procedure>(make-rpc-request host url port username password #!optional (session-id (*session-id*)))</procedure>

==== module

<syntax>(alist-let alist (formal ...) body ...)</syntax> <syntax>(alist-let/and alist (formal ...) body ...)</syntax> <syntax>(alist-let/nor alist (formal ...) body ...)</syntax>

Where is either , which will be used both as the key name and the variable, or , which will be the variable name and key respectively.

Equivalent to:

<enscript highlight="scheme"> (let ((var (alist-ref 'key alist)) ...) body ...) </enscript>

Except that, with , if is false, the whole expression evaluates to false; and with , if is false, the whole expression evaluates to true.

Example:

<enscript highlight="scheme"> (alist-let/and '((ant . 3) (bunny . 5) (Cat . 3)) (ant bunny (cat Cat)) (list ant bunny cat)) </enscript>

Expands to something like this:

<enscript highlight="scheme"> (let ((%alist '((ant . 3) (bunny . 5) (Cat . 3)))) (let ((ant (alist-ref 'ant %alist)) (bunny (alist-ref 'bunny %alist)) (cat (alist-ref 'Cat %alist))) (list ant bunny cat))) </enscript>

<procedure>(unique-tag! #!optional (new-n #f))</procedure>

Return an unique tag, that starts at 0 and is incremented by 1 on each call. If is given and is a , set the internal variable to for future use.

<constant>status/stopped</constant> <constant>status/check-wait</constant> <constant>status/check</constant> <constant>status/download-wait</constant> <constant>status/download</constant> <constant>status/seed-wait</constant> <constant>status/seed</constant>

The torrent status constants.

<constant>priority/low</constant> <constant>priority/normal</constant> <constant>priority/high</constant>

The priority constants -- for .

=== Example

The following example lists all the torrents that satisfy a certain predicate: they must be seeding, have an upload ratio greater than 1, and the download path must be (note that, to Transmission, and are not the same).

<enscript highlight="scheme"> (import srfi-1 transmission transmission.utils) (parameterize ((*host* "hostname") (*username* "username") (*password* "password")) (let ((tag (unique-tag!))) (with-transmission-result (torrent-get '("downloadDir" "id" "name" "status" "uploadRatio") #:ids #f #:tag tag) (lambda (arguments tag req resp) (define (want-torrent? tor) (alist-let/and tor (downloadDir status uploadRatio) (and (= status status/seed) (> uploadRatio 1) (string=? downloadDir "/some/path/")))) (alist-let/and arguments (torrents) (let ((wanted-tors (filter want-torrent? (vector->list torrents)))) (for-each print wanted-tors)))) ; NOTE: The error case handling procedure doesn't have to accept ; #!optional arguments -- the "missing" arguments will be #f. (lambda (result tag req resp) (error 'here "torrent-get call failed with the following error" result))))) </enscript>

For more examples take a look at https://git.sr.ht/~siiky/trmote (no longer in this egg's download directory). The example above is similar to from that repo.

=== License

  Anyone is free to copy, modify, publish, use, compile, sell, or
  distribute this software, either in source code form or as a compiled
  binary, for any purpose, commercial or non-commercial, and by any
  means.
  
  In jurisdictions that recognize copyright laws, the author or authors
  of this software dedicate any and all copyright interest in the
  software to the public domain. We make this dedication for the benefit
  of the public at large and to the detriment of our heirs and
  successors. We intend this dedication to be an overt act of
  relinquishment in perpetuity of all present and future rights to this
  software under copyright law.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
  
  For more information, please refer to <http://unlicense.org>

=== Version History

==== 0.3.0 (2023/12/28)

  • Add to .
==== 0.2.2 (2022/03/21)

  • Add parameter to support HTTPS as well.
==== 0.2.1 (2021/01/10)

  • now supports the case where the result was just a
  condition;

  • Add (hopefully) useful example programs to the directory -- you
  can get these from the repo or the {{chicken-install}} cache directory;

  • Export from .
==== 0.2.0 (2020/12/09)

  • Return a more functional object from API calls instead of a more
  C-like {{reply}} for easier error handling. There's no chance to mistake an
  error for a good result now;

  • Improve and to support optional variable
  names, and save the value resulting of evaluating the alist expression;

  anyway.

==== 0.1.1 (2020/12/02)

Add and update the documentation accordingly.

==== 0.1.0 (2020/12/01)

Initial release with all methods defined, and almost all tested.