package com.example.register.netrequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.register.entity.OrangeMessage;

import java.io.IOException;
import java.util.Objects;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OkHttpMessage {

    /**
     * 消费消息
     *
     * @return
     */
    public OrangeMessage consumeMessage() throws IOException {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url("http://192.168.43.115:8081/orange/message/consumeMessage")
                .build();
        Response response = okHttpClient.newCall(request).execute();
        JSONObject jsonObject = JSON.parseObject(Objects.requireNonNull(response.body()).string());
        OrangeMessage orangeMessage = JSON.toJavaObject(jsonObject.getJSONObject("data"), OrangeMessage.class);
        /**
         * 消费完消息以后，设置消息为已读
         */
        Request request1 = new Request.Builder()
                .url("http://192.168.43.115:8081/orange/message/updateMessage")
                .build();
        okHttpClient.newCall(request1).execute();
        return orangeMessage;
    }

}
