API Wilayah Indonesia

Menyediakan data wilayah seluruh Indonesia mulai dari provinsi, kabupaten/kota, kecamatan dan desa

Penggunaan di C#

1. Membuat Class

        
            public class provinsi
            {
                public provinsi()
                {

                }

                public string id { get; set; }
                public string description { get; set; }
            }

            public class kabupaten
            {
                public kabupaten()
                {

                }

                public string provinsi_id { get; set; }
                public string id { get; set; }
                public string description { get; set; }
            }

            public class kecamatan
            {
                public kecamatan()
                {

                }

                public string kabupaten_id { get; set; }
                public string id { get; set; }
                public string description { get; set; }
            }

            public class desa
            {
                public desa()
                {

                }

                public string kecamatan_id { get; set; }
                public string id { get; set; }
                public string description { get; set; }
            }
        
    

2. Fungsi

        

            string Baseurl = "https://api-wilayah.belajardisiniaja.com";

            string ApiKey = "uowueorwer7973220u302u20302u";

            public List GetWilayah(string wilayah, string id = null)
            {
                List sInfo = new List();
                using (HttpClient client = new HttpClient())
                {
                    //Passing service base url
                    client.BaseAddress = new Uri(Baseurl);
                    client.DefaultRequestHeaders.Clear();
                    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + ApiKey);
                    //Tentukan format data permintaan
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    //Sending request to find web api REST service resource GetAllEmployees using HttpClient
                    HttpResponseMessage Res = client.GetAsync("/" + wilayah + "/" + id).Result;
                    //Checking the response is successful or not which is sent using HttpClient
                    if (Res.IsSuccessStatusCode)
                    {
                        //Storing the response details recieved from web api
                        var sResponse = Res.Content.ReadAsStringAsync().Result;
                        //Deserializing the response recieved from web api and storing into the Employee list
                        sInfo = JsonConvert.DeserializeObject>(sResponse);
                    }
                    //returning the employee list to view
                    return sInfo;
                }
            }