├── .travis.yml ├── gradle.properties ├── settings.gradle ├── .gitignore ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── src ├── test │ ├── resources │ │ └── ovh.conf │ └── java │ │ └── com │ │ └── ovh │ │ └── api │ │ └── test │ │ ├── ApiTest.java │ │ └── EndpointsTest.java └── main │ └── java │ └── com │ └── ovh │ └── api │ ├── OvhApiException.java │ └── OvhApi.java ├── LICENSE ├── gradlew.bat ├── CONTRIBUTING.md ├── gradlew └── README.rst /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | org.gradle.daemon=true -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | 2 | rootProject.name = 'java-ovh' 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /build/ 2 | /.gradle/ 3 | /ovh.conf 4 | /.settings/ 5 | /bin/ 6 | /.classpath 7 | /.project 8 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ovh/java-ovh/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /src/test/resources/ovh.conf: -------------------------------------------------------------------------------- 1 | endpoint=ovh-eu 2 | application_key=0000000000000000 3 | application_secret=00000000000000000000000000000000 4 | consumer_key=00000000000000000000000000000000 -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Apr 22 17:45:10 CEST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.11-bin.zip 7 | -------------------------------------------------------------------------------- /src/main/java/com/ovh/api/OvhApiException.java: -------------------------------------------------------------------------------- 1 | package com.ovh.api; 2 | 3 | public class OvhApiException extends Exception { 4 | 5 | public enum OvhApiExceptionCause { 6 | CONFIG_ERROR, 7 | INTERNAL_ERROR, 8 | RESSOURCE_NOT_FOUND, 9 | RESSOURCE_CONFLICT_ERROR, 10 | BAD_PARAMETERS_ERROR, 11 | AUTH_ERROR, 12 | API_ERROR; 13 | }; 14 | 15 | private final OvhApiExceptionCause ovhCause; 16 | 17 | public OvhApiException(String message, OvhApiExceptionCause ovhCause) { 18 | super(message); 19 | this.ovhCause = ovhCause; 20 | } 21 | 22 | public OvhApiExceptionCause getOvhCause() { 23 | return ovhCause; 24 | } 25 | 26 | @Override 27 | public String toString() { 28 | return "OvhApiException [ovhCause=" + ovhCause + "] : " + getLocalizedMessage(); 29 | } 30 | 31 | 32 | } 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013-2016, OVH SAS. 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of OVH SAS nor the 13 | names of its contributors may be used to endorse or promote products 14 | derived from this software without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY OVH SAS AND CONTRIBUTORS ``AS IS'' AND ANY 17 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL OVH SAS AND CONTRIBUTORS BE LIABLE FOR ANY 20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /src/test/java/com/ovh/api/test/ApiTest.java: -------------------------------------------------------------------------------- 1 | package com.ovh.api.test; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.net.HttpURLConnection; 6 | import java.net.URL; 7 | import java.nio.charset.StandardCharsets; 8 | 9 | import org.junit.Assert; 10 | import org.junit.Before; 11 | import org.junit.Test; 12 | import org.junit.runner.RunWith; 13 | import org.mockito.Mockito; 14 | import org.powermock.api.mockito.PowerMockito; 15 | import org.powermock.core.classloader.annotations.PrepareForTest; 16 | import org.powermock.modules.junit4.PowerMockRunner; 17 | 18 | import com.google.gson.Gson; 19 | import com.ovh.api.OvhApi; 20 | 21 | @RunWith(PowerMockRunner.class) 22 | @PrepareForTest({OvhApi.class, System.class}) 23 | public class ApiTest { 24 | 25 | HttpURLConnection mockCon; 26 | 27 | String me = "{\"firstname\":\"Foo\",\"vat\":\"\",\"ovhSubsidiary\":\"FR\",\"area\":\"\",\"birthDay\":\"Invalid date\",\"nationalIdentificationNumber\":null,\"spareEmail\":null,\"ovhCompany\":\"ovh\",\"state\":\"complete\",\"email\":\"test@foobar.com\",\"currency\":{\"symbol\":\"€\",\"code\":\"EUR\"},\"city\":\"Roubaix\",\"fax\":\"\",\"nichandle\":\"fb0000-ovh\",\"address\":\"1 rue du Foobar\",\"companyNationalIdentificationNumber\":null,\"birthCity\":\"\",\"country\":\"FR\",\"language\":\"fr_FR\",\"organisation\":\"\",\"name\":\"Bar\",\"phone\":\"+33.000000000\",\"sex\":\"male\",\"zip\":\"59000\",\"corporationType\":\"\",\"legalform\":\"individual\"}"; 28 | 29 | @Before 30 | public void setup() throws Exception { 31 | mockCon = Mockito.mock(HttpURLConnection.class); 32 | 33 | URL mockedUrl = PowerMockito.mock(URL.class); 34 | PowerMockito.whenNew(URL.class).withArguments(Mockito.anyString()).thenReturn(mockedUrl); 35 | PowerMockito.when(mockedUrl.openConnection()).thenReturn(mockCon); 36 | } 37 | 38 | private void setupResp(String resp, int respCode) throws Exception { 39 | InputStream inputStrm = new ByteArrayInputStream(resp.getBytes(StandardCharsets.UTF_8)); 40 | Mockito.when(mockCon.getInputStream()).thenReturn(inputStrm); 41 | 42 | Mockito.when(mockCon.getResponseCode()).thenReturn(respCode); 43 | } 44 | 45 | @Test 46 | public void me() throws Exception { 47 | String endpoint = "ovh-eu"; 48 | String appKey = "000000000000000"; 49 | String appSecret = "00000000000000000000000000000000"; 50 | String consumerKey = "00000000000000000000000000000000"; 51 | OvhApi api = new OvhApi(endpoint, appKey, appSecret, consumerKey); 52 | 53 | setupResp(me,200); 54 | 55 | String json = api.get("/me"); 56 | Gson gson = new Gson(); 57 | Me me = gson.fromJson(json, Me.class); 58 | 59 | Assert.assertEquals(me.firstname, "Foo"); 60 | Assert.assertEquals(me.name, "Bar"); 61 | Assert.assertEquals(me.nichandle, "fb0000-ovh"); 62 | } 63 | 64 | public class Me { 65 | public String firstname; 66 | public String name; 67 | public String nichandle; 68 | 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/test/java/com/ovh/api/test/EndpointsTest.java: -------------------------------------------------------------------------------- 1 | package com.ovh.api.test; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.net.HttpURLConnection; 6 | import java.net.URL; 7 | import java.nio.charset.StandardCharsets; 8 | 9 | import org.junit.Before; 10 | import org.junit.Test; 11 | import org.junit.runner.RunWith; 12 | import org.mockito.Mockito; 13 | import org.powermock.api.mockito.PowerMockito; 14 | import org.powermock.core.classloader.annotations.PrepareForTest; 15 | import org.powermock.modules.junit4.PowerMockRunner; 16 | 17 | import com.ovh.api.OvhApi; 18 | 19 | @RunWith(PowerMockRunner.class) 20 | @PrepareForTest(OvhApi.class) 21 | public class EndpointsTest { 22 | 23 | @Before 24 | public void setup() throws Exception { 25 | HttpURLConnection mockCon = Mockito.mock(HttpURLConnection.class); 26 | InputStream inputStrm = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8)); 27 | Mockito.when(mockCon.getInputStream()).thenReturn(inputStrm); 28 | Mockito.when(mockCon.getResponseCode()).thenReturn(200); 29 | 30 | URL mockedUrl = PowerMockito.mock(URL.class); 31 | PowerMockito.whenNew(URL.class).withArguments(Mockito.anyString()).thenReturn(mockedUrl); 32 | PowerMockito.when(mockedUrl.openConnection()).thenReturn(mockCon); 33 | } 34 | 35 | @Test 36 | public void raw() throws Exception { 37 | OvhApi api = new OvhApi("https://foo.bar", "", "", ""); 38 | api.get("/me"); 39 | PowerMockito.verifyNew(URL.class).withArguments("https://foo.bar/me"); 40 | } 41 | 42 | 43 | @Test 44 | public void ovhEu() throws Exception { 45 | OvhApi api = new OvhApi("ovh-eu", "", "", ""); 46 | api.get("/me"); 47 | PowerMockito.verifyNew(URL.class).withArguments("https://eu.api.ovh.com/1.0/me"); 48 | } 49 | 50 | 51 | @Test 52 | public void ovhCa() throws Exception { 53 | OvhApi api = new OvhApi("ovh-ca", "", "", ""); 54 | api.get("/me"); 55 | PowerMockito.verifyNew(URL.class).withArguments("https://ca.api.ovh.com/1.0/me"); 56 | } 57 | 58 | @Test 59 | public void kimsufiEu() throws Exception { 60 | OvhApi api = new OvhApi("kimsufi-eu", "", "", ""); 61 | api.get("/me"); 62 | PowerMockito.verifyNew(URL.class).withArguments("https://eu.api.kimsufi.com/1.0/me"); 63 | } 64 | 65 | @Test 66 | public void kimsufiCa() throws Exception { 67 | OvhApi api = new OvhApi("kimsufi-ca", "", "", ""); 68 | api.get("/me"); 69 | PowerMockito.verifyNew(URL.class).withArguments("https://ca.api.kimsufi.com/1.0/me"); 70 | } 71 | 72 | @Test 73 | public void soyoustartEu() throws Exception { 74 | OvhApi api = new OvhApi("soyoustart-eu", "", "", ""); 75 | api.get("/me"); 76 | PowerMockito.verifyNew(URL.class).withArguments("https://eu.api.soyoustart.com/1.0/me"); 77 | } 78 | 79 | @Test 80 | public void soyoustartCa() throws Exception { 81 | OvhApi api = new OvhApi("soyoustart-ca", "", "", ""); 82 | api.get("/me"); 83 | PowerMockito.verifyNew(URL.class).withArguments("https://ca.api.soyoustart.com/1.0/me"); 84 | } 85 | 86 | @Test 87 | public void runabove() throws Exception { 88 | OvhApi api = new OvhApi("runabove", "", "", ""); 89 | api.get("/me"); 90 | PowerMockito.verifyNew(URL.class).withArguments("https://api.runabove.com/1.0/me"); 91 | } 92 | 93 | @Test 94 | public void runaboveCa() throws Exception { 95 | OvhApi api = new OvhApi("runabove-ca", "", "", ""); 96 | api.get("/me"); 97 | PowerMockito.verifyNew(URL.class).withArguments("https://api.runabove.com/1.0/me"); 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to java-ovh 2 | 3 | ## Submitting Modifications: 4 | 5 | So you want to contribute you work? Awesome! We are eager to review it. 6 | To submit your contribution, you must use Github Pull Requests. Your work 7 | does not need to be fully polished before submiting it. Actually, we love 8 | helping people writing a great contribution. Hence, if you are wondering 9 | how to integrate a specific change, feel free to start a discussion in 10 | a Pull Request. 11 | 12 | Before we can actually accept and merge a Pull Request, it will need 13 | to follow the conding guidelines (see below), and each commit shall be 14 | signed to indicate your full agreement with these guidelines and the 15 | DCO (see below). 16 | 17 | To sign a commit, you may use a command like: 18 | 19 | ``` 20 | # New commit 21 | git commit -s 22 | 23 | # Previous commit 24 | git commit --amend -s 25 | ``` 26 | 27 | If a Pull Request can not be automatically merged, you will probably need 28 | to "rebase" your work on latest project update: 29 | 30 | ``` 31 | # Assuming, this project remote is registered as "upstream" 32 | git fetch upstream 33 | git rebase upstream/master 34 | ``` 35 | 36 | ## Contribution guidelines 37 | 38 | 1. your code must follow the coding style rules (see below) 39 | 2. your code must be documented 40 | 3. you code must be tested 41 | 4. your work must be signed (see "Developer Certificate of Origin" below) 42 | 5. you may contribute through GitHub Pull Requests 43 | 44 | ## Licensing for new files: 45 | 46 | java-ovh is licensed under a (modified) BSD license. Anything contributed to 47 | java-ovh must be released under this license. 48 | 49 | When introducing a new file into the project, please make sure it has a 50 | copyright header making clear under which license it''s being released. 51 | 52 | ## Developer Certificate of Origin: 53 | 54 | ``` 55 | To improve tracking of contributions to this project we will use a 56 | process modeled on the modified DCO 1.1 and use a "sign-off" procedure 57 | on patches that are being contributed. 58 | 59 | The sign-off is a simple line at the end of the explanation for the 60 | patch, which certifies that you wrote it or otherwise have the right 61 | to pass it on as an open-source patch. The rules are pretty simple: 62 | if you can certify the below: 63 | 64 | By making a contribution to this project, I certify that: 65 | 66 | (a) The contribution was created in whole or in part by me and I have 67 | the right to submit it under the open source license indicated in 68 | the file; or 69 | 70 | (b) The contribution is based upon previous work that, to the best of 71 | my knowledge, is covered under an appropriate open source License 72 | and I have the right under that license to submit that work with 73 | modifications, whether created in whole or in part by me, under 74 | the same open source license (unless I am permitted to submit 75 | under a different license), as indicated in the file; or 76 | 77 | (c) The contribution was provided directly to me by some other person 78 | who certified (a), (b) or (c) and I have not modified it. 79 | 80 | (d) The contribution is made free of any other party''s intellectual 81 | property claims or rights. 82 | 83 | (e) I understand and agree that this project and the contribution are 84 | public and that a record of the contribution (including all 85 | personal information I submit with it, including my sign-off) is 86 | maintained indefinitely and may be redistributed consistent with 87 | this project or the open source license(s) involved. 88 | 89 | 90 | then you just add a line saying 91 | 92 | Signed-off-by: Random J Developer 93 | 94 | using your real name (sorry, no pseudonyms or anonymous contributions.) 95 | ``` 96 | 97 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | Lightweight Java wrapper around OVH's APIs. Handles all the hard work including credential creation and requests signing. 2 | 3 | .. code:: java 4 | 5 | import com.ovh.api.OvhApi; 6 | 7 | public class OvhApiTest { 8 | 9 | public void testCall() throws OvhApiException { 10 | String endpoint = "ovh-eu"; 11 | String appKey = "0000000000000000"; 12 | String appSecret = "00000000000000000000000000000000"; 13 | String consumerKey = "00000000000000000000000000000000"; 14 | 15 | OvhApi api = new OvhApi(endpoint, appKey, appSecret, consumerKey); 16 | try { 17 | api.get("/me"); 18 | } catch (OvhApiException e) { 19 | System.out.prinln(e); 20 | } 21 | } 22 | } 23 | 24 | The wrapper accepts and returns raw json Strings. You can serialize/deserialize it with any external library, the following example uses Gson from Google. 25 | 26 | .. code:: java 27 | 28 | import com.ovh.api.OvhApi; 29 | import com.google.gson.Gson; 30 | 31 | public class OvhApiTest { 32 | 33 | public class Me { 34 | 35 | public String firstname; 36 | public String name; 37 | public String nichandle; 38 | 39 | @Override 40 | public String toString() { 41 | return "Me [firstname=" + firstname + ", name=" + name + ", nichandle=" + nichandle + "]"; 42 | } 43 | 44 | } 45 | 46 | public void testCall() throws OvhApiException { 47 | String endpoint = "ovh-eu"; 48 | String appKey = "0000000000000000"; 49 | String appSecret = "00000000000000000000000000000000"; 50 | String consumerKey = "00000000000000000000000000000000"; 51 | 52 | OvhApi api = new OvhApi(endpoint, appKey, appSecret, consumerKey); 53 | try { 54 | String json = api.get("/me"); 55 | Gson gson = new Gson(); 56 | Me me = gson.fromJson(json, Me.class); 57 | System.out.println(json); 58 | System.out.println(me.toString()); 59 | } catch (OvhApiException e) { 60 | System.out.prinln(e); 61 | } 62 | } 63 | } 64 | 65 | Configuration 66 | ============= 67 | 68 | The straightforward way to use OVH's API keys is to embed them directly in the 69 | application code. While this is very convenient, it lacks of elegance and 70 | flexibility. 71 | 72 | Alternatively it is suggested to use configuration files or environment 73 | variables so that the same code may run seamlessly in multiple environments. 74 | Production and development for instance. 75 | 76 | This wrapper will first look for direct instantiation parameters then 77 | ``OVH_ENDPOINT``, ``OVH_APPLICATION_KEY``, ``OVH_APPLICATION_SECRET`` and 78 | ``OVH_CONSUMER_KEY`` environment variables. If either of these parameter is not 79 | provided, it will look for a configuration file of the form: 80 | 81 | .. code:: ini 82 | 83 | endpoint=ovh-eu 84 | application_key=my_app_key 85 | application_secret=my_application_secret 86 | consumer_key=my_consumer_key 87 | 88 | The client will successively attempt to locate this configuration file in 89 | 90 | 1. Current working directory: ``./ovh.conf`` 91 | 2. Current user's home directory ``~/.ovh.conf`` 92 | 3. System wide configuration ``/etc/ovh.conf`` 93 | 94 | This lookup mechanism makes it easy to overload credentials for a specific 95 | project or user. 96 | 97 | Get the sources 98 | --------------- 99 | 100 | The project is hosted on github and uses gradle as a build system. 101 | 102 | .. code:: bash 103 | 104 | git clone https://github.com/ovh/java-ovh.git 105 | cd java-ovh 106 | ./gradlew build 107 | 108 | The compiled library will be at build/libs/java-ovh.jar 109 | 110 | You've developed a new cool feature ? Fixed an annoying bug ? We'd be happy 111 | to hear from you ! 112 | 113 | Run the tests 114 | ------------- 115 | 116 | .. code:: bash 117 | 118 | ./gradlew test 119 | 120 | See the report at buid/reports/tests/index.html 121 | 122 | 123 | Supported APIs 124 | ============== 125 | 126 | OVH Europe 127 | ---------- 128 | 129 | - **Documentation**: https://eu.api.ovh.com/ 130 | - **Community support**: api-subscribe@ml.ovh.net 131 | - **Console**: https://eu.api.ovh.com/console 132 | - **Create application credentials**: https://eu.api.ovh.com/createApp/ 133 | - **Create script credentials** (all keys at once): https://eu.api.ovh.com/createToken/ 134 | 135 | OVH North America 136 | ----------------- 137 | 138 | - **Documentation**: https://ca.api.ovh.com/ 139 | - **Community support**: api-subscribe@ml.ovh.net 140 | - **Console**: https://ca.api.ovh.com/console 141 | - **Create application credentials**: https://ca.api.ovh.com/createApp/ 142 | - **Create script credentials** (all keys at once): https://ca.api.ovh.com/createToken/ 143 | 144 | So you Start Europe 145 | ------------------- 146 | 147 | - **Documentation**: https://eu.api.soyoustart.com/ 148 | - **Community support**: api-subscribe@ml.ovh.net 149 | - **Console**: https://eu.api.soyoustart.com/console/ 150 | - **Create application credentials**: https://eu.api.soyoustart.com/createApp/ 151 | - **Create script credentials** (all keys at once): https://eu.api.soyoustart.com/createToken/ 152 | 153 | So you Start North America 154 | -------------------------- 155 | 156 | - **Documentation**: https://ca.api.soyoustart.com/ 157 | - **Community support**: api-subscribe@ml.ovh.net 158 | - **Console**: https://ca.api.soyoustart.com/console/ 159 | - **Create application credentials**: https://ca.api.soyoustart.com/createApp/ 160 | - **Create script credentials** (all keys at once): https://ca.api.soyoustart.com/createToken/ 161 | 162 | Kimsufi Europe 163 | -------------- 164 | 165 | - **Documentation**: https://eu.api.kimsufi.com/ 166 | - **Community support**: api-subscribe@ml.ovh.net 167 | - **Console**: https://eu.api.kimsufi.com/console/ 168 | - **Create application credentials**: https://eu.api.kimsufi.com/createApp/ 169 | - **Create script credentials** (all keys at once): https://eu.api.kimsufi.com/createToken/ 170 | 171 | Kimsufi North America 172 | --------------------- 173 | 174 | - **Documentation**: https://ca.api.kimsufi.com/ 175 | - **Community support**: api-subscribe@ml.ovh.net 176 | - **Console**: https://ca.api.kimsufi.com/console/ 177 | - **Create application credentials**: https://ca.api.kimsufi.com/createApp/ 178 | - **Create script credentials** (all keys at once): https://ca.api.kimsufi.com/createToken/ 179 | 180 | Runabove 181 | -------- 182 | 183 | - **Community support**: https://community.runabove.com/ 184 | - **Console**: https://api.runabove.com/console/ 185 | - **Create application credentials**: https://api.runabove.com/createApp/ 186 | - **High level SDK**: https://github.com/runabove/python-runabove 187 | 188 | License 189 | ======= 190 | 191 | 3-Clause BSD -------------------------------------------------------------------------------- /src/main/java/com/ovh/api/OvhApi.java: -------------------------------------------------------------------------------- 1 | package com.ovh.api; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.DataOutputStream; 5 | import java.io.File; 6 | import java.io.FileInputStream; 7 | import java.io.IOException; 8 | import java.io.InputStreamReader; 9 | import java.io.UnsupportedEncodingException; 10 | import java.net.HttpURLConnection; 11 | import java.net.URL; 12 | import java.security.MessageDigest; 13 | import java.security.NoSuchAlgorithmException; 14 | import java.util.HashMap; 15 | import java.util.Map; 16 | import java.util.Properties; 17 | 18 | import com.ovh.api.OvhApiException.OvhApiExceptionCause; 19 | 20 | /** 21 | * Simple low level wrapper over the OVH REST API. 22 | * 23 | * 24 | * @author mbsk 25 | * 26 | */ 27 | public class OvhApi { 28 | 29 | private final String endpoint; 30 | private final String appKey; 31 | private final String appSecret; 32 | private final String consumerKey; 33 | 34 | private final static Map endpoints; 35 | 36 | static { 37 | endpoints = new HashMap<>(); 38 | endpoints.put("ovh-eu", "https://eu.api.ovh.com/1.0"); 39 | endpoints.put("ovh-ca", "https://ca.api.ovh.com/1.0"); 40 | endpoints.put("kimsufi-eu", "https://eu.api.kimsufi.com/1.0"); 41 | endpoints.put("kimsufi-ca", "https://ca.api.kimsufi.com/1.0"); 42 | endpoints.put("soyoustart-eu", "https://eu.api.soyoustart.com/1.0"); 43 | endpoints.put("soyoustart-ca", "https://ca.api.soyoustart.com/1.0"); 44 | endpoints.put("runabove", "https://api.runabove.com/1.0"); 45 | endpoints.put("runabove-ca", "https://api.runabove.com/1.0"); 46 | } 47 | 48 | public OvhApi() throws OvhApiException { 49 | super(); 50 | 51 | Map env = System.getenv(); 52 | if(env.containsKey("OVH_ENDPOINT") && env.containsKey("OVH_APPLICATION_KEY") && env.containsKey("OVH_APPLICATION_SECRET") && env.containsKey("OVH_CONSUMER_KEY")) { 53 | endpoint = System.getenv("OVH_ENDPOINT"); 54 | appKey = System.getenv("OVH_APPLICATION_KEY"); 55 | appSecret = System.getenv("OVH_APPLICATION_SECRET"); 56 | consumerKey = System.getenv("OVH_CONSUMER_KEY"); 57 | } else { 58 | // find the config file 59 | File configFile = new File("ovh.conf"); 60 | if(!configFile.exists()) { 61 | String userHomePath = System.getProperty("user.home"); 62 | configFile = new File(userHomePath+"/ovh.conf"); 63 | if(!configFile.exists()) { 64 | configFile = new File("/etc/ovh.conf"); 65 | } 66 | } 67 | 68 | if(configFile.exists()) { 69 | try { 70 | // read the configuration file 71 | Properties config = new Properties(); 72 | config.load(new FileInputStream(configFile)); 73 | 74 | // get the values 75 | endpoint = config.getProperty("endpoint", null); 76 | appKey = config.getProperty("application_key", null); 77 | appSecret = config.getProperty("application_secret", null); 78 | consumerKey = config.getProperty("consumer_key", null); 79 | 80 | } catch (Exception e) { 81 | throw new OvhApiException(e.getMessage(), OvhApiExceptionCause.CONFIG_ERROR); 82 | } 83 | } else { 84 | throw new OvhApiException("environnment variables OVH_ENDPOINT, OVH_APPLICATION_KEY, OVH_APPLICATION_SECRET, OVH_CONSUMER_KEY or configuration files ./ovh.conf, ~/ovh.conf, /etc/ovh.conf were not found", OvhApiExceptionCause.CONFIG_ERROR); 85 | } 86 | } 87 | 88 | } 89 | 90 | public OvhApi(String endpoint, String appKey, String appSecret, String consumerKey) { 91 | this.endpoint = endpoint; 92 | this.appKey = appKey; 93 | this.appSecret = appSecret; 94 | this.consumerKey = consumerKey; 95 | } 96 | 97 | private void assertAllConfigNotNull() throws OvhApiException{ 98 | if(endpoint==null || appKey==null || appSecret==null || consumerKey==null) { 99 | throw new OvhApiException("", OvhApiExceptionCause.CONFIG_ERROR); 100 | } 101 | } 102 | 103 | public String get(String path) throws OvhApiException { 104 | assertAllConfigNotNull(); 105 | return get(path, "", true); 106 | } 107 | 108 | public String get(String path, boolean needAuth) throws OvhApiException { 109 | assertAllConfigNotNull(); 110 | return get(path, "", needAuth); 111 | } 112 | 113 | public String get(String path, String body, boolean needAuth) throws OvhApiException { 114 | assertAllConfigNotNull(); 115 | return call("GET", body, appKey, appSecret, consumerKey, endpoint, path, needAuth); 116 | } 117 | 118 | public String put(String path, String body, boolean needAuth) throws OvhApiException { 119 | assertAllConfigNotNull(); 120 | return call("PUT", body, appKey, appSecret, consumerKey, endpoint, path, needAuth); 121 | } 122 | 123 | public String post(String path, String body, boolean needAuth) throws OvhApiException { 124 | assertAllConfigNotNull(); 125 | return call("POST", body, appKey, appSecret, consumerKey, endpoint, path, needAuth); 126 | } 127 | 128 | public String delete(String path, String body, boolean needAuth) throws OvhApiException { 129 | assertAllConfigNotNull(); 130 | return call("DELETE", body, appKey, appSecret, consumerKey, endpoint, path, needAuth); 131 | } 132 | 133 | private String call(String method, String body, String appKey, String appSecret, String consumerKey, String endpoint, String path, boolean needAuth) throws OvhApiException 134 | { 135 | 136 | try { 137 | String indexedEndpoint = endpoints.get(endpoint); 138 | endpoint = (indexedEndpoint==null)?endpoint:indexedEndpoint; 139 | 140 | URL url = new URL(new StringBuilder(endpoint).append(path).toString()); 141 | 142 | // prepare 143 | HttpURLConnection request = (HttpURLConnection) url.openConnection(); 144 | request.setRequestMethod(method); 145 | request.setReadTimeout(30000); 146 | request.setConnectTimeout(30000); 147 | request.setRequestProperty("Content-Type", "application/json"); 148 | request.setRequestProperty("X-Ovh-Application", appKey); 149 | // handle authentification 150 | if(needAuth) { 151 | // get timestamp from local system 152 | long timestamp = System.currentTimeMillis() / 1000; 153 | 154 | // build signature 155 | String toSign = new StringBuilder(appSecret) 156 | .append("+") 157 | .append(consumerKey) 158 | .append("+") 159 | .append(method) 160 | .append("+") 161 | .append(url) 162 | .append("+") 163 | .append(body) 164 | .append("+") 165 | .append(timestamp) 166 | .toString(); 167 | String signature = new StringBuilder("$1$").append(HashSHA1(toSign)).toString(); 168 | 169 | // set HTTP headers for authentication 170 | request.setRequestProperty("X-Ovh-Consumer", consumerKey); 171 | request.setRequestProperty("X-Ovh-Signature", signature); 172 | request.setRequestProperty("X-Ovh-Timestamp", Long.toString(timestamp)); 173 | } 174 | 175 | if(body != null && !body.isEmpty()) 176 | { 177 | request.setDoOutput(true); 178 | DataOutputStream out = new DataOutputStream(request.getOutputStream()); 179 | out.writeBytes(body); 180 | out.flush(); 181 | out.close(); 182 | } 183 | 184 | 185 | String inputLine; 186 | BufferedReader in; 187 | int responseCode = request.getResponseCode(); 188 | if (responseCode == 200) { 189 | in = new BufferedReader(new InputStreamReader(request.getInputStream())); 190 | } else { 191 | in = new BufferedReader(new InputStreamReader(request.getErrorStream())); 192 | } 193 | 194 | // build response 195 | StringBuilder response = new StringBuilder(); 196 | while ((inputLine = in.readLine()) != null) { 197 | response.append(inputLine); 198 | } 199 | in.close(); 200 | 201 | if(responseCode == 200) { 202 | // return the raw JSON result 203 | return response.toString(); 204 | } else if(responseCode == 400) { 205 | throw new OvhApiException(response.toString(), OvhApiExceptionCause.BAD_PARAMETERS_ERROR); 206 | } else if (responseCode == 403) { 207 | throw new OvhApiException(response.toString(), OvhApiExceptionCause.AUTH_ERROR); 208 | } else if (responseCode == 404) { 209 | throw new OvhApiException(response.toString(), OvhApiExceptionCause.RESSOURCE_NOT_FOUND); 210 | } else if (responseCode == 409) { 211 | throw new OvhApiException(response.toString(), OvhApiExceptionCause.RESSOURCE_CONFLICT_ERROR); 212 | } else { 213 | throw new OvhApiException(response.toString(), OvhApiExceptionCause.API_ERROR); 214 | } 215 | 216 | } catch (NoSuchAlgorithmException e) { 217 | throw new OvhApiException(e.getMessage(), OvhApiExceptionCause.INTERNAL_ERROR); 218 | } catch (IOException e) { 219 | throw new OvhApiException(e.getMessage(), OvhApiExceptionCause.INTERNAL_ERROR); 220 | } 221 | 222 | } 223 | 224 | public static String HashSHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException { 225 | MessageDigest md; 226 | md = MessageDigest.getInstance("SHA-1"); 227 | byte[] sha1hash = new byte[40]; 228 | md.update(text.getBytes("iso-8859-1"), 0, text.length()); 229 | sha1hash = md.digest(); 230 | StringBuffer sb = new StringBuffer(); 231 | for (int i = 0; i < sha1hash.length; i++) { 232 | sb.append(Integer.toString((sha1hash[i] & 0xff) + 0x100, 16).substring(1)); 233 | } 234 | return sb.toString(); 235 | } 236 | 237 | } 238 | --------------------------------------------------------------------------------