aboutsummaryrefslogtreecommitdiff
path: root/web/gn-uri.scm
blob: 3455d51d14a5122135e3ce8dc5d8fa0b291a324d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
(define-module (web gn-uri)
  #:use-module (ice-9 string-fun)
  #:use-module (ice-9 match)
  #:use-module (ice-9 regex)

  #:export (get-version
            ;; url-parse-id
            ;; normalize-id
            ;; strip-lang
            mk-meta
            mk-data
            mk-doc
            mk-html
            mk-url
            mk-id
            mk-gnid
            mk-predicate
            prefix
            url-parse-id
            normalize-id))

(define (normalize-id str)
  ;; (string-replace-substring (string-downcase str) " " "_")
  (match str
    (#f "unknown")
    (_ (string-replace-substring str " " "_"))))

(define (url-parse-id uri)
  (if uri
      (car (reverse (string-split uri #\/))) "unknown"))

(define get-version
  "4.0.0")

(define base-url
  "http://localhost:8091")

(define uri-base-url
   ;always points to genenetwork.org!
  "http://genenetwork.org")

(define (prefix)
  "Build the API URL including version" base-url)

(define* (mk-url postfix
                 #:optional (ext ""))
  "Makes a fully qualified URL by adding the path (postfix+ext) to the API URL.
   If there is an existing http+hostname no prefix is added"
  (match (string-match "^http:" postfix)
    (#f (string-append (prefix) "/" postfix ext))
    (_ (string-append postfix ext))))

(define* (mk-uri postfix)
  "Add the path to the GN URI. A URI always points to http://genenetwork.org/"
  (string-append uri-base-url "/" postfix))

(define (mk-html path)
  "Create a pointer to API HTML documentation"
  (mk-url path ".html"))

(define (mk-doc path)
  "Create a pointer to API HTML documentation (doc infix)"
  (mk-html (string-append "doc/" path)))

(define (mk-meta path)
  "Create a meta URL for the API path"
  (mk-url path ".meta.json"))

(define (mk-data path)
  "Append .json for the API path. If it is not a full URL it will prepend the host"
  (mk-url path ".json"))

(define (mk-term postfix)
  (mk-html (string-append "term" "/" postfix)))

(define (mk-predicate postfix)
  (mk-html (string-append "predicate" "/" postfix)))

(define (mk-id postfix)
  "Expand URL to make $api/id/identifier.
   If postfix is a path it will only apply the last element"
  (mk-url (string-append "id" "/"
                         (url-parse-id postfix))))

(define (mk-gnid postfix)
  "Expand URL to make http://genenetwork.org/id/identifier.
   If postfix is a path it will only apply the last element"
  (mk-uri (string-append "id" "/"
                         (url-parse-id postfix))))