Changeset 31831 in osm


Ignore:
Timestamp:
2015-12-15T20:51:34+01:00 (9 years ago)
Author:
floscher
Message:

[mapillary] Document MapillaryURL and add tests for this class

Location:
applications/editors/josm/plugins/mapillary
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/utils/MapillaryURL.java

    r31828 r31831  
     1// License: GPL. For details, see LICENSE file.
    12package org.openstreetmap.josm.plugins.mapillary.utils;
    23
     
    2324  }
    2425
     26  /**
     27   * Gives you the URL for the online editor of a specific mapillary image.
     28   * @param key the key of the image to which you want to link
     29   * @return the URL of the online editor for the image with the given image key
     30   */
    2531  public static URL browseEditURL(String key) {
    2632    if (key == null || !key.matches("[a-zA-Z0-9\\-_]{22}")) {
     
    3036  }
    3137
     38  /**
     39   * Gives you the URL for the online viewer of a specific mapillary image.
     40   * @param key the key of the image to which you want to link
     41   * @return the URL of the online viewer for the image with the given image key
     42   */
    3243  public static URL browseImageURL(String key) {
    3344    if (key == null || !key.matches("[a-zA-Z0-9\\-_]{22}")) {
     
    3748  }
    3849
     50  /**
     51   * @return the URL where the user can view all uploaded images that are not yet published
     52   */
    3953  public static URL browseUploadImageURL() {
    40     return string2URL(BASE_WEBSITE_URL + "map/upload/im");
     54    return string2URL(BASE_WEBSITE_URL + "map/upload/im/");
    4155  }
    4256
     57  /**
     58   * Gives you the URL which the user should visit to initiate the OAuth authentication process
     59   * @param redirectURI the URI to which the user will be redirected when the authentication is finished
     60   * @return the URL that the user should visit to start the OAuth authentication
     61   */
    4362  public static URL connectURL(String redirectURI) {
    4463    HashMap<String, String> parts = new HashMap<>();
    45     parts.put("redirect_uri", redirectURI);
     64    if (redirectURI != null && redirectURI.length() >= 1) {
     65      parts.put("redirect_uri", redirectURI);
     66    }
    4667    parts.put("response_type", "token");
    4768    parts.put("scope", "user:read public:upload public:write");
     
    4970  }
    5071
     72  /**
     73   * Gives you the API-URL where you get 20 images within the given bounds.
     74   * For more than 20 images you have to use different URLs with different page numbers.
     75   * @param bounds the bounds in which you want to search for images
     76   * @param page number of the page to retrieve from the API
     77   * @return the API-URL which gives you the images in the given bounds as JSON
     78   */
    5179  public static URL searchImageURL(Bounds bounds, int page) {
    5280    HashMap<String, String> parts = new HashMap<>();
     
    5785  }
    5886
     87  /**
     88   * Gives you the API-URL where you get 10 sequences within the given bounds.
     89   * For more than 10 sequences you have to use different URLs with different page numbers.
     90   * @param bounds the bounds in which you want to search for sequences
     91   * @param page number of the page to retrieve from the API
     92   * @return the API-URL which gives you the sequences in the given bounds as JSON
     93   */
    5994  public static URL searchSequenceURL(Bounds bounds, int page) {
    6095    HashMap<String, String> parts = new HashMap<>();
     
    65100  }
    66101
     102  /**
     103   * Gives you the API-URL where you get the traffic signs for 20 images within the given bounds.
     104   * For the signs from more than 20 images you have to use different URLs with different page numbers.
     105   * @param bounds the bounds in which you want to search for traffic signs
     106   * @param page number of the page to retrieve from the API
     107   * @return the API-URL which gives you the traffic signs in the given bounds as JSON
     108   */
    67109  public static URL searchTrafficSignURL(Bounds bounds, int page) {
    68110    HashMap<String, String> parts = new HashMap<>();
     
    73115  }
    74116
     117  /**
     118   * @return the URL where you'll find the upload secrets as JSON
     119   */
    75120  public static URL uploadSecretsURL() {
    76121    return string2URL(BASE_API_URL + "me/uploads/secrets/" + queryString(null));
    77122  }
    78123
     124  /**
     125   * @return the URL where you'll find information about the user account as JSON
     126   */
    79127  public static URL userURL() {
    80128    return string2URL(BASE_API_URL + "me/" + queryString(null));
    81129  }
    82130
     131  /**
     132   * Adds the given {@link Bounds} to a {@link Map} that contains the parts of a query string.
     133   * @param parts the parts of a query string
     134   * @param bounds the bounds that will be added to the query string
     135   */
    83136  private static void putBoundsInQueryStringParts(Map<String, String> parts, Bounds bounds) {
    84     parts.put("min_lat", String.format(Locale.UK, "%f", bounds.getMin().lat()));
    85     parts.put("max_lat", String.format(Locale.UK, "%f", bounds.getMax().lat()));
    86     parts.put("min_lon", String.format(Locale.UK, "%f", bounds.getMin().lon()));
    87     parts.put("max_lon", String.format(Locale.UK, "%f", bounds.getMax().lon()));
     137    if (bounds != null) {
     138      parts.put("min_lat", String.format(Locale.UK, "%f", bounds.getMin().lat()));
     139      parts.put("max_lat", String.format(Locale.UK, "%f", bounds.getMax().lat()));
     140      parts.put("min_lon", String.format(Locale.UK, "%f", bounds.getMin().lon()));
     141      parts.put("max_lon", String.format(Locale.UK, "%f", bounds.getMax().lon()));
     142    }
    88143  }
    89144
     145  /**
     146   * Builds a query string from it's parts that are supplied as a {@link Map}
     147   * @param parts the parts of the query string
     148   * @return the constructed query string (including a leading ?)
     149   */
    90150  private static String queryString(Map<String, String> parts) {
    91     StringBuilder ret = new StringBuilder().append("?client_id=").append(CLIENT_ID);
     151    StringBuilder ret = new StringBuilder("?client_id=").append(CLIENT_ID);
    92152    if (parts != null) {
    93153      for (Entry<String, String> entry : parts.entrySet()) {
     
    105165  }
    106166
     167  /**
     168   * Converts a {@link String} into a {@link URL} without throwing a {@link MalformedURLException}.
     169   * Instead such an exception will lead to an {@link Main#error(Throwable)}.
     170   * So you should be very confident that your URL is well-formed when calling this method.
     171   * @param string the String describing the URL
     172   * @return the URL that is constructed from the given string
     173   */
    107174  private static URL string2URL(String string) {
    108175    try {
    109176      return new URL(string);
    110177    } catch (MalformedURLException e) {
    111       Main.error("The MapillaryAPI class produces malformed URLs!", e);
     178      Main.error(new Exception("The "+MapillaryURL.class.getSimpleName()+" class produces malformed URLs!", e));
    112179      return null;
    113180    }
Note: See TracChangeset for help on using the changeset viewer.