Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea/
Empty file removed tasks/i
Empty file.
Binary file added tasks/maven/hw/.mvn/wrapper/maven-wrapper.jar
Binary file not shown.
18 changes: 18 additions & 0 deletions tasks/maven/hw/.mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Licensed to the Apache Software Foundation (ASF) under one
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

в репозитории должно быть только то, что находится внутри твоего проекта, Т.е. рутовая помка и все что находится на ее уровне. У тебя почему-то tasks/maven/hw - так не должно быть

# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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
#
# http://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.
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.2/apache-maven-3.9.2-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar
26 changes: 26 additions & 0 deletions tasks/maven/hw/common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.dmdev</groupId>
<artifactId>homework-maven</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>common</artifactId>

<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230227</version>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не должно быть лишних зависимостей в проекте

</dependency>
</dependencies>

<properties>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

properties тоже наследуются, как и dependency, plugins, etc. Поэтому достаточно определить их лишь раз в parent pom

</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.dmdev.common.models;


public class User {
private String firstName;
private String lastName;
private String email;
private String nickname;

public User(String firstName, String lastName, String email, String nickname) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.nickname = nickname;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getNickname() {
return nickname;
}

public void setNickname(String nickname) {
this.nickname = nickname;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.dmdev.common.repository;

import com.dmdev.common.models.User;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class UserRepository {

public static User getUser(Long id) {
String url = "http://userrepository.com/api/id/";

String response = performGetRequest(url, id);
User user = processResponse(response);

System.out.println("Имя: " + user.getFirstName());
System.out.println("Фамилия: " + user.getLastName());
System.out.println("Email: " + user.getEmail());
System.out.println("Никнейм: " + user.getNickname());
return user;
}

public static String performGetRequest(String url, Long id) {
StringBuilder responseBuilder = new StringBuilder();
try {
URL apiUrl = new URL(url + id);

HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
connection.setRequestMethod("GET");

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
responseBuilder.append(line);
}
reader.close();

connection.disconnect();
} catch (IOException e) {
System.out.println("Request error: " + e);
}

return responseBuilder.toString();
}

private static User processResponse(String response) {
try {
JSONObject json = new JSONObject(response);

String firstName = json.optString("firstName", "Default");
String lastName = json.optString("lastName", "Default");
String email = json.optString("email", "default@example.com");
String nickname = json.optString("nickname", "");

return new User(firstName, lastName, email, nickname);
} catch (JSONException e) {
e.printStackTrace();
return new User("Default", "Default", "default@example.com", "");
}
}
}
4 changes: 4 additions & 0 deletions tasks/maven/hw/common/src/test/java/com/dmdev/AppIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.dmdev;

public class AppIT {
}
4 changes: 4 additions & 0 deletions tasks/maven/hw/common/src/test/java/com/dmdev/AppTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.dmdev;

public class AppTest {
}
Loading