Skip to content

Commit

Permalink
Merge pull request #65 from mrmike/migrate-project-to-kotlin
Browse files Browse the repository at this point in the history
Convert this project to Kotlin!
  • Loading branch information
mrmike committed Apr 10, 2022
2 parents 16d5586 + 443891d commit f1ede57
Show file tree
Hide file tree
Showing 54 changed files with 1,183 additions and 1,384 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/push.yml
Expand Up @@ -11,7 +11,5 @@ jobs:
uses: actions/setup-java@v1
with:
java-version: '11'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew clean build
90 changes: 61 additions & 29 deletions README.md
@@ -1,6 +1,6 @@
# Ok2Curl [![Commit check](https://github.com/mrmike/Ok2Curl/actions/workflows/push.yml/badge.svg)](https://github.com/mrmike/Ok2Curl/actions/workflows/push.yml) [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-Ok2Curl-green.svg?style=flat)](https://android-arsenal.com/details/1/2653) [![Release](https://jitpack.io/v/mrmike/Ok2Curl.svg)](https://jitpack.io/#mrmike/Ok2Curl)

Convert OkHttp requests into curl logs.
Simply way to transform OkHttp requests into curl logs.

## Usage
Add library to project dependencies. Library is hosted on Maven Central.
Expand All @@ -10,20 +10,19 @@ repositories {
}
dependencies {
implementation 'com.github.mrmike:ok2curl:0.7.0'
implementation 'com.github.mrmike:ok2curl:0.8.0'
}
```

To start logging requests with Ok2Curl add interceptor to OkHttp client.
```java
OkHttpClient okHttp = new OkHttpClient.Builder()
.addInterceptor(new CurlInterceptor(new Loggable() {
@Override
public void log(String message) {
Log.v("Ok2Curl", message);
}
}))
.build();
```kotlin
val client = OkHttpClient.Builder()
.addInterceptor(CurlInterceptor(object : Logger {
override fun log(message: String) {
Log.v("Ok2Curl", message)
}
}))
.build()
```

## Result
Expand All @@ -36,34 +35,67 @@ curl -X GET -H "Cache-Control:max-stale=2147483647, only-if-cached" https://api.
## Network interceptors
By default Ok2Curl uses application interceptors from OkHttp which is adequate for most cases. But sometimes you may want to use network interceptor e.g. to log Cookies set via [CookieHandler](http://docs.oracle.com/javase/6/docs/api/java/net/CookieHandler.html). In such a case add interceptor the same way as below:

```
OkHttpClient okHttp = new OkHttpClient.Builder()
.addNetworkInterceptor(new CurlInterceptor())
.build();
```kotlin
val client = OkHttpClient.Builder()
.addNetworkInterceptor(CurlInterceptor(logger))
.build()
```

To get know more about Interceptor in OkHttp take a look here: https://github.com/square/okhttp/wiki/Interceptors

## Configuration

`CurlInterceptor` accepts an optional configuration object. With `Configuration` you can specify various options like:
* header modifiers - custom logic for modifying header values
* components - list of required command components
* flags - optional curl flags
* limit - bytes limit for body
* delimiter for command components

```kotlin
class Configuration(
val headerModifiers: List<HeaderModifier> = emptyList(),
val components: List<CommandComponent> = CommandComponent.DEFAULT,
val flags: Flags = Flags.EMPTY,
val limit: Long = 1024L * 1024L,
val delimiter: String = " "
)
```

## Header modifiers
Ok2Curl allows you to modify any header before creating curl command. All you have to do is create your own modifier that implements [HeaderModifier](https://github.com/mrmike/Ok2Curl/blob/master/ok2curl/src/main/java/com/moczul/ok2curl/modifier/HeaderModifier.java)
and add this modifier to CurlInterceptor. See [sample](https://github.com/mrmike/Ok2Curl/blob/master/sample/src/main/java/com/moczul/sample/RequestService.java) for reference.
Ok2Curl allows you to modify any header before creating curl command. All you have to do is create your own modifier that implements [HeaderModifier](https://github.com/mrmike/Ok2Curl/blob/master/ok2curl/src/main/java/com/moczul/ok2curl/modifier/HeaderModifier.kt)
and add this modifier to CurlInterceptor. See [sample](https://github.com/mrmike/Ok2Curl/blob/master/sample/src/main/java/com/moczul/sample/RequestService.kt) for reference.
```
val modifier = BasicAuthorizationHeaderModifier(Base64Decoder())
val configuration = Configuration(headerModifiers = listOf(modifier))
val curlInterceptor = CurlInterceptor(AndroidLogger(), configuration)
```
final BasicAuthorizationHeaderModifier modifier = new BasicAuthorizationHeaderModifier(new Base64Decoder());
final List<HeaderModifier> modifiers = Collections.<HeaderModifier>singletonList(modifier);

final CurlInterceptor curlInterceptor = new CurlInterceptor(new AndroidLogger(), modifiers);
## Command Components
With Ok2Curl configuration you can specify a list of components for curl command. For instance,
you can skip header, body, and flag components.
```kotlin
val components = listOf(Curl, Method, Url)
val configuration = Configuration(components = components)
val curlInterceptor = CurlInterceptor(AndroidLogger(), configuration)
```

## Options
Ok2Curl supports basic Curl options. In order to use options use the following code:
As a result, CurlInterceptor will receive given simplified command
```shell
// Headers, body and flags are skipped
curl -X GET https://api.github.com/repos/vmg/redcarpet/issues?state=closed
```
final Options options = Options.builder()
.insecure()
.connectTimeout(120)
.retry(5)
.build();

final CurlInterceptor interceptor = new CurlInterceptor(logger, options);
## Flags
Ok2Curl supports basic Curl options. To use options use the following code:
```kotlin
val flags = Flags.builder()
.insecure()
.connectTimeout(seconds = 120)
.retry(5)
.build()
val configuration = Configuration(flags = flags)
val curlInterceptor = CurlInterceptor(AndroidLogger(), configuration)
```
Since now every Curl command will start with `curl --insecure --connect-timeout 120 --retry 5...`

Expand All @@ -75,7 +107,7 @@ Since now every Curl command will start with `curl --insecure --connect-timeout
* --compressed
* --location

If would like to support any new options please feel free to open PR. Full list of curl options is
If would like to support any new options please feel free to open PR. A full list of curl options is
available [here](https://curl.haxx.se/docs/manpage.html).


Expand Down
6 changes: 5 additions & 1 deletion build.gradle
@@ -1,13 +1,17 @@
buildscript {
ext.kotlin_version = '1.6.20'

repositories {
google()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}

dependencies {
classpath 'com.android.tools.build:gradle:7.0.4'
classpath 'com.android.tools.build:gradle:7.1.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
classpath 'io.github.gradle-nexus:publish-plugin:1.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
53 changes: 33 additions & 20 deletions gradlew
@@ -1,5 +1,21 @@
#!/usr/bin/env sh

#
# Copyright 2015 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

##############################################################################
##
## Gradle start up script for UN*X
Expand Down Expand Up @@ -28,7 +44,7 @@ APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m"'
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
Expand Down Expand Up @@ -66,6 +82,7 @@ esac

CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar


# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
Expand Down Expand Up @@ -109,10 +126,11 @@ if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi

# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
# For Cygwin or MSYS, switch paths to Windows format before running java
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`

JAVACMD=`cygpath --unix "$JAVACMD"`

# We build the pattern for arguments to be converted via cygpath
Expand All @@ -138,19 +156,19 @@ if $cygwin ; then
else
eval `echo args$i`="\"$arg\""
fi
i=$((i+1))
i=`expr $i + 1`
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
0) set -- ;;
1) set -- "$args0" ;;
2) set -- "$args0" "$args1" ;;
3) set -- "$args0" "$args1" "$args2" ;;
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi

Expand All @@ -159,14 +177,9 @@ save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
APP_ARGS=$(save "$@")
APP_ARGS=`save "$@"`

# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"

# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
cd "$(dirname "$0")"
fi

exec "$JAVACMD" "$@"
43 changes: 24 additions & 19 deletions gradlew.bat
@@ -1,3 +1,19 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem

@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
Expand All @@ -13,15 +29,18 @@ if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m"
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
if "%ERRORLEVEL%" == "0" goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Expand All @@ -35,7 +54,7 @@ goto fail
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto init
if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
Expand All @@ -45,28 +64,14 @@ echo location of your Java installation.

goto fail

:init
@rem Get command-line arguments, handling Windows variants

if not "%OS%" == "Windows_NT" goto win9xME_args

:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2

:win9xME_args_slurp
if "x%~1" == "x" goto execute

set CMD_LINE_ARGS=%*

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar


@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*

:end
@rem End local scope for the variables with windows NT shell
Expand Down
16 changes: 14 additions & 2 deletions ok2curl/build.gradle
@@ -1,9 +1,9 @@
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'maven-publish'

ext {
PUBLISH_GROUP_ID = 'com.github.mrmike'
PUBLISH_VERSION = '0.7.1'
PUBLISH_VERSION = '0.8.0'
PUBLISH_ARTIFACT_ID = 'ok2curl'
}

Expand All @@ -12,8 +12,20 @@ apply from: "${rootProject.projectDir}/scripts/publish-module.gradle"
targetCompatibility = '1.8'
sourceCompatibility = '1.8'

compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {
implementation 'com.squareup.okhttp3:okhttp:4.9.3'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito:mockito-core:2.23.0'
Expand Down
16 changes: 16 additions & 0 deletions ok2curl/src/main/java/com/moczul/ok2curl/CommandComponent.kt
@@ -0,0 +1,16 @@
package com.moczul.ok2curl

sealed class CommandComponent {

object Curl : CommandComponent()
object Flags : CommandComponent()
object Method : CommandComponent()
object Header : CommandComponent()
object Body : CommandComponent()
object Url : CommandComponent()

companion object {
@JvmField
val DEFAULT = listOf(Curl, Flags, Method, Header, Body, Url)
}
}

0 comments on commit f1ede57

Please sign in to comment.