Skip to content

Commit 23ed9e6

Browse files
committed
Добавлен модуль okhttp
1 parent d72fad6 commit 23ed9e6

16 files changed

+1057
-271
lines changed

build.gradle

Lines changed: 143 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,144 @@
1-
buildscript {
2-
repositories {
3-
mavenCentral()
4-
}
5-
dependencies {
6-
classpath 'net.sf.proguard:proguard-gradle:6.0.1'
7-
}
8-
}
9-
10-
plugins {
11-
id "java"
12-
id "com.github.johnrengelman.shadow" version "2.0.2"
13-
id "org.sonarqube" version "2.6.2"
14-
}
15-
16-
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
17-
import proguard.gradle.ProGuardTask
18-
19-
20-
sourceCompatibility = '1.8'
21-
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
22-
23-
if (!hasProperty('mainClass')) {
24-
ext.mainClass = 'com.annimon.ownlang.Main'
25-
}
26-
27-
ext.generatedJavaDir = "${rootProject.projectDir}/src/main/generatedJava"
28-
sourceSets.main.java.srcDirs += project.generatedJavaDir
29-
30-
repositories {
31-
jcenter()
32-
}
33-
34-
task generateJavaSources() {
35-
doLast {
36-
def source = """
37-
package com.annimon.ownlang;
38-
class Gen {
39-
private Gen() {}
40-
public static final String BUILD_DATE = "${new Date().format('YYMMdd')}";
41-
}
42-
"""
43-
def genFile = new File("${project.generatedJavaDir}/com/annimon/ownlang/Gen.java")
44-
genFile.getParentFile().mkdirs()
45-
genFile.write(source)
46-
}
47-
}
48-
compileJava.dependsOn(generateJavaSources)
49-
50-
task run(dependsOn: classes, type: JavaExec) {
51-
main = project.mainClass
52-
classpath = sourceSets.main.runtimeClasspath
53-
standardInput = System.in
54-
ignoreExitValue = true
55-
}
56-
57-
task runOptimizing(dependsOn: classes, type: JavaExec) {
58-
main = project.mainClass
59-
classpath = sourceSets.main.runtimeClasspath
60-
ignoreExitValue = true
61-
// args '-o 9 -m -a -f examples/game/minesweeper.own'.split(' ')
62-
args '-o 9 -m -a -f program.own'.split(' ')
63-
}
64-
65-
task dist(type: ShadowJar) {
66-
from sourceSets.main.output
67-
configurations = [project.configurations.runtime]
68-
destinationDir file("$rootProject.projectDir/dist")
69-
70-
exclude 'META-INF/*.DSA'
71-
exclude 'META-INF/*.RSA'
72-
exclude 'META-INF/maven/**'
73-
exclude 'LICENSE*'
74-
75-
manifest.attributes(
76-
'Main-Class': project.mainClass,
77-
'Build-Date': new Date().format('YYMMdd')
78-
)
79-
}
80-
81-
task proguard(dependsOn: dist, type: ProGuardTask) {
82-
configuration "$rootProject.projectDir/proguard.properties"
83-
injars "$rootProject.projectDir/dist/OwnLang.jar"
84-
outjars "$rootProject.projectDir/store/OwnLang.jar"
85-
86-
// Automatically handle the Java version of this build.
87-
if (System.getProperty('java.version').startsWith('1.')) {
88-
// Before Java 9, the runtime classes were packaged in a single jar file.
89-
libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
90-
} else {
91-
// As of Java 9, the runtime classes are packaged in modular jmod files.
92-
def jmods = files { file("${System.getProperty('java.home')}/jmods").listFiles() }
93-
jmods.each {
94-
libraryjars it, jarfilter: '!**.jar', filter: '!module-info.class'
95-
}
96-
}
97-
}
98-
99-
task sandbox(dependsOn: proguard, type: Jar) {
100-
from zipTree("$rootProject.projectDir/store/OwnLang.jar")
101-
libsDirName = "$rootProject.projectDir/store"
102-
appendix = "Sandbox"
103-
104-
exclude "**/modules/canvas/**", "**/modules/canvasfx/**", "**/modules/forms/**",
105-
"**/modules/java/**", "**/modules/jdbc/**", "**/modules/robot/**",
106-
"**/modules/socket/**", "io/**",
107-
"**/modules/aimp/**", "aimpremote/**",
108-
"**/modules/downloader/**",
109-
"**/modules/zip/**", "**/modules/gzip/**",
110-
"jline/**", "org/fusesource/**", "META-INF/native/**"
111-
112-
manifest {
113-
attributes 'Main-Class': project.mainClass
114-
}
115-
}
116-
117-
dependencies {
118-
compile ('io.socket:socket.io-client:1.0.0') {
119-
exclude group: 'org.json', module: 'json'
120-
}
121-
compile 'org.json:json:20180130'
122-
compile 'org.yaml:snakeyaml:1.20'
123-
compile 'jline:jline:2.14.5'
124-
125-
testImplementation 'junit:junit:4.12'
126-
testImplementation 'org.openjdk.jmh:jmh-core:1.13'
127-
testImplementation 'org.openjdk.jmh:jmh-generator-annprocess:1.13'
128-
}
129-
130-
sonarqube {
131-
properties {
132-
property "sonar.projectName", "Own-Programming-Language-Tutorial"
133-
property "sonar.projectKey", "aNNiMON_Own-Programming-Language-Tutorial"
134-
property "sonar.host.url", "https://sonarcloud.io"
135-
}
136-
}
137-
138-
test {
139-
testLogging {
140-
events "passed", "skipped", "failed"
141-
exceptionFormat "full"
142-
}
1+
buildscript {
2+
repositories {
3+
mavenCentral()
4+
}
5+
dependencies {
6+
classpath 'net.sf.proguard:proguard-gradle:6.0.1'
7+
}
8+
}
9+
10+
plugins {
11+
id "java"
12+
id "com.github.johnrengelman.shadow" version "2.0.2"
13+
id "org.sonarqube" version "2.6.2"
14+
}
15+
16+
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
17+
import proguard.gradle.ProGuardTask
18+
19+
20+
sourceCompatibility = '1.8'
21+
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
22+
23+
if (!hasProperty('mainClass')) {
24+
ext.mainClass = 'com.annimon.ownlang.Main'
25+
}
26+
27+
ext.generatedJavaDir = "${rootProject.projectDir}/src/main/generatedJava"
28+
sourceSets.main.java.srcDirs += project.generatedJavaDir
29+
30+
repositories {
31+
jcenter()
32+
}
33+
34+
task generateJavaSources() {
35+
doLast {
36+
def source = """
37+
package com.annimon.ownlang;
38+
class Gen {
39+
private Gen() {}
40+
public static final String BUILD_DATE = "${new Date().format('YYMMdd')}";
41+
}
42+
"""
43+
def genFile = new File("${project.generatedJavaDir}/com/annimon/ownlang/Gen.java")
44+
genFile.getParentFile().mkdirs()
45+
genFile.write(source)
46+
}
47+
}
48+
compileJava.dependsOn(generateJavaSources)
49+
50+
task run(dependsOn: classes, type: JavaExec) {
51+
main = project.mainClass
52+
classpath = sourceSets.main.runtimeClasspath
53+
standardInput = System.in
54+
ignoreExitValue = true
55+
}
56+
57+
task runOptimizing(dependsOn: classes, type: JavaExec) {
58+
main = project.mainClass
59+
classpath = sourceSets.main.runtimeClasspath
60+
ignoreExitValue = true
61+
// args '-o 9 -m -a -f examples/game/minesweeper.own'.split(' ')
62+
args '-o 9 -m -a -f program.own'.split(' ')
63+
}
64+
65+
task dist(type: ShadowJar) {
66+
from sourceSets.main.output
67+
configurations = [project.configurations.runtime]
68+
destinationDir file("$rootProject.projectDir/dist")
69+
70+
exclude 'META-INF/*.DSA'
71+
exclude 'META-INF/*.RSA'
72+
exclude 'META-INF/maven/**'
73+
exclude 'LICENSE*'
74+
75+
manifest.attributes(
76+
'Main-Class': project.mainClass,
77+
'Build-Date': new Date().format('YYMMdd')
78+
)
79+
}
80+
81+
task proguard(dependsOn: dist, type: ProGuardTask) {
82+
configuration "$rootProject.projectDir/proguard.properties"
83+
injars "$rootProject.projectDir/dist/OwnLang.jar"
84+
outjars "$rootProject.projectDir/store/OwnLang.jar"
85+
86+
// Automatically handle the Java version of this build.
87+
if (System.getProperty('java.version').startsWith('1.')) {
88+
// Before Java 9, the runtime classes were packaged in a single jar file.
89+
libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
90+
} else {
91+
// As of Java 9, the runtime classes are packaged in modular jmod files.
92+
def jmods = files { file("${System.getProperty('java.home')}/jmods").listFiles() }
93+
jmods.each {
94+
libraryjars it, jarfilter: '!**.jar', filter: '!module-info.class'
95+
}
96+
}
97+
}
98+
99+
task sandbox(dependsOn: proguard, type: Jar) {
100+
from zipTree("$rootProject.projectDir/store/OwnLang.jar")
101+
libsDirName = "$rootProject.projectDir/store"
102+
appendix = "Sandbox"
103+
104+
exclude "**/modules/canvas/**", "**/modules/canvasfx/**", "**/modules/forms/**",
105+
"**/modules/java/**", "**/modules/jdbc/**", "**/modules/robot/**",
106+
"**/modules/okhttp/**",
107+
"**/modules/socket/**", "io/**",
108+
"**/modules/aimp/**", "aimpremote/**",
109+
"**/modules/downloader/**",
110+
"**/modules/zip/**", "**/modules/gzip/**",
111+
"jline/**", "org/fusesource/**", "META-INF/native/**"
112+
113+
manifest {
114+
attributes 'Main-Class': project.mainClass
115+
}
116+
}
117+
118+
dependencies {
119+
compile ('io.socket:socket.io-client:1.0.0') {
120+
exclude group: 'org.json', module: 'json'
121+
}
122+
compile 'org.json:json:20180130'
123+
compile 'org.yaml:snakeyaml:1.20'
124+
compile 'jline:jline:2.14.5'
125+
126+
testImplementation 'junit:junit:4.12'
127+
testImplementation 'org.openjdk.jmh:jmh-core:1.13'
128+
testImplementation 'org.openjdk.jmh:jmh-generator-annprocess:1.13'
129+
}
130+
131+
sonarqube {
132+
properties {
133+
property "sonar.projectName", "Own-Programming-Language-Tutorial"
134+
property "sonar.projectKey", "aNNiMON_Own-Programming-Language-Tutorial"
135+
property "sonar.host.url", "https://sonarcloud.io"
136+
}
137+
}
138+
139+
test {
140+
testLogging {
141+
events "passed", "skipped", "failed"
142+
exceptionFormat "full"
143+
}
143144
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use ["std", "okhttp"]
2+
3+
// https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/recipes/PostMultipart.java
4+
5+
println okhttp.request()
6+
.header("Authorization", "Client-ID 9199fdef135c122")
7+
.url("https://api.imgur.com/3/image")
8+
.post(MultipartBody.builder()
9+
.setType(MultipartBody.FORM)
10+
.addFormDataPart("title", "Sample image")
11+
.addFormDataPart("image", "image.png", RequestBody.file("image/png", "image.png"))
12+
.build()
13+
)
14+
.newCall(okhttp.client)
15+
.execute()
16+
.body()
17+
.string()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use ["std", "okhttp"]
2+
3+
TOKEN = "your bot token"
4+
5+
println okhttp.request()
6+
.url("https://api.telegram.org/bot" + TOKEN + "/sendVoice")
7+
.post(MultipartBody.builder()
8+
.setType(MultipartBody.FORM)
9+
.addFormData({
10+
"chat_id": "-1001100000123",
11+
"caption": "sample text"
12+
})
13+
.addFormDataPart("voice", "file.ogg",
14+
RequestBody.file("audio/ogg", "voice.ogg"))
15+
.build()
16+
)
17+
.newCall(okhttp.client)
18+
.execute()
19+
.body()
20+
.string()

examples/network/okhttp_websocket.own

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use ["std", "okhttp"]
2+
3+
// https://github.com/square/okhttp/blob/b21ed68c08c2a5c1eb0bbe93a6f720d1aa2820da/samples/guide/src/main/java/okhttp3/recipes/WebSocketEcho.java
4+
5+
okhttp.client.newWebSocket(
6+
okhttp.request().url("ws://echo.websocket.org"),
7+
{
8+
"onOpen": def(ws, resp) {
9+
ws.send("Hello...")
10+
ws.send("...World!")
11+
ws.close(1000, "Goodbye, World!")
12+
},
13+
"onTextMessage": def(ws, text) = echo(text),
14+
"onBytesMessage": def(ws, bytes) = echo(bytes),
15+
"onClosing": def(ws, code, reason) {
16+
ws.close(1000)
17+
echo("CLOSE:", code, reason)
18+
}
19+
}
20+
)

0 commit comments

Comments
 (0)